-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPrime.html
56 lines (40 loc) · 1.33 KB
/
TestPrime.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
<!DOCTYPE html>
<html>
<body>
<p>Enter the number and click the button to test, if the number is prime... or not.</p>
<input id="input" type=text value="" />
<button onclick="IsItPrime()">Is this prime?</button>
<p id="demo">Is your number really a prime number?</p>
<script>
function IsItPrime()
{
var x = document.getElementById("input").value;
var p;
var i;
if (x == 0) {
document.getElementById("demo").innerHTML = "<i>Not defined for 0. Try some other value.</i>";
return;
}
if (x == 1) {
document.getElementById("demo").innerHTML = "<i>Number 1 is measuring unit, so it is not considered as prime number.</i>";
return;
}
if (x == 2) {
document.getElementById("demo").innerHTML = "<b>Yes, number 2 is a prime!<br /><img src='http://www.marymarcusfiction.com/wp-content/uploads/2015/09/yay-54383329058.jpeg'></b>";
return;
}
for (var i = 2; i < x; i++) {
if (x % i == 0) {
document.getElementById("demo").innerHTML = "<font color='red'>" + x + " is not a prime! Divisible by " + i + ".</font>";
break;
}
if (x % i != 0)
{
document.getElementById("demo").innerHTML ="<b>Yes, " + x + " is a prime!</b><br /><img src='http://www.marymarcusfiction.com/wp-content/uploads/2015/09/yay-54383329058.jpeg'>";
}
}
}
</script>
<!-- Known bugs: Zero, negative numbers and number with 15+ places are not going to be recognised. -->
</body>
</html>