Hopefully, this will start off the week for the mathematically inclined.

I signed up for the coursera (www.coursera.org) classes for Startup Engineering out of curiousity and got this interesting Assignment to generate the first 100 prime number using Server Side Javascript as my algorithm implementation scripting language.

Boy! I got me doing some research and found the Sieve (http://en.wikipedia.org/wiki/Sieve*of*Eratosthenes) Method as the quickest mathematical formula to implement for primality detection, hence after submission, i decided to tweak the code a bit more and generate first 1000 prime numbers.

Then I upped my curiousity and attempted to generate the prime numbers between 2 and 3,000,000 :).

Well my system was taking too long processing it that i just had to stop the script and quench my curiousity.

having said that, below is my Nodejs script snippet:

`[code]`

# !/usr/bin/env node

/*
*
Author : Abdulhakim Haliru

http://www.leproghrammeen.com
June, 2013.
This is a Server Side Javascript written for the nodejs engine to generate
the first 1000 prime numbers and write it to a file "prime_number.txt" and write t using the sieve of Eratosthenes method

The method is efficient for any number below 10,000,000.
*
*
*

*
*
*
*
*
*/

var fs = require('fs');

var arrPrime = [];

var getPrime = function(ar,base){

var base = base ;

var arrOdd = ar;

var ps = arrPrime.length ;

var i = 0;

pn = arrOdd.shift();

arrPrime.push(pn);

for(i = 0; i < arrOdd.length; i++)

{

```
if((arrOdd[i] % pn) === 0)
```

{ arrOdd.splice(i,1); } }

if(arrPrime.length == parseInt(base)) { return arrPrime ;

}

return getPrime(arrOdd,base);

};

var primeNumbers = function(base)

{

var arrOdd = [] for( i = 2; i < 20000 ; i++) {

arrOdd.push(i);

}

arr = getPrime(arrOdd,base) ;

return arr;

};

var fmt = function(k){

return k.join(",");

};

var p100 = [];

var base = 1000;

p100 = fmt(primeNumbers(base));

//console.log(p100);

output = p100;

outfile = "prime_numbers.txt";

fs.writeFileSync(outfile,output);

console.log("Script: "+ __filename + " has posted all output to the file " + outfile);

`[/code]`

Have a great week ahead and enjoy coding.... :)

Written : 30th June 2013 by @hakeemhal

Original Post