Friday, 15 March 2013

for loop - Javascript code summing primes is looping weirdly -



for loop - Javascript code summing primes is looping weirdly -

i have next code i'm trying sum primes 10.

i'm not trying find efficient code or right code question i'm having difficulty grasping how for-loop acting. mean when i = 7.

since have 2 nested for-loops, reason i = 7 outer loop looping more once, mean inner loop seemingly reaching termination status j < k @ moment j = 2 , k = 2 yet appears insist on continuing loop.

here's code:

var array = [2]; var total = 0; function isprime(i, j) { if ( i%array[j] === 0 ) { console.log("not p check: " + + ", j " + j + " , k " + k); console.log(i + " not prime"); k = j; } else if ((j + 1) === array.length) { console.log(i + " prime"); total += i; console.log("total far " + total); array.push(i); console.log(array); k = j; console.log("is p check: " + + ", j " + j + " , k " + k); } else { j++; console.log("check " + (j + 1) + ": " + + ", j " + j + " , k " + k); isprime(i,j); } } for(var = 3; <=10; i++) { var k = array.length; for(var j = 0; j < k; j++) { console.log("check 1: " + + ", j " + j + " , k " + k); isprime(i, j); } } console.log(total); console.log(array);

the reply inner loop unnecessary in solution. recursive function doing work of inner loop. happens in inner loop numbers repeated more necessary.

dygestor's solution 1 approach. other approach simpler

for(var = 3; <= 10; i++) { isprime(i, 0); }

the logs method:

check 1: 3, j 0 , k 1 3 prime total far 3 [2, 3] p check: 3, j 0 , k 0 check 1: 4, j 0 , k 2 not p check: 4, j 0 , k 2 4 not prime check 1: 5, j 0 , k 2 check 2: 5, j 1 , k 2 5 prime total far 8 [2, 3, 5] p check: 5, j 1 , k 1 check 1: 6, j 0 , k 3 not p check: 6, j 0 , k 3 6 not prime check 1: 7, j 0 , k 3 check 2: 7, j 1 , k 3 check 3: 7, j 2 , k 3 7 prime total far 15 [2, 3, 5, 7] p check: 7, j 2 , k 2 check 1: 7, j 1 , k 2 check 3: 7, j 2 , k 2 check 4: 7, j 3 , k 2 not p check: 7, j 3 , k 2 7 not prime check 1: 7, j 2 , k 3 check 4: 7, j 3 , k 3 not p check: 7, j 3 , k 3 7 not prime check 1: 8, j 0 , k 4 not p check: 8, j 0 , k 4 8 not prime check 1: 9, j 0 , k 4 check 2: 9, j 1 , k 4 not p check: 9, j 1 , k 4 9 not prime check 1: 10, j 0 , k 4 not p check: 10, j 0 , k 4 10 not prime 15 [2, 3, 5, 7]

the logs removing inner loop (bear in mind missing console.log within inner loop well):

3 prime total far 3 [2, 3] p check: 3, j 0 , k 0 not p check: 4, j 0 , k 0 4 not prime check 2: 5, j 1 , k 0 5 prime total far 8 [2, 3, 5] p check: 5, j 1 , k 1 not p check: 6, j 0 , k 1 6 not prime check 2: 7, j 1 , k 0 check 3: 7, j 2 , k 0 7 prime total far 15 [2, 3, 5, 7] p check: 7, j 2 , k 2 not p check: 8, j 0 , k 2 8 not prime check 2: 9, j 1 , k 0 not p check: 9, j 1 , k 0 9 not prime not p check: 10, j 0 , k 1 10 not prime 15 [2, 3, 5, 7]

also k variable unnecessary in new solution. j increment until reaches end of primes array or until target number divisible, whichever sooner.

if want utilize inner loop solution, need remove recursive function , like:

var primes = [2]; var sum = 0; // start looping for(var = 3; <= 10; ++i) { var prime = true; // prime until proven innocent for(var j = 0; j < primes.length; ++j) { // length stays same until later if(i % arr[j] === 0) { // meat of isprime function: divisible? prime = false; break; // stop loop early: number not prime! } } if(prime) { // have prime! primes.push(i); // add together our list of primes sum += i; // add together prime sum } } // log result console.log("the sum of primes 10 (inclusive)", sum); console.log("these primes were", primes);

javascript for-loop primes

No comments:

Post a Comment