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 : 素数リストを出力する
/**
* isPrime.js
* @version 1.0
* @author think49
*/
/**
* 数値を素数判定する。
* @function
* @param {Number} num 数値
* @returns {Boolean} 素数なら true を返し、非素数 (小数, 負の数, 合成数など) なら false を返す。
*/
function isPrime (num) {
var a = 2;
if (num === a) {
return true;
}
/** 自然数を判定する */
isNaturalNumber: {
if (num < 1 || num % 1 !== 0) {
return false;
}
}
/** フェルマーテスト (フェルマーの小定理) */
fermatTest: {
if (Math.pow(a, num) % num !== a) {
return false;
}
}
/** エラトステネスのふるい */
eratosthenes: {
do {
if (num % a === 0) {
return false;
}
a++;
} while (num > a);
}
return num === a;
}
@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