Created
May 15, 2016 04:40
-
-
Save skilbjo/bf66a9ce48e2271c31e35a95e044bf36 to your computer and use it in GitHub Desktop.
Prime number generator from scratch! :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var range = [490,510], | |
start = range[0], | |
end = range[1] | |
; | |
var check_prime = function(num, j, ans){ | |
if(j === 1) return ans; | |
if(num % j === 0) { | |
ans.push(false); | |
} | |
ans.push(true); | |
return check_prime(num, j-1, ans); | |
}; | |
var primes = function(start,end){ | |
var result = {}; | |
if(!start || !end || start*1 < 0 || end*1 < 0){ | |
return 'Bad inputs! Provide an an integer for start and end positions.'; | |
} | |
for(var i = start; i <= end; i++){ | |
var tmp = []; | |
for(var j = 3; j <= (i - 1); j++){ | |
tmp = check_prime(i,j,[]); | |
} | |
if( tmp.every(function(el){ return el === true; }) ){ | |
result[i] = 'prime'; | |
} | |
} | |
return result; | |
}; | |
console.log( | |
primes(start, end) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment