Skip to content

Commit

Permalink
problem7 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
goformarty committed Feb 27, 2018
1 parent 209386a commit de924ca
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 71 deletions.
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ <h4>Sum square difference</h4>
<br /><br />
<h5 class="answer">Answer: <span id="answer6"></span></h5>
</div>

<!-- Problem 7 -->
<div class="problemContainer">
<h2>Problem 7</h2>
<h4>10001st prime</h4>
<br />
<p>By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the nth number?</p>
<br />
<input id="input7" type="number" min="1" max="15000" placeholder="Enter your number" />
<button id="btn7">Calculate</button>
<br /><br />
<h5 class="answer">Answer: <span id="answer7"></span></h5>
</div>
</div>


Expand Down
155 changes: 96 additions & 59 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// PROBLEM 1: DOM
var btn1 = document.getElementById("btn1");
btn1.onclick = function() {
btn1.onclick = function () {
document.getElementById("answer1").innerHTML = problem1();
};

Expand All @@ -11,11 +11,11 @@ function problem1() {
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 10000) {
if (max > 10000) {
return "Please specify a number between 0-10000";
}
for(var i = 1; i< max; i++){
if(i % 3 === 0 || i % 5 === 0){
for (var i = 1; i < max; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}
Expand All @@ -24,7 +24,7 @@ function problem1() {

// PROBLEM 2: DOM
var btn2 = document.getElementById("btn2");
btn2.onclick = function() {
btn2.onclick = function () {
document.getElementById("answer2").innerHTML = problem2();
};

Expand All @@ -38,23 +38,23 @@ function problem2() {
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 5000000) {
if (max > 5000000) {
return "Please specify a number between 0-5000000";
}
for (sum=0; sum<max; sum=x+y) {
if (sum%2 === 0) {
for (sum = 0; sum < max; sum = x + y) {
if (sum % 2 === 0) {
total += sum;
}
x=y;
y=sum;
x = y;
y = sum;
}
return total;
}


// PROBLEM 3: DOM
var btn3 = document.getElementById("btn3");
btn3.onclick = function() {
btn3.onclick = function () {
document.getElementById("answer3").innerHTML = problem3();
};

Expand All @@ -64,10 +64,10 @@ function problem3() {
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 5000000) {
if (max > 5000000) {
return "Please specify a number between 0-5000000";
}
for (var i=2; i<=max; i++) {
for (var i = 2; i <= max; i++) {
if (!(max % i)) {
max /= i;
i--;
Expand All @@ -78,7 +78,7 @@ function problem3() {

// PROBLEM 4: DOM
var btn4 = document.getElementById("btn4");
btn4.onclick = function() {
btn4.onclick = function () {
document.getElementById("answer4").innerHTML = problem4();
};

Expand All @@ -88,7 +88,7 @@ function problem4() {
if (max === "") {
return "Uoh-ooh, you forgot to specify both numbers!";
}
if(max > 5) {
if (max > 5) {
return "Please specify a number between 0-5";
}
var i, n, m, d, inf, sup, limit, number = 0;
Expand All @@ -99,7 +99,9 @@ function problem4() {
sup = 10 * number + 9;

function isPalindromic(n) {
var p = 0, q = n, r;
var p = 0,
q = n,
r;
while (n > 0) {
r = n % 10;
p = 10 * p + r;
Expand All @@ -125,7 +127,7 @@ function problem4() {

// PROBLEM 5: DOM
var btn5 = document.getElementById("btn5");
btn5.onclick = function() {
btn5.onclick = function () {
document.getElementById("answer5").innerHTML = problem5();
};

Expand All @@ -135,15 +137,16 @@ function problem5() {
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 100) {
if (max > 100) {
return "Please specify a number between 0-100";
}


var i, n = 1;

function largestPower(n, max) {
var p, e = 2, largest = n;
var p, e = 2,
largest = n;
while ((p = Math.pow(n, e)) <= max) {
largest = p;
e += 1;
Expand All @@ -153,28 +156,28 @@ function problem5() {

function isPrime(n) {
var i, max = Math.ceil(Math.sqrt(n));
// since the main loop generates odd numbers only
// we can start testing primality dividing by 3
for (i = 3; i <= max; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}

for (i = 3; i <= max; i += 2) {
if (isPrime(i)) {
n *= largestPower(i, max);
}
}

return n * largestPower(2, max);
// since the main loop generates odd numbers only
// we can start testing primality dividing by 3
for (i = 3; i <= max; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}

for (i = 3; i <= max; i += 2) {
if (isPrime(i)) {
n *= largestPower(i, max);
}
}

return n * largestPower(2, max);
}

// PROBLEM 6: DOM
var btn6 = document.getElementById("btn6");
btn6.onclick = function() {
btn6.onclick = function () {
document.getElementById("answer6").innerHTML = problem6();
};

Expand All @@ -184,31 +187,65 @@ function problem6() {
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 200) {
if (max > 200) {
return "Please specify a number between 0-200";
}

function squareSum(max) {
var i, s = 1,
e = Math.log(max) / Math.LN10;
// special case: max is a power of 10
if (e - (e << 0) === 0) {
s = max * (max >> 1) + (max >> 1);
}
else {
for (i = 2; i <= max; i += 1) {
s += i;
}
}
return s * s;
}
function sumSquare(max) {
var i, s = 1;
for (i = 2; i <= max; i += 1) {
s += i * i;
}
return s;
}

return squareSum(max) - sumSquare(max);
e = Math.log(max) / Math.LN10;
// special case: max is a power of 10
if (e - (e << 0) === 0) {
s = max * (max >> 1) + (max >> 1);
} else {
for (i = 2; i <= max; i += 1) {
s += i;
}
}
return s * s;
}

function sumSquare(max) {
var i, s = 1;
for (i = 2; i <= max; i += 1) {
s += i * i;
}
return s;
}

return squareSum(max) - sumSquare(max);
}
// PROBLEM 7: DOM
var btn7 = document.getElementById("btn7");
btn7.onclick = function () {
document.getElementById("answer7").innerHTML = problem7();
}

// PROBLEM 6: FUNCTION
function problem7() {

var num = document.getElementById('input7').value;
var primes = [];
var counter = 2;

while (primes.length < num) {
if (isPrime(counter)) {
primes.push(counter);
}
counter++;
}
return primes[num - 1];

function isPrime(num) {
if (num < 2) {
return false;
}

for (var i = 2; i <= num / 2; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
}
41 changes: 29 additions & 12 deletions problems-first-version/problem7.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
function getPrimes(max) {
var sieve = [], i, j, primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
function isPrime(num) {
if (num < 2) {
return false;
}
return primes;
}

for (var i = 2; i <= num/2; i++) {
if(num % i === 0) {
return false;
}
}
return true;
}

isPrime(2);

function findPrime(num) {
var primes = [];
var counter = 2;
while (primes.length < num) {
if(isPrime(counter)) {
primes.push(counter);
}
counter++;
}
return primes[num - 1];
}

findPrime(10001);


0 comments on commit de924ca

Please sign in to comment.