Skip to content

Instantly share code, notes, and snippets.

@p4prawin
Last active December 18, 2015 04:49
Show Gist options
  • Save p4prawin/5728566 to your computer and use it in GitHub Desktop.
Save p4prawin/5728566 to your computer and use it in GitHub Desktop.
simple javacript function to find Prime Numbers between 0 to given number
/*
author : Pravin Mane.
sr. digital technologist @ExperienceCommerce Pvt. Ltd.
Live Demo:
http://jsfiddle.net/p4prawin/YS8M5/10/
*/
function getPrimeNumbers(tillVal) {
var findTill = tillVal;
var primeNoArray = [2, 3, 5, 7]; //prime nos between 1-10
var flag = 1;
for (var i = 7; i <= findTill; i++) {
flag = 1;
for (var k = 0; k < primeNoArray.length; k++) {
if (i % primeNoArray[k] === 0) {
flag = 0;
break;
}
}
if (flag == 1) {
primeNoArray.push(" " + i);
}
}
return primeNoArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment