Skip to content

Instantly share code, notes, and snippets.

@umedaikiti
Created November 20, 2017 05:21
Show Gist options
  • Save umedaikiti/24b40390540e4138ae3ebd93bda9a8ca to your computer and use it in GitHub Desktop.
Save umedaikiti/24b40390540e4138ae3ebd93bda9a8ca to your computer and use it in GitHub Desktop.
Enumerating Carmichael numbers
#[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