<!DOCTYPE html>
<HTML>
<HEAD>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
<BODY>
<SCRIPT type="text/javascript">
// The largest integer Java natively supports is 2^53-1, so these
// routines are designed to work for *positive* integers up to that.
// Currently the function check does the idiot proof to see only positive
// integers (not too large) are passed to the other routines.
// trial_divide(N,max) uses trial division to seek the smallest
// prime divisor of N, returns 0 if none found.
function trial_divide(N,max) {
// Trial divides the positive integer N by the primes from 2 to max
// Returns the first prime divisor found, or 0 if none found
// Note: if N < max^2 is a prime, then N will be returned.
if (N%2 == 0) return 2;
if (N%3 == 0) return 3;
// No need to go past the square root of our number
var Stop = Math.min(Math.sqrt(N),max);
// Okay, lets "wheel factor" alternately adding 2 and 4
var di=2;
for(i=5; i<=Stop; i+=di, di=6-di) {
if (N%i == 0) return i;
};
if (N >= max*max) return 0;
return N;
}
// modmult(a,b,N) finds a*b (mod N) where a, b, and N can be
// up to (2^53-1)/2. Might up this to 2^53-1 eventually...
function modadd(a,b,N) {
// When the integers a, b satisfy a+b > 2^53-1, then (a+b)%N is wrong
// so we add this routine to allow us to reach a, b = 2^53-1.
if (a+b > 9007199254740991) {
// Could reduce a and b (mod N) here, but assuming that has already been done
// won't hurt if not... subtract 2^52 from one, 2^52-1 from the other and the
// add it back modulo N (MaxInt+1)
var t = ( (a-4503599627370496) + (b-4503599627370495) )%N;
return ( t + (9007199254740991 % N) );
}
// Usual case: a + b is not too large:
return ( (a+b)%N );
}
function modmult(a,b,N) {
if (a > N) a = a%N;
if (b > N) b = b%N;
if (a*b <= 9007199254740991) {
return ((a*b)%N);
} else {
if (b > a) return modmult(b,a,N);
// Right to left binary multiplication
var t = 0;
var f = a;
while (b > 1) {
if ((b & 1) == 1) t = modadd(t,f,N);
b = Math.floor(b/2);
f = modadd(f,f,N);
};
t = modadd(t,f,N);
return t;
}
}
// modpow(a,exp,N) finds a^exp (mod N) where a, b, and N are
// limited by modmult
function modpow(a,exp,N) {
if (exp == 0) return 1;
// Right to left binary exponentiation
var t = 1;
var f = a;
while (exp > 1) {
if ((exp & 1) == 1) { // if exponent is odd
t = modmult(t,f,N);
}
exp = Math.floor(exp/2);
f = modmult(f,f,N);
};
t = modmult(t,f,N);
return t;
}
// SPRP(N,a) checks if N (odd!) is a strong probable prime base a
// (returns true or false)
function SPRP(N,a) {
var d = N-1; s = 1; // Assumes N is odd!
while ( ((d=d/2) & 1) == 0) s++; // Using d>>1 changed the sign of d!
// Now N-1 = d*2^s with d odd
var b = modpow(a,d,N);
if (b == 1) return true;
if (b+1 == N) return true;
while (s-- > 1) {
b = modmult(b,b,N);
if (b+1 == N) return true;
}
return false;
}
// The idiot proofing, answer returning script
function check(e){
if (e.hasAttribute("disabled")) return;
var start = performance.now();
var TrialLimit = 1300; // Should be bigger, like 10000
var N = document.getElementById("primenumbercheck").value;
var Result = "Unknown error";
var a;
if (N > 9007199254740991) {
Result = "Sorry, this routine will only handle integers below 9007199254740991.";
} else if (N == 1) {
Result = "The number 1 is neither prime or composite (it the multiplicative identity).";
} else if (N < 1) {
Result = "We usually restrict the terms prime and composite to positive integers";
} else if (N != Math.floor(N)) {
Result = "We usually restrict the terms prime and composite to positive integers";
} else {
// Okay, N is of a resonable size, lets trial divide
window.status = "Trial dividing " + N + " to " + TrialLimit + ".";
i = trial_divide(N,TrialLimit);
if (i > 0 && i != N) {
Result = N+" is not a prime! It is "+i+" * "+N/i;
} else if (N < TrialLimit*TrialLimit) {
Result = N+" is a (proven) prime!";
} else if ( SPRP(N,a=2) && SPRP(N,a=3) && SPRP(N,a=5) && SPRP(N,a=7)
&& SPRP(N,a=11) && SPRP(N,a=13) && SPRP(N,a=17)) {
// Some of these tests are unnecessary for small numbers, but for
// small numbers they are quick anyway.
if (N < 341550071728321) {
Result = N + " is a (proven) prime.";
} else if (N == 341550071728321) {
Result = N+" is not a prime! It is 10670053 * 32010157.";
} else {
Result = N + " is probably a prime (it is a sprp bases 2, 3, 5, 7, 11, 13 and 17).";
};
} else {
Result = N+" is (proven) composite (failed sprp test base "+a+").";
};
};
var end = performance.now();
var timeTaken = end - start;
window.status= "Done!"; // here so says done when present alert box
var x = document.getElementsByClassName("primecheck");
x[0].innerHTML = Result;
setTimer(timeTaken);
}
function generate(e){
if (e.hasAttribute("disabled")) return;
var start = performance.now();
var x = document.getElementsByClassName("primelist");
var N = document.getElementById("primenumberlist").value;
var i = 2;
var Result = "2, ";
var currentInt = 3;
var possiblePrime;
if (N == 2)
Result = Result + currentInt + ", ";
else {
while (i < N) {
possiblePrime = false;
if(currentInt == 3) {
Result = Result + currentInt + ", ";
possiblePrime = false;
}
else if(currentInt % 5 == 0 && currentInt > 5) {
possiblePrime = false;
}
else if(currentInt % 6 == 1 || currentInt % 6 == 5)
possiblePrime = true;
if(possiblePrime) {
if(trial_divide(currentInt,Math.ceil(Math.sqrt(currentInt))) == currentInt) {
Result = Result + currentInt + ", ";
i++;
}
}
currentInt = currentInt + 2;
}
}
var end = performance.now();
var timeTaken = end - start;
x[0].innerHTML = Result.substring(0, Result.length - 2);
setTimer(timeTaken);
}
function setTimer(timeTaken) {
var timerElement = document.getElementsByClassName("timeElapsed")[0];
var timerPreText = "Time Elapsed: ";
var duration = convertMS(timeTaken);
timerPreText = timerPreText + duration.day + " days: " + duration.hour + " hours: " + duration.minute + " minutes: " + duration.seconds + " seconds" + ": " +
duration.milliseconds + " milliseconds";
timerElement.innerHTML = timerPreText;
}
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function convertMS( milliseconds ) {
var day, hour, minute, seconds, milliseconds;
seconds = Math.floor(milliseconds / 1000);
milliseconds = milliseconds - (seconds * 1000);
milliseconds = milliseconds.toFixed(2);
minute = Math.floor(seconds / 60);
seconds = seconds % 60;
hour = Math.floor(minute / 60);
minute = minute % 60;
day = Math.floor(hour / 24);
hour = hour % 24;
return {
day: day,
hour: hour,
minute: minute,
seconds: seconds,
milliseconds: milliseconds
};
}
function checkInput(input, buttonId)
{
var name = input.value;
var cansubmit = (name.length > 0);
if (cansubmit) {
document.getElementById(buttonId).removeAttribute("disabled");
} else {
document.getElementById(buttonId).setAttribute("disabled", null);
}
};
</SCRIPT>
<article class="message is-success timerDiv" style="visibility: visible;">
<div class="message-body">
<p class="timeElapsed">Time elapsed will be shown here!</p>
</div>
</article>
<article class="message is-primary">
<div class="message-body">
<strong class="title">Check prime number</strong>
<div class="content" style="margin-top: 10px;">
<div><input id="primenumbercheck" class="input is-rounded" type="text" placeholder="Enter the number you wanna check..." style="max-width: 25%;"
onkeypress="return isNumberKey(event)" onkeyup="checkInput(this, 'primenumbercheckButton')">
<a class="button is-link is-rounded" onclick="check(this)" id="primenumbercheckButton" disabled>Check</a></div>
</div>
</div>
</article>
<div class="container is-fullhd">
<div class="notification primecheck">
Prime check goes here!
</div>
</div>
<article class="message is-primary">
<div class="message-body">
<strong class="title">List prime numbers</strong>
<div class="content" style="margin-top: 10px;">
<div><input id="primenumberlist" class="input is-rounded" type="text" placeholder="Enter the number of prime numbers you wanna generate..." style="max-width:
35%;" onkeypress="return isNumberKey(event)" onkeyup="checkInput(this, 'primenumberlistButton')">
<a class="button is-link is-rounded" onclick="generate(this)" id="primenumberlistButton" disabled>List</a></div>
</div>
</div>
</article>
<div class="container is-fullhd">
<div class="notification primelist">
Prime list goes here!
</div>
</div>
</div>
</BODY>
</HEAD>
</HTML>