Tuesday, May 26, 2015

Solutions to a small kids puzzle

How to solve the following kid puzzle by using only digits from 1 to 9 exactly one time :

With the following code, I found 20 solutions if we take into account operators priority, 119 without.
The solutions are :
List(3, 2, 1, 5, 4, 7, 8, 9, 6)
List(3, 2, 1, 5, 4, 7, 9, 8, 6)
List(5, 2, 1, 3, 4, 7, 8, 9, 6)
List(5, 2, 1, 3, 4, 7, 9, 8, 6)
List(5, 3, 1, 7, 2, 6, 8, 9, 4)
List(5, 3, 1, 7, 2, 6, 9, 8, 4)
List(5, 4, 1, 9, 2, 7, 3, 8, 6)
List(5, 4, 1, 9, 2, 7, 8, 3, 6)
List(5, 9, 3, 6, 2, 1, 7, 8, 4)
List(5, 9, 3, 6, 2, 1, 8, 7, 4)
List(6, 3, 1, 9, 2, 5, 7, 8, 4)
List(6, 3, 1, 9, 2, 5, 8, 7, 4)
List(6, 9, 3, 5, 2, 1, 7, 8, 4)
List(6, 9, 3, 5, 2, 1, 8, 7, 4)
List(7, 3, 1, 5, 2, 6, 8, 9, 4)
List(7, 3, 1, 5, 2, 6, 9, 8, 4)
List(9, 3, 1, 6, 2, 5, 7, 8, 4)
List(9, 3, 1, 6, 2, 5, 8, 7, 4)
List(9, 4, 1, 5, 2, 7, 3, 8, 6)
List(9, 4, 1, 5, 2, 7, 8, 3, 6)

Friday, January 30, 2015

Quick hack to automatically ip geoloc people who tried to get access to your server (using SSH)

The following script gives this kind of output (for only 3 days of logs...) :
# ./sshhack-iploc /var/log/auth.log*
-----------------------------
-- SSH failed distinct attempts by country
Hong Kong -> 42
China -> 38
France -> 10
United States -> 6
Germany -> 2
Venezuela -> 1
United Kingdom -> 1
India -> 1
Republic of Korea -> 1
Brazil -> 1
Netherlands -> 1
-----------------------------
-- SSH top 10 of tested users
User 'root' -> 78318
User 'admin' -> 158
User 'oracle' -> 21
User 'postgres' -> 18
User 'ts' -> 14
User 'vnc' -> 13
User 'test' -> 12
User 'bin' -> 12
User 'git' -> 11
User 'teamspeak3' -> 10
-----------------------------
-- General Information
Processed time range : 92 hours (~3 days)
Total number of auth failures 78757 (~856 by hour)
Distinct tested logins 116

Tuesday, January 27, 2015

serial versus parallel isPrime function, parallelization is not always interesting...

Parallel implementation of isPrime function based on promise and futures generates sometime too much overhead in comparison to the simple one. In fact it is really faster with prime number... but with not prime numbers it won't be always the case. So how to decide which implementation is best for a given number, ... no idea yet Full code is available here.
$ sbt 'test-only fr.janalyse.primes.IsPrimesTest'

[info] IsPrimesTest:
[info] - isPrimePara tests
[info] - isPrime mono versus parallel tests
[info]   + duration for 10000 : 1050ms serial processing, highest prime 104729 
[info]   + duration for 10000 : 5451ms parallel processing, highest prime 104729 (ForkJoinPool) 
[info]   + duration for 10000 : 13384ms parallel processing, highest prime 104729 (CachedThreadPool) 
[info] - very big prime test
[info]   + duration for 17436413019234331 : 22063ms sequential isPrime 
[info]   + duration for 17436413019234331 : 5839ms parallel isPrime (ForkJoinPool) 
[info]   + duration for 17436413019234331 : 5848ms parallel isPrime (CachedThreadPool) 

Thursday, January 22, 2015

Primes computation : From home made akka actors based solution to akka-stream

Available on github, Primes is a library dedicated to primes number computation, it is fully generic and can work with various numeric types (Int, Long, BigInt for example). This library generates CheckedValue instances which contains the just tested numeric value, it tells you if it is prime number or not, gives you its position and the number of digit it contains. The fact that it computes the prime position, or not prime position, implies that the computation starts from the beginning or from a previous persisted state (previous highest prime CheckedValue and not prime CheckedValue).

In January 2014,

I spent some times writing an actor based primes computation algorithm, it was not a very easy task because it implies to manage myself many things :
  • back pressure management,
  • parallelism,
  • reordering results.

The full source code of this old implementation is available here.

Now, one year later,

it was time to look at something else, back pressure, results ordering, parallelism, ... are too common patterns with actors that we shouldn't have to deal with them directly, it is up to the framework to provide us with the right solution.

Akka-stream brings us a great abstraction, for them, and the result becomes quite easier to read and to understand. back-pressure is automatically taken into account, parallel processing and ordering are just done through mapAsync call instead of just a map call.

The full source code of this new implementation is available here.

Now let's give place to the code, and just compare NEW versus OLD code :

NEW : Akka-stream actors primes computation

Check this to see how to use it, for example directly from the scala console (sbt console).

OLD : Akka actors primes computation

From performance point of view

Some "naive" raw performance results ("sbt test" to try yourself) :