JSFiddle - React, Tailwind, and code Playground
by ktstowell
HTML
<h1><a href="http://projecteuler.net/problem=3">Problem:</a></h1>
<p>The prime factors of 13195 are 5, 7, 13 and 29.</p>
<p>What is the largest prime factor of the number 600851475143 ?</p>
<hr />
<div id="prime"></div>
JavaScript
(function() {
var //cap = 600851475143,
cap = 131950,
chunk = 1000,
prime_cont = document.getElementById('prime'),
i = cap,
j;
// First get the block size to process. Handling the
// Second, find each prime number, top down,
// lower than the cap.
function main() {
console.log(i)
var count = chunk;
while(count-- && i > 0) {
if(isPrime(i)) {
if(isFactor(i)) {
prime_cont.textContent = 'The largest Prime Factor is: ' + i;
i=0;
} else {
i--;
}
} else {
i--;
}
}
if(i>0) {
setTimeout(main, 1);
}
}
function isPrime(n) {
var ret;
for(j=n; j>0; j--) {
if(n % j === 0 && j !== n && j !== 1) {
ret = false;
break;
} else {
ret = true;
}
}
return ret;
}
function isFactor(n) {
if(cap % n === 0) {
return true;
}
}
})(document);