<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<div class="code">
<a href="https://projecteuler.net/problem=7">Project Euler</a> Solution
<br/>Problem ID #7 : 10001st prime
<br/>
<br/>By listing the first six prime numbers:
<br/>2, 3, 5, 7, 11, and 13,
<br/>we can see that the 6th prime is 13.
<br/>
<br/>What is the 10,001st prime number?
<br/>
</div>
<div id="main">
<br/>Please enter the target number for this exercise:
<br/>
<br/><input type="text" id="txtTarget" value="10001" size="15" />
<br/>
<br/><button type="button" onclick="getResult()">Click for Results</button>
</div>
<script>
//my javscript functions
//ARRAY FUNCTIONS
function sumArray(theArray) { return theArray.reduce(function(prev, curr) { return prev + curr; }); }
function emptyArray(theArray) { while (theArray.length > 0) theArray.pop(); }
//MATH FUNCTIONS
//some of consecutive squares (1^2 + 2^2 + ... + 10^2)
//last modified on 2015-07-22
function SumOfConsecutiveSquares(num) {
return Math.round((num/6)*(num+1)*((2*num)+1));
}
//sum of consecutive digits - that has its uses
//last modified on 2015-07-16
function SumOfIntegers(num) { return num * (num+1) / 2; }
//my version of Lowest Common Multiple - again lots of exercises seem to want this or some variant
//I found this on the internet - it can be a bit confusing, especially the fact that the use of
//for loops is doing the bulk of the math updates in atypical fashion
//last modified on 2015-07-15
function LCM(theArray) {
for (var d, i, j, n, r = 1; (n = theArray.pop()) !== undefined;) {
while (n > 1) {
if (n%2) {
for (i = 3, j = Math.floor(Math.sqrt(n)); i <= j && n%i; i+=2);
d = (i <= j) ? i : n;
} else d = 2;
for (n /= d, r *= d, i = theArray.length; i;
...
//this uses the isPrime function in my script library
//it also uses the knowledge that after 3, all prime numbers come from the pool of
//6n +/- 1 : so n = 1, so 6n +/1 is 5 or 7, n = 2 = 11,13... and while occasionally
//one of those numbers isn't prime - all of the prime numbers will come from them...
function getResult() {
var target = +document.getElementById('txtTarget').value;
var primeArray = [2,3]; //we'll start with the first two primes
for (var n = 6; primeArray.length < target; n += 6) {
if (isPrime(n-1)) primeArray.push(n-1);
if (isPrime(n+1)) primeArray.push(n+1);
}
console.log(primeArray[target-1]);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.