Skip to content

Instantly share code, notes, and snippets.

@eira-fransham
Forked from rust-play/playground.rs
Created May 24, 2019 14:06
Show Gist options
  • Save eira-fransham/e0e1685216ff85dfbc371226ba18a65f to your computer and use it in GitHub Desktop.
Save eira-fransham/e0e1685216ff85dfbc371226ba18a65f 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