Created
May 24, 2019 14:06
-
-
Save rust-play/a7f34ef3a7892175fa2c93418297ae01 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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::iter::repeat; | |
fn aks_coefficients(k: usize) -> Vec<i64> { | |
let mut coefficients = vec![0; k + 1]; | |
coefficients[0] = 1; | |
for i in 1..(k + 1) { | |
coefficients[i] = -(1..i).fold(coefficients[0], |prev, j|{ | |
let old = coefficients[j]; | |
coefficients[j] = old - prev; | |
old | |
}); | |
} | |
coefficients | |
} | |
#[no_mangle] | |
pub extern "C" fn is_prime(p: usize) -> bool { | |
if p < 2 { | |
false | |
} else { | |
let c = aks_coefficients(p); | |
(1 .. (c.len() - 1) / 2 + 1).all(|i| (c[i] % (p as i64)) == 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment