Skip to content

Instantly share code, notes, and snippets.

@veganben
Created June 11, 2019 15:54
Show Gist options
  • Save veganben/abc5598a17a80a40dfd4fc89cf6e9e45 to your computer and use it in GitHub Desktop.
Save veganben/abc5598a17a80a40dfd4fc89cf6e9e45 to your computer and use it in GitHub Desktop.
In a throw-away comment on The Infinite Monkey Cage, Prof Brian Cox said that the year 2021 has prime factors 43 and 47. This got me thinking …
<!doctype html>
<html>
<head>
<title>Prof Brian Cox's throw away line that 2021 has prime factors 43 and 47 got me thinking... </title>
<style>
html {
background-color: #deeded;
}
h1, h2, p, input, label {
color: #234567;
font-family: Tahoma;
font-weight: 100;
font-size: 1rem
}
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.2rem;
}
input {
border-radius: 1px;
border: 1px;
margin-left: 8rem;
padding: 0.5rem;
margin-top: 2rem;
height: 2rem;
width: 18rem;
}
</style>
<script>
var getSqrt = function(top) {
return Math.floor(Math.sqrt(year));
};
var isPrime = function(n) {
var squirt = Math.ceil(Math.sqrt(n));
for (var j = 2; j < squirt; j++) {
if (n % j === 0) {
return false;
}
}
return true;
};
var showPrimesForANumber = function() {
var yearEl = document.getElementById("year");
var year = yearEl.value;
var outputEl = document.getElementById("output");
var output = "";
var message = "";
outputEl.innerHTML = "calculating ...";
if(!isNaN(year) && year % 1 === 0) {
if(Math.sqrt(year) % 1 === 0) {
message = "The year " + year + " is itself a square (of " + Math.sqrt(year) + ")";
} else {
for(var i = 1; i < Math.ceil(Math.sqrt(year)); i++) {
if(isPrime(i)) {
var extra = "";
var checkit = year / i;
if(checkit % 1 === 0) {
if(isPrime(checkit)) {
if(i === 1) {
extra = " (" + year + " is itself prime)";
}
output += i + " & " + checkit + extra + "<br>";
}
}
}
}
if(output === "") {
message = "There are no unique prime factors for the year " + year;
} else {
message = "The prime factors for the year " + year + " are :<br>" + output;
}
}
} else {
message = "that's not a year";
}
setTimeout(function() {outputEl.innerHTML = message}, 4);
};
</script>
</head>
<body>
<h1>Find Prime Factors</h1>
<h2>After Professor Brian Cox's throw-away line on The Infinite Monkey Cage podcast that the year 2021 had the prime factors <em>43</em> and <em>47</em>, it got me thinking.... </h2>
<label for="year">Find the prime factors for a given year<br>
<input id="year" name="year" placeholder="year" onblur="showPrimesForANumber()"></input>
</label>
<br>
<p id="output">(hit tab to calculate) </p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment