var TMan = new N6LTimerMan(); //タイマーマネージャー TMan.add(); window.onload=function(){ readCSV('./primelist100000.txt', 'analyzeCSV', 'readedCSV'); } var PRMTABLE = []; var READED = false; function readedCSV(res) { if(res.length < 1) return; var i; for(i = 0; i < res.length; i++){ PRMTABLE.push(parseInt(res[i])); } READED = true; } var PRM = [2, 3, 5]; var ONELOOPNUM = 1000; var TMP = 7; var NUM; var MOD = true; var USEMOD = 100; function checkprime(num, max){ if(max < PRM[PRM.length - 1]) return num; var i; var mod = num % 6; if((mod != 1)&&(mod != 5)) return -1; for(i = 2; i < PRM.length; i++){ if(max < PRM[i]) return num; if(num % PRM[i] == 0) return -1; } return num; } function enumprime(num){ PRM = [2, 3, 5]; TMP = 7; function Loop(id){ var cnt; for(cnt = 0; cnt < ONELOOPNUM; cnt++){ if(num < TMP){ if(checkprime(NUM, num) == NUM) { TXT_ANS.value = "Prime Number" } else{ TXT_ANS.value = "Not Prime Number" } return; } var maxtmp = Math.ceil(Math.sqrt(TMP)); if(checkprime(TMP, maxtmp) == TMP){ PRM.push(TMP); } TMP += 2; } TMan.timer[id].setalerm(function() { Loop(id); }, 50); //メインループ再セット } Loop(0); } //素数列挙あり function oncheckprime(){ TXT_ANS.value = "Calculating・・・" NUM = parseInt(TXT_NUM.value); var max = Math.ceil(Math.sqrt(NUM)); if((NUM == 1)||(NUM == 2)){ TXT_ANS.value = "Prime Number" return; } if((NUM <= 0)||(NUM % 2 == 0)){ TXT_ANS.value = "Not Prime Number" return; } var mod = NUM % 6; if((NUM != 3)&&((mod != 1)&&(mod != 5))){ TXT_ANS.value = "Not Prime Number" return; } enumprime(max); } //素数列挙なし function oncheckprimeeasy(){ TXT_ANS.value = "Calculating・・・" NUM = parseInt(TXT_NUM.value); MAX = Math.ceil(Math.sqrt(NUM)); if((NUM == 1)||(NUM == 2)||(NUM == 3)){ TXT_ANS.value = "Prime Number" return; } if((NUM <= 0)||(NUM % 2 == 0)){ TXT_ANS.value = "Not Prime Number" return; } TMP = 5; var mod = NUM % 6; if((NUM != 3)&&((mod != 1)&&(mod != 5))){ TXT_ANS.value = "Not Prime Number" return; } if(MAX <= TMP){ TXT_ANS.value = "Prime Number" return; } function Loop2(id){ var cnt; for(cnt = 0; cnt < ONELOOPNUM; cnt++){ // if((MOD)||(USEMOD <= TMP)){ mod = TMP % 6; if((mod != 1)&&(mod != 5)){ TMP += 2; continue; } // } if(NUM % TMP == 0){ TXT_ANS.value = "Not Prime Number" return; } TMP += 2; if(MAX <= TMP){ TXT_ANS.value = "Prime Number" return; } } TMan.timer[id].setalerm(function() { Loop2(id); }, 50); //メインループ再セット } Loop2(0); } function onEratosthenes(){ TXT_ANS.value = "Calculating・・・" NUM = parseInt(TXT_NUM.value); MAX = Math.ceil(Math.sqrt(NUM)); if(200000 <= NUM){ TXT_ANS.value = "Do not number range!!!" return; } if((NUM == 1)||(NUM == 2)||(NUM == 3)){ TXT_ANS.value = "Prime Number" return; } if((NUM <= 0)||(NUM % 2 == 0)){ TXT_ANS.value = "Not Prime Number" return; } var mod = NUM % 6; if((NUM != 3)&&((mod != 1)&&(mod != 5))){ TXT_ANS.value = "Not Prime Number" return; } var i; PRM = [2]; for(i = 3; i <= NUM;){ PRM.push(i); i += 2; } TMP = 1; function Loop3(id){ var cnt; for(cnt = 0; cnt < ONELOOPPROC; cnt++){ if(MAX <= PRM[TMP]){ if(PRM[PRM.length - 1] == NUM){ TXT_ANS.value = "Prime Number" return; } else{ TXT_ANS.value = "Not Prime Number" return; } TMP++; } TXT_ANS.value = "Calculating・・・" + PRM[TMP]; var tmp; var n; for(tmp = TMP; tmp < PRM.length; tmp++){ var tprm = PRM.filter(n => ((n === PRM[tmp])||(n % PRM[tmp]) !== 0)); PRM = []; PRM = [...tprm]; } } TMan.timer[id].setalerm(function() { Loop3(id); }, 50); //メインループ再セット } Loop3(0); } function onTable(){ if(!READED) { TXT_ANS.value = "Do not read yet!!!" return; } TXT_ANS.value = "Calculating・・・" NUM = parseInt(TXT_NUM.value); MAX = Math.ceil(Math.sqrt(NUM)); if(100000 <= NUM){ TXT_ANS.value = "Do not number range!!!" return; } if((NUM == 1)||(NUM == 2)||(NUM == 3)){ TXT_ANS.value = "Prime Number" return; } if((NUM <= 0)||(NUM % 2 == 0)){ TXT_ANS.value = "Not Prime Number" return; } var mod = NUM % 6; if((NUM != 3)&&((mod != 1)&&(mod != 5))){ TXT_ANS.value = "Not Prime Number" return; } TMP = 0; function Loop4(id){ TXT_ANS.value = "Calculating・・・" + PRMTABLE[TMP]; var i; for(i = 0; i < ONELOOPNUM; i++){ if(NUM < PRMTABLE[TMP + i]){ TXT_ANS.value = "Not Prime Number" return; } if(PRMTABLE[TMP + i] == NUM){ TXT_ANS.value = "Prime Number" return; } } TMP = TMP + i + 1; TMan.timer[id].setalerm(function() { Loop4(id); }, 50); //メインループ再セット } Loop4(0); } function onCHK(){ MOD = CB_CHK.checked; }
var TMan = new N6LTimerMan(); //タイマーマネージャー TMan.add(); TMan.add(); ...省略... function onN6LIsPrime(){ TXT_ANS_T.value = "Calculating・・・" NUM = parseInt(TXT_NUM_T.value); var ret = N6LIsPrime(0, NUM); function Loop5(id){ if(ret == NUM){ TXT_ANS_T.value = "Prime Number" return; } else if(ret < 0){ TXT_ANS_T.value = "Not Prime Number" return; } ret = N6LISPRMRET; TMan.timer[id].setalerm(function() { Loop5(id); }, 75); //メインループ再セット } Loop5(1); } }
function IsPrime(num){ if((num == 1)||(num == 2)||(num == 3)) return num; ///////////////////#1 var mod = num % 6; if((mod != 1)&&(mod != 5)){ return -1; } /////////////////// var max = Math.floor(Math.sqrt(num)); var tmp; for(tmp = 5; max < tmp;){ ///////////////////#2 mod = tmp % 6; if((mod != 1)&&(mod != 5)){ tmp += 2; continue; } /////////////////// if(num % tmp == 0){ return -1; } tmp += 2; } return num; }
function IsPrimeEratosthenes(num){ if((num == 1)||(num == 2)||(num == 3)) return num; var mod = num % 6; if((mod != 1)&&(mod != 5)){ return -1; } var max = Math.floor(Math.sqrt(num)); var i; var PRM = [2]; for(i = 3; i <= num;){ PRM.push(i); i += 2; } var TMP; for(TMP = 1; PRM[TMP] <= max; TMP++){ if(max < PRM[TMP]){ if(PRM[PRM.length - 1] == num){ return num; } else{ return -1; } var tmp; var n; for(tmp = TMP; tmp < PRM.length; tmp++){ var tprm = PRM.filter(n => ((n === PRM[tmp])||(n % PRM[tmp]) !== 0)); PRM = []; PRM = [...tprm]; } } return num; }
function N6LIsPrimeMNRD(id, num){ N6LISPRMRET = 0; N6LNUM = num; N6LMAX = Math.ceil(Math.sqrt(NUM)); if((N6LNUM == 1)||(N6LNUM == 2)||(N6LNUM == 3)){ N6LISPRMRET = N6LNUM; return N6LNUM; } if((N6LNUM <= 0)||(N6LNUM % 2 == 0)){ N6LISPRMRET = -1; return -1; } N6LTMP0 = Math.ceil(N6LMAX / 2) * 2 + 1; N6LTMP1 = 5; var mod = N6LNUM % 6; if((N6LNUM != 3)&&((mod != 1)&&(mod != 5))){ N6LISPRMRET = -1; return -1; } if(N6LTMP0 <= N6LTMP1){ N6LISPRMRET = N6LNUM; return N6LNUM; } function LoopMNRD(id){ var cnt; for(cnt = 0; cnt < N6LONELOOPNUM; cnt++){ mod = N6LNUM % N6LTMP0; if(mod != 0){ N6LTMP0 -= 2; } else{ N6LISPRMRET = -1; return -1; } mod = N6LNUM % N6LTMP1; if(mod != 0){ N6LTMP1 += 2; } else{ N6LISPRMRET = -1; return -1; } if(N6LTMP0 <= N6LTMP1){ N6LISPRMRET = N6LNUM; return N6LNUM; } } TMan.timer[id].setalerm(function() { LoopMNRD(id); }, 50); //メインループ再セット N6LISPRMRET = 0; return 0; } N6LISPRMRET = LoopMNRD(id); return N6LISPRMRET; } function onN6LIsPrimeMNRD(){ TXT_ANS_MNRD.value = "Calculating・・・" NUM = parseInt(TXT_NUM_MNRD.value); var ret = N6LIsPrimeMNRD(0, NUM); function Loop6(id){ if(ret == NUM){ TXT_ANS_MNRD.value = "Prime Number" return; } else if(ret < 0){ TXT_ANS_MNRD.value = "Not Prime Number" return; } ret = N6LISPRMRET; TMan.timer[id].setalerm(function() { Loop6(id); }, 75); //メインループ再セット } Loop6(1); }