Created
November 20, 2017 05:21
-
-
Save umedaikiti/24b40390540e4138ae3ebd93bda9a8ca to your computer and use it in GitHub Desktop.
Enumerating Carmichael numbers
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
#[derive(PartialEq, Copy, Clone)] | |
enum State { | |
Unknown, | |
NotPrime, | |
NotCarmichael | |
} | |
fn main() { | |
let mut a = [State::Unknown; 1000000]; | |
a[0] = State::NotCarmichael; | |
a[1] = State::NotCarmichael; | |
for i in 2..a.len() { | |
match a[i] { | |
State::Unknown => {//prime number | |
let mut j = 2*i; | |
while j < a.len() { | |
if a[j] != State::NotCarmichael { | |
a[j] = if j % (i*i) == 0 || (j-1) % (i-1) != 0 { | |
State::NotCarmichael | |
} | |
else { | |
State::NotPrime | |
} | |
} | |
j += i; | |
} | |
} | |
State::NotPrime => println!("{}", i), //Carmichael number | |
State::NotCarmichael => (), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment