QED – the Strange Theory of Light and Matter

QED – the Strange Theory of Light and Matter

I would start with the word “RECOMMENDED”.

A few days ago, I finished reading lectures by Professor Richard Feynman “QED: A Strange Light Theory and Substance”. What is that QED is quantum electrodynamics. The main thing – do not be afraid of these words. In fact, everything is much more complicated than you can imagine

We live in a very interesting era of discoveries, known and unknown!

This book (in fact – it’s 4 lectures) has become a bestseller on quantum physics since it is written in the most understandable way and for those who just want to know a little more and for those who already know a lot.

You probably have heard that quanta are behaving like “particles” and as “waves”. It all depends on the observer. That is, if you follow one “electron” – then it will move as one small fraction. And if you do not observe, then more than one electron can appear on the receiver. Riddle to today.

The behavior of light, the behavior of matter at the lowest level that can be represented today. And the most interesting thing is how to measure everything and prove the truth of the theory! Personally, I am always very curious as to how the masses of quanta were measured with great accuracy.

Also, you will understand (if you read a book) why the light is reflected from some surfaces and not absorbed by others.

Recommendations for reading:

Do not try to remember all terms. There are many of them. Just try to understand if you understand the essence of the above.

The fact is that modern physicists themselves are changing in the names of quantum particles and began to give them rather odd names, such as “s-quark”, where “s” – means “strange”. I will not spoil more. Just if you feel that you like to discover a new one, it’s not open – it’s a book for you.

Splitting and execution of JUnit test in multiple threads

Splitting and execution of JUnit test in multiple threads

Hello,

In this article, I will describe and present some example of how it is possible to run JUnit or TestNG test in multiple threads.
Let’s think about the situation that, that there is the test, which needs to validate 10 locales in 10 languages. Locales and languages are always dynamically taken from somewhere.
In this case, the most common way is:

for (Locale locale: locales) {
    for (Language language: languages) {
        assertStuff(locale, language);
    }
}

The problem here is that complexity will be always On^2.

So, what we can do with this?

We have multithreading! Bingo!

So, we can modify an example to something like:

for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    t.start();
}

Now it looks much better. But the problem is that we will not be seeing the error, because of assertions work in the main thread only.

Let’s modify the code a little bit and collect all the threads into the list:

List threads = new ArrayList<>();
for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    threads.add(t);
    t.start();
}

Alright, now we have the list of triggered threads. But what to with that?

After some researching about Java concurrency, I found the way how to implement Concurrent Assertions

    
void assertConcurrent(final String message, final List<? extends Runnable> runnables, final int maxTimeoutSeconds) throws InterruptedException {
        final int numThreads = runnables.size();
        final List exceptions = Collections.synchronizedList(new ArrayList<>());
        final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
        try {
            final CountDownLatch allThreadsReady = new CountDownLatch(numThreads);
            final CountDownLatch afterInitBlocker = new CountDownLatch(1);
            final CountDownLatch allThreadsAreDone = new CountDownLatch(numThreads);
            for (final Runnable submittedTestRunnable : runnables) {
                threadPool.submit(() -> {
                    allThreadsReady.countDown();
                    try {
                        afterInitBlocker.await();
                        submittedTestRunnable.run();
                    } catch (final Throwable e) {
                        exceptions.add(e);
                    } finally {
                        allThreadsAreDone.countDown();
                    }
                });
            }
            assertTrue("Timeout during threads initializing threads.", 
                    allThreadsReady.await(runnables.size() * WAIT_MULTIPLIER, TimeUnit.MILLISECONDS));
            
            afterInitBlocker.countDown();
            assertTrue(message +" timeout! More than" + maxTimeoutSeconds + "seconds", 
                    allThreadsAreDone.await(maxTimeoutSeconds, TimeUnit.SECONDS));
        } finally {
            threadPool.shutdownNow();
        }
        assertTrue(message + "failed with errors" + exceptions, exceptions.isEmpty());
}

And then, the final test could be like:

List threads = new ArrayList<>();
for (Locale locale: locales) {
    Thread t = new Thread(() -> {
        for (Language language: languages) {
            assertStuff(locale, language);
        }
    }

    threads.add(t);
    t.start();
}
assertConcurrent("Failures are found", threads, 120); // 120 is max timeout of the test

The explanation of the Concurrent assertion is not difficult if You have an experience with Multithreading.
But even if not – feel free to use this code

Happy testing!

The Fibonacci sequence number of “1 000 000”?

The Fibonacci sequence number of “1 000 000”?

Most of the people know or at least have heard about the Fibonacci sequence numbers. To be short – Fibonacci sequence numbers is a sum of the previous both numbers.
For example, the 1st and 2nd numbers are 1 and 1.
So, the 3rd = 2.
And 4th = 2 + 1 = 3.
And 5th = 3 + 2 = 5.
And 6th = 5 + 3 = 8, and so on.
So as the result, we have 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ….. Here we can see that the Fibonacci sequence number on the position 10 is equal to 55.
So, how to calculate the Fibonacci sequence number on the position 1 000 000 (1 Million)? Try to find such online service and You will not because the process of calculation is not so easy. The thing is that we cannot represent such long character in any of existing number format (Long, Double, BigInt) whatever. The length of Fibonacci sequence number on the position 1 000 000 is 208 988 digits. it means that if we want to write this number on the wall and the size of 1 digit is 5mm – the wall should be ~ 5.5 square meters.

How to perform the calculation like this one?
The algorithm should be as much optimised as it’s possible. It means that it is impossible to store all the previous values in the array because the size of the array will be extremely huge and it will be impossible to keep the array in memory. The array should self-reducible after adding the new sum of the previous numbers. In this case. Even in the end, the array will be no bigger than ~ 600Kb.

The method of summing two numbers is very simple:
1234
+
_888
——-
2122

But the complexity is growing proportionally to the increasing of number’s size.

If you would like to see the Fibonacci sequence number of 1 million, here it is:

The first 20 numbers: 19532821287077577316  …
The last 20 numbers:  …  68996526838242546875

Now, let’s see the Java solution.

public String getFibonaciDynamic(int arrLength) {
    long startTime = System.currentTimeMillis();
    LinkedList<String> arr = new LinkedList<>();
    arr.add(0, "1"); // Adding the numbers on the 1st and 2nd position
    arr.add(1, "1");

    int i = 0;
    while (i <= arrLength - 3) {
        arr.add(2, bigSum(arr.get(0), arr.get(1)));
        arr.remove(0);
        i++;
        if (i % 10000 == 0) {
            long endTime = System.currentTimeMillis();
            String s1 = "Calculated sequenced number of: " + i + " in " + (endTime - startTime) / 1000 + " sec"; // All the System.out.prinln are needed for watching the progress
            System.out.println(s1);
            String s2 = "Previous number is: " + arr.getFirst();
            System.out.println(s2);
            String s3 = "Last number is: " + arr.getLast();
            System.out.println(s3);
            Utils.writeUsingFileWriter(s1 + "\n" + s2 + "\n" + s3, "fib_" + i);
            System.out.println();
        }
    }

    long endTime = System.currentTimeMillis();
    System.out.println("Fibonacci sequence with length " + arrLength + " took " + (endTime - startTime) / 1000 + " sec");
    return arr.getLast();
}

And calculating of big sum:


public String bigSum(String s1, String s2) {
    int lengthS1 = s1.length();
    int lengthS2 = s2.length();

    int diff = Math.abs(lengthS1 - lengthS2);

    String prefix = repeatString("0", diff);


    if (lengthS1 < lengthS2) {
        s1 = prefix + s1;
    } else if (lengthS2 < lengthS1) {
        s2 = prefix + s2;
    }

    StringBuilder out = new StringBuilder();
    int buf = 0;
    for (int i = s1.length() - 1; i >= 0; i--) {
        int n1 = Integer.parseInt(String.valueOf(s1.charAt(i)));
        int n2 = Integer.parseInt(String.valueOf(s2.charAt(i)));

        int sum = buf + n1 + n2;
        buf = 0;
        if (sum >= 10) {
            sum = sum % 10;
            buf++;
        } else {
            buf = 0;
        }

        if (buf > 0 && i == 0) {
            out.insert(0, "1").insert(1, sum);
        } else {
            out.insert(0, sum);
        }
    }


    return out.toString();
}

I hope this article could inspire the people to start loving a math and numbers 🙂

Enjoy my blog!

MobiCastle — is it the breakthrough in the mobile applications for travels?

MobiCastle — is it the breakthrough in the mobile applications for travels?

During the last few years, the tones of the mobile applications for travelling have appeared on the mobile market. Their goals were to simplify the ways of travelling, find the best places to stay and to eat. But have they asked the users what they are really interested in? Do the apps really work for the easy travels?

Let’s review a simple example of 1–2 days of the journey.

A person is an average tourist on a car, with a low-average budget for the journey. It means that this person is not interested in the expensive hotels, restaurants, VIP tours, etc. The person wants to find the next things:

1. Something that is possible to get for free during the trip. Probably the discounts or special offers.

2. The place where to leave a car. Not the most expensive parking places, but something, that is recommended by the people as a secure and cheap place.

3. The information about the museums as normally it’s not expensive and quite often it has different discounts.

4. The place where it is possible to take great photos with no limits, probably even having a photo session.

5. Short description about places with an audio-guide. This is very important if you want to walk, to watch, to take the photos with free hands.

MobiCastle — is the first travel service that includes the best castles of Europe. Why the castles? Every year hundreds of millions of tourists are visiting the castles of Europe. These are the perfect places for taking great pictures and active time spending.

But is it only about the castles? No, of course. MobiCastle has a very useful information about the ticket prices, parking, possible offers or discounts, also about free presentations and the most cheaper Booking.com offers in the area.

There are Android and iOS applications.

 

 More information and localized applications could be found onhttps://www.mobicastle.com/

Download the MobiCastle applications and start to build your journeys today!

What people are telling about MobiCastle:

It is easy to travel when there is valuable information! Recommended!

This app is absolutely perfect. Even embedded advertising accommodation is very convenient and useful. For, my person, I will be happy if he stayed and will be regularly maintained and updated.

Stone. Finally, a proper application to the Czech castles. Only in the future, please add and lesser-known ruins of castles and fortresses are missing, but otherwise really cool 🙂

Very cool and useful application.

Enjoy the travels with MobiCastle!

This is why the engineers should learn the algorithms

This is why the engineers should learn the algorithms

In this article, I will try to explain why it’s really important to learn the basics algorithms. The examples will be based on the sorting algorithms.

I suppose that everybody heard about the definition “Big-O”. If you an engineer and you still didn’t hear about it – do not hesitate to google for it. You will understand the importance of it.

big-o

Many people have heard about such sorting algorithms as Tree Sort, Bubble Sort. But, are they the best? Are they the most optimised? Let’s have a look at the several examples of the sorting algorithms written in Java. In this battle will participate:

  1. Bubble Sort – O(n^2)
  2. Mergesort – O(n log(n))
  3. Counting Sort – O(n+k)
  4. Default Java Collections sort method (sorting of primitive int array) – based on Mergesort algorithms regarding the Java doc
  5. Default Java Collections sort method (sorting of Integer ArrayList) – based on Mergesort algorithms regarding the Java doc

The input parameter was an array of integer numbers in a range between 0 and 1000. The length of this array is 10 000 000 items.

Below is an example of the unit-test body that was used for measuring the time of CountingSort algorithm. Absolutely the same body was for all the types of algorithms. Of course with the usage of the different algorithms inside 🙂

int size = 10000000;
int randomIntRange = 1000;
System.out.println("Counting Sorting. Size of array: " + size);

CountingSort sortAlgorithms = new CountingSort();
//MergeSorting sortAlgorithms = new MergeSorting();
//BubbleSorting sortAlgorithms = new BubbleSorting();

//Collecting the integer array
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
 arr[i] = new Random().nextInt(randomIntRange);
}

//Measuring the time
long startTime = System.currentTimeMillis();
sortAlgorithms.sort(arr);
long endTime = System.currentTimeMillis();
System.out.println("Time execution: " + (endTime - startTime) + " ms");

The results were the next:

  1. Mergesort. Size of array: 10000000
    Time execution: 1261 ms
  2. Counting Sorting. Size of array: 10000000
    Time execution: 94 ms
  3. Bubble Sorting. Size of array: 10000000
    Time execution: endless. Ater waiting of 15 minutes I forgot what do I wait for.

So, as you can see the results are very different.

But then I decided to try to measure a Java implementation of sorting. In the Java doc I found that it is based on Mergesort.
So, I made two tests.
In the 1st test, we collected 10 000 000 integer items into the primitive array of integer, then convert it to the ArrayList and then measure the sorting.
In the 2nd test, we collected 10 000 000 integer items into ArrayList directly.
In both cases we used the Java Collections comparator.

The results were next:

  1. Array Java Collections Sorting. Size of array: 10000000
    Time execution: 3973 ms
  2. Java Collections Sorting. Size of array: 10000000
    Time execution: 1715 ms

As you can see the conversion of the array into List also took the time.
But, even so, Counting Sort algorithm is  1715 / 94 ~ 18 times faster.

Now the question – does it make sense to think about the algorithms? The question is open for everybody.

Leave the comments and the feedback!

Happy coding!

Turn Your iPad and iPhone into powerful Python IDE

Turn Your iPad and iPhone into powerful Python IDE

Few days ago I decided try to use iPad for development in Python. I thought it could be funny but I didn’t believe that I will like it. After investigation of all possibilities I opened for myself powerful combination of the tools that allows to perform full process of development, refactoring and scripts execution on iPad. And even more – everything can connected to GitHub.

Let’s review it step by step.

First of all what You need – iPad or iPhone. Of course iPad is much better for development and the reason is obvious.

The second what You need is to download / buy 2 awesome applications and 1 script:

  • Pythonista 3 – this application has support of Python 2 and 3. Pretty cool IDE for development and running Python scripts (Price – 9.99$)
  • Working Copy – the best GitHub client for iOS devices that I saw (10 days of trial period. Then You need to buy full version to be able to push the changes. But it also has amazing code editor. So, very useful application. The price of full version is 10.99$)You
  • Appex script – that allows migrate the code from Working Copy to Pythonista.

That’s all what You really need. So, just 21$ and You have full IDE on Your iOS device. And You don’t need anymore to take Your laptop with You if You want to travel and work.

Of course that development on MacBook is more comfortable. But if You want to perform some small code changes and push it – this is the best combination.

Details of the usage

  1. Open working copy and connect Your GitHub account. Then clone the repository that You need to work with.
  2. Copy python script from Appex github repo (link above) to clipboard or some buffer or text file or email or Skype whatever.
  3. Open Working Copy, go to Your project and click on right top “Share” icon and click “Run Pythonista Script”. If it’s not visible – activate in this share menu.
  4. Click on “Scratchpad”icon. Paste into editor and run this script. After this action You can open Pythonista app and You will find new available repository called “from Working Copy”.
  5. Open this repo and You will find Your code.
  6. Open file You want to modify. Modify something, click on icon “Instrument” in top right corner and click on “Share”. After this choose “Save in Working Copy”
  7. In the opened dialog click “Save As..”. Do not modify the name. You will see the message on blue popup that “..file in use.. Tap here to overwrite Your file”. Tap on this blue popup, in the new dialog click on “Just Save” (in means that You want to commit the changes later) – and voila, Your file is ready to be committed.

Now go back to Working Copy and commit and push Your changes.

Also I would recommend to download / buy application iZip Pro

This app will allow You to extract and modify different zip files in case You need this.

If You have any questions  – feel free to ask me. Share this article and thank You for reading my blog!