SlightlyLoony
Tera Contributor
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
‎07-29-2010
09:30 AM
And now for something completely different: a contest! If you click on "Read more" below, you'll see some code that finds prime numbers using the Sieve of Eratosthenes algorithm. Can you find the bug in this code? If you can, leave a description in a comment. First one to post the right bug gets a cheap imitation gold sticky star on their forehead! And two if you fix the bug...
gs.log(primes(10111,10222));
function primes(m, n) {
var answer = [];
var lo = n ? Math.max(2,Math.abs(m)) : 2;
var hi = n ? Math.max(2,Math.abs(n)) : Math.abs(m);
if (hi < lo || hi > 1000000)
return answer;
var sieve = new Array();
for (var i = 0; i <= hi; i++)
sieve = true;
for (var i = 2; i < hi; i++) {
if (!sieve)
continue;
if (i >= lo)
answer.push(i);
for (var j = i * 2; j <= hi; j += i)
sieve[j] = false;
}
return answer;
}
1 Comment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.