Created
February 19, 2017 06:11
-
-
Save CraigglesO/5ef2df245d8aaa794e70334aaf3db121 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use std::num; | |
fn main () { | |
// ... use stuff from num library | |
let mut input = String::new(); | |
let mut a: Vec<bool> = Vec::new(); | |
let mut n: i64 = 0; | |
println!("Please enter a number"); | |
match io::stdin().read_line(&mut input) { | |
Ok(t) => { | |
input.pop(); | |
if t > 15 { return; } | |
println!("{} is the input", input); | |
n = input.parse::<i64>().unwrap(); | |
} | |
Err(error) => println!("error: {}", error), | |
} | |
for number in 0..n { | |
a.push(true); | |
} | |
println!("{:?}", a); | |
println!("{} is n", n); | |
let sqrt = (n as f64).sqrt() as i64 + 1; | |
for i in 2..sqrt as i64 { | |
if a[i] == true { | |
let mut j = i*2; | |
while j < n { | |
a[j] = false; | |
j += i; | |
} | |
} | |
} | |
} | |
// Input: an integer n > 1. | |
// | |
// Let A be an array of Boolean values, indexed by integers 2 to n, | |
// initially all set to true. | |
// | |
// for i = 2, 3, 4, ..., not exceeding √n: | |
// if A[i] is true: | |
// for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n: | |
// A[j] := false. | |
// | |
// Output: all i such that A[i] is true. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment