Friday, 15 April 2011

javascript - Web Worker Prime number calculation -



javascript - Web Worker Prime number calculation -

i trying create html 5 web worker calculate number of prime numbers (staring 1) given value: javascript code is:

var found = 0; var n = 1; var total = 0; var threshold = 10; while (total < threshold) { n += 1; (var = 2; <= math.sqrt(n); i++) { if (!(n % == 0)) { total++; postmessage(found); } else { found++; } } }

this code based on: http://www.codeproject.com/articles/250102/an-html-progressbar-using-web-workers however, web worker script returns value 8, wrong input number 10. going wrong here?

your logic wrong , can sure prime number if not divisible till end. if divisible in between not prime.i have updated code below.

var found = 0; var n = 1; var total = 0; var threshold = 10; while (total < threshold) { n += 1; found = 0 ; (var = 2; <= math.sqrt(n); i++) { if ((n % == 0)) { found = 1; break; } } if(found == 0) total++; postmessage(found); }

javascript web-worker

No comments:

Post a Comment