comptime primes
This commit is contained in:
parent
1432ef7821
commit
485252a76f
1 changed files with 47 additions and 2 deletions
|
|
@ -1,3 +1,48 @@
|
|||
pub fn nth(n: u32) -> u32 {
|
||||
todo!("What is the 0-indexed {n}th prime number?")
|
||||
const PRIME_LEN: usize = 10_010;
|
||||
const LIMIT: usize = 106_000;
|
||||
|
||||
struct PrimeCache {
|
||||
primes: [u32; PRIME_LEN],
|
||||
}
|
||||
|
||||
impl PrimeCache {
|
||||
const fn new() -> Self {
|
||||
let mut primes = [0; PRIME_LEN];
|
||||
let mut sieve = [true; LIMIT];
|
||||
|
||||
sieve[0] = false;
|
||||
sieve[1] = false;
|
||||
|
||||
let mut i = 2;
|
||||
while i * i < LIMIT {
|
||||
if sieve[i] {
|
||||
let mut j = i * i;
|
||||
while j < LIMIT {
|
||||
sieve[j] = false;
|
||||
j += i;
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
let mut num = 0;
|
||||
let mut j = 0;
|
||||
while num < LIMIT && j < PRIME_LEN {
|
||||
if sieve[num] {
|
||||
primes[j] = num as u32;
|
||||
j += 1;
|
||||
}
|
||||
num += 1;
|
||||
}
|
||||
Self { primes }
|
||||
}
|
||||
}
|
||||
|
||||
static CACHE: PrimeCache = PrimeCache::new();
|
||||
|
||||
pub fn nth(n: u32) -> u32 {
|
||||
let nth = n as usize;
|
||||
if nth >= PRIME_LEN {
|
||||
panic!("N more than comptime was calculated");
|
||||
}
|
||||
CACHE.primes[nth]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue