Last active
November 9, 2024 20:23
-
-
Save jonahb/6f3bf89d2e6296a994606e63ebff0556 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Is n prime?</title> | |
<style> | |
body { | |
margin-top: 250px; | |
font-family: 'San Francisco', Helvetica, sans-serif; | |
font-size: 64px; | |
text-align: center; | |
} | |
a { | |
font-size: 32px; | |
} | |
a:hover { | |
cursor: pointer; | |
} | |
#yes:hover { | |
color: blue; | |
} | |
#no:hover { | |
color: red; | |
} | |
#n { | |
font-weight: bold; | |
} | |
#factors { | |
margin-top: 48px; | |
font-size: 16px; | |
color: #999; | |
} | |
</style> | |
</head> | |
<body onload="next();"> | |
<script> | |
var n; | |
function next() { | |
n = 100 + Math.ceil(Math.random() * 9900) | |
document.getElementById("n").textContent = n.toString(); | |
} | |
function answer(answer) { | |
var factors = factorize(n); | |
var isPrime = (factors.length === 0); | |
alert((answer === isPrime) ? "RIGHT!" : "WRONG :("); | |
document.getElementById("factors").textContent = isPrime ? | |
n.toString() + " is prime" : | |
"Factors of " + n + " — " + factors.join(", "); | |
next(); | |
} | |
function factorize(n) { | |
var factors = []; | |
var k; | |
for (k = 2; k <= Math.sqrt(n); ++k) { | |
if (n % k === 0) { | |
factors.push(k); | |
factors.push(n / k) | |
} | |
} | |
return factors.sort(function(a, b) { return a - b; }); | |
} | |
</script> | |
Is <span id="n"></span> prime?<br> | |
<a id="yes" onclick="answer(true);">Yes</a> | |
<a id="no" onclick="answer(false);">No</a><br> | |
<div id="factors"></div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<h/1>