Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 24, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/a7f34ef3a7892175fa2c93418297ae01 to your computer and use it in GitHub Desktop.
Save rust-play/a7f34ef3a7892175fa2c93418297ae01 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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