Pure Programmer
Blue Matrix


Cluster Map

Project: Sieve of Eratosthenes

A simple way to find prime numbers is to use the [[Sieve of Eratosthenes]]. We start by creating an array of booleans initialized to true. Then we set to false elements that have indexes that are multiples of 2, 3, 5, 7, etc. We will end up with an array that only has elements that are true when their index is a prime number.

Write a program that uses a Sieve of Eratosthenes to compute then print all the prime numbers less than 1000. The program should also print the number of primes found. How large can you make the sieve array before you run out of memory?

Output
$ rustc SieveOfEratosthenes.rs error[E0425]: cannot find function `sqrt` in this scope --> SieveOfEratosthenes.rs:19:28 | 19 | let mut maxCheck:isize = (sqrt((sieveMax) as f64)) as isize; | ^^^^ not found in this scope | help: use the `.` operator to call the method `sqrt` on `f64` | 19 | let mut maxCheck:isize = (((sieveMax) as f64).sqrt() as isize; | ~ ~~~~~~~~ error[E0277]: the type `[bool]` cannot be indexed by `isize` --> SieveOfEratosthenes.rs:23:13 | 23 | if sieve[i] == false { | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[bool]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<bool>` to implement `Index<isize>` error[E0277]: the type `[bool]` cannot be indexed by `isize` --> SieveOfEratosthenes.rs:29:12 | 29 | sieve[j] = false; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[bool]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<bool>` to implement `Index<isize>` error[E0277]: the type `[bool]` cannot be indexed by `isize` --> SieveOfEratosthenes.rs:41:13 | 41 | if sieve[i] == true { | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[bool]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<bool>` to implement `Index<isize>` error: aborting due to 4 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.

Solution