« Back
in javascript node challenges command-line algorythms read.

Tuenti Contest 2015.

Every year since 2011, tuenti hosts a programming contest offering some nice prices and the posibility of visiting their head quarters at Madrid (personally, I think the challenge is actually a job interview).

In the 2015th edition, I decided to take part just for fun and soonly jumped to challenge 2: they asked you to made a command line program to determine which is the amount of what they call "almost primes" between a range of numbers provided in an input file.

It turned out that in mathematics, these are actually called semi prime numbers, being these numbers that have exactly two prime factors.

Hands on work, I made my first implementation of the algorythm:

function semiPrime(n) {  
    var aux = 0;
    for (var i = 2; i <= n; i++) {
        while (n % i  0) {
            if (aux  2) {
                return false;
            }
            aux++;
            n /= i;
        }
    }
    return aux == 2;
}

Looks familiar? Yes, I decided to use JavaScript, my favourite programming language, to solve the challenges. This algorythm could be optimised using the number theory.

Using Node.js and some libraries to work with files, I finally made it. The result is a nice command line program that works smoothly and even shows a beautiful progress bar for the impatient people.

calculating... 1/6 [/////             ] 29%

I hope you enjoyed the post as much as I did and see you at the next programming contest!

comments powered by Disqus