{{ message }}

Instantly share code, notes, and snippets.

# gomfunkel/ sieve-of-eratosthenes.md

Last active Jan 12, 2016
Sieve of Eratosthenes

# Sieve of Eratosthenes

Simple implementation of the Sieve of Eratosthenes to find prime numbers.

# Sieve of Eratosthenes

Simple implementation of the Sieve of Eratosthenes to find prime numbers.

 function( a, // Check for primes in the range from 1 to 'a' b, // Array holding numbers as key and primeness as value c, // Placeholder d // Placeholder ){ b=[]; // Initialize array for(c=1;c
 function(a,b,c,d){b=[];for(c=1;c
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2011 YOUR_NAME_HERE Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
 { "name": "sieveOfEratosthenes", "description": "Simple implementation of the Sieve of Eratosthenes to find prime numbers.", "keywords": [ "prime", "number", "math" ] }
 Sieve of Eratosthenes
Expected value (Primes between 1 and 100): 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
Actual value (Primes between 1 and 100):

### gomfunkel commented Oct 8, 2012

 Saved some bytes by flooring the sqrt using "0|" instead of ceiling it with "Math.ceil()" which is not necessary. Additionally sped up the sieving by only considering numbers still marked as prime when eliminating multiples.

### gomfunkel commented Oct 8, 2012

 Hmm, actually it isn't even necessary to floor the sqrt...

### atk commented Oct 9, 2012

 Instead of if, you can put the testing of b[c] inside the last for loop, saving 2 bytes: `function(a,b,c,d){b=[];for(c=1;c

### gomfunkel commented Oct 9, 2012

 @atk Nice, thanks!

### atk commented Oct 10, 2012

 You're welcome, @gomfunkel

### neizod commented Oct 13, 2012

 subscript.