Created
November 13, 2010 13:19
-
-
Save think49/675313 to your computer and use it in GitHub Desktop.
isPrime.js : 素数を判定する / get-primes.js : 素数リストを出力する
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
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コードの整理に LabelledStatement を使いました。
コメントに JSDoc を使いました。
素数判定に下記の定理を使いました。
TagIndexで回答しました。