-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathisitprime.html
190 lines (175 loc) · 5.98 KB
/
isitprime.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<!--This is a simple but fast prime number checker/factorizator. It's based on a simple "self-building" algorithm and is able to find all prime numbers from 1-1M in about 100ms on a regular machine. It was inspired by this prime number calculator: http://www.math.com/students/calculators/source/prime-number.htm. Unfortunately, that site is horribly slow at checking large primes, whereas my algorithm is lightning fast.-->
<html>
<head>
<title>Is it prime?</title>
<link rel="icon" href="pelement.png">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,700" rel="stylesheet" type="text/css">
<style>
body{
background-image:url("images/stripes.png");
margin:0;
font-family: Roboto,Arial,sans-serif;
font-weight: 700;
}
#primegen{
display: block;
position: fixed;
width: 100%;
height: 100%;
font-size: 3vw;
color: #069;
text-align: center;
line-height: 100vh;
text-shadow: 0.3vw 0.3vw 0.5vw rgba(0,0,0,0.1);
}
#primepanel{
display: none;
position: fixed;
width: 60%;
height: 30vw;
left: 20%;
top: 50%;
margin-top: -15vw;
background-color: white;
border-radius: 1vw;
box-shadow: 0.3vw 0.3vw 1vw rgba(0,0,50,0.3);
overflow:hidden;
}
input{
position: absolute;
width: 90%;
left: 5%;
height: 5vw;
border: none;
text-align: center;
font-size: 4vw;
font-family: Trebuchet MS;
top: 4vw;
border-bottom: 0.2vw solid #999;
outline: none;
}
button{
position: absolute;
width: 20vw;
height: 3vw;
left: 20vw;
background-color:#ddd;
font-size: 1.7vw;
font-family: inherit;
top: 11.5vw;
border: none;
outline: none;
cursor: pointer;
}
button:hover{
background-color:#ccc;
}
#output{
position: absolute;
width: 54vw;
height: 9vw;
left: 2vw;
top: 17vw;
padding: 1vw;
background-color: #eee;
text-align: center;
}
</style>
<script>
var primes=[2];
var factors;
function init(){
setTimeout(generatePrimes,100); //load the background
}
function generatePrimes(){
var isPrime=true;
for (var i=3;i<1000000;i++){
isPrime=true;
for (var a=0;primes[a]*primes[a]<=i;a++){
if (i%primes[a]==0){
isPrime=false;
break;
}
}
if (isPrime){
primes.push(i);
}
}
document.getElementById("primepanel").style.display="block";
}
function press(){
if (event.keyCode==13){isitprime();}
}
function isitprime(){
factors="";
if (document.getElementById("input").value=="1"){
document.getElementById("output").innerHTML="1 is not prime \<a href=\"http://www.askamathematician.com/2010/01/q-why-is-the-number-1-not-considered-a-prime-number\" target=\"_blank\"\>by definition!\<\/a\>";
}else if (parseFloat(document.getElementById("input").value)<1){
document.getElementById("output").innerHTML=""+document.getElementById("input").value+" is not included in prime definition.";
}else if (prime(parseFloat(document.getElementById("input").value))){
if (parseFloat(document.getElementById("input").value)>1e12){
document.getElementById("output").innerHTML=""+document.getElementById("input").value+" is likely prime!<br><br>WARNING: input is very large. There may be prime factors that the script missed.";
}else{
document.getElementById("output").innerHTML=""+document.getElementById("input").value+" is prime!";
}
}else{
document.getElementById("output").innerHTML=""+document.getElementById("input").value+" is not prime.<br><br>"+document.getElementById("input").value+"'s prime factors are:<br>"+factors;
}
}
function prime(input){
var foundprime=true;
var allfactors=[];
var input2=input;
var alertUser=false,alertBig=false;
for (var i=0;primes[i]<=Math.sqrt(input);i++){
if (input2%primes[i]==0){
input2/=primes[i];
foundprime=false;
allfactors.push(primes[i]);
i--;
}
if (input2==1){break;}
}
if (!foundprime&&input2>1){
input=input2;
var isPrime=true;
for (var i=0;primes[i]<Math.sqrt(input);i++){
if (input2%primes[i]==0){
isPrime=false;
break;
}
}
if (isPrime){
if (input2<1e12){
allfactors.push(input2);
}else{
alertBig=true;
}
}else{
alertUser=true;
}
}
if (!foundprime){
for (var a=0;a<allfactors.length-1;a++){
factors+=""+allfactors[a]+", ";
}
factors+="and "+allfactors[allfactors.length-1]+".";
if (alertUser){factors+="<br><br>WARNING: input is very large and all prime factors were not found.";}
else if (alertBig){
factors+="<br><br>WARNING: remainder ("+input2+") is very large and the script could not establish if it's prime.";
}
}
return foundprime;
}
</script>
</head>
<body onload=init()>
<div id="primegen">Generating prime database...</div>
<div id="primepanel">
<input id="input" placeholder="input number" onkeydown=press()></input>
<button onclick=isitprime()>is it prime?</button>
<div id="output">info will appear here.<br><br><br><br><br>Written by reddit user /u/PicturElements.</div>
</div>
</body>
</html>