Skip to content

Instantly share code, notes, and snippets.

@think49
Created November 13, 2010 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save think49/675313 to your computer and use it in GitHub Desktop.
Save think49/675313 to your computer and use it in GitHub Desktop.
isPrime.js : 素数を判定する / get-primes.js : 素数リストを出力する
/**
* get-primes.js
* Get a prime list.
*
* @version 1.0.1
* @author think49
* @url https://gist.github.com/675313
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
*/
function getPrimes (length) {
var i, number, primes, prime, perfectPower, primeLength;
/** Check a natural number */
if (length < 1 || length % 1 !== 0) {
return [];
}
primes = [2];
primeLength = primes.length;
number = 3;
/** Eratosthenes' sieve */
while (length > primeLength) {
i = 0;
do {
prime = primes[i++];
perfectPower = prime * prime;
} while (number > perfectPower && number % prime !== 0);
if (number < perfectPower) {
primeLength = primes.push(number);
}
number += 2;
}
return primes;
}
@think49
Copy link
Author

think49 commented Nov 13, 2010

コードの整理に LabelledStatement を使いました。

コメントに JSDoc を使いました。

素数判定に下記の定理を使いました。


TagIndexで回答しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment