O'Reilly logo

Scratch Cookbook by Brandon Milonovich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sieve of Eratosthenes

This recipe is all about generating prime numbers. It is a known algorithm that can generate these numbers for us. An algorithm is a specific procedure (or set of instructions) that leads us to a result. If you'd like detailed information about how the algorithm works, you should take a moment to visit http://mathworld.wolfram.com/SieveofEratosthenes.html.

In a nutshell, the sieve works by determining if a number is prime by doing the following. You write down all of the numbers from 2 until your end point (let's say that we want to find all of the primes until the number 100 ; you'd write every number from 2 to 100). Next, go through each number from smallest to greatest and cross it out if it is divisible by 2. Then look ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required