use crate::FixedPoint;
use g_math::fixed_point::tables::prime_table::nth_prime;
use crate::ToponError;
pub const MAX_PRIME_INDEX: usize = 1144;
pub fn sqrt_nth_prime(n: usize) -> Result<FixedPoint, ToponError> {
let p = nth_prime(n).ok_or(ToponError::InvalidConfig)?;
Ok(FixedPoint::from_int(p as i32).sqrt())
}
pub fn constant_bank(n: usize) -> Result<Vec<FixedPoint>, ToponError> {
if n == 0 || n > MAX_PRIME_INDEX + 1 {
return Err(ToponError::InvalidConfig);
}
(0..n).map(sqrt_nth_prime).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn max_prime_index_matches_table() {
assert!(nth_prime(MAX_PRIME_INDEX).is_some(), "table shrank");
assert!(nth_prime(MAX_PRIME_INDEX + 1).is_none(), "table grew");
}
#[test]
fn bank_rejects_empty_and_oversized() {
assert!(constant_bank(0).is_err());
assert!(constant_bank(MAX_PRIME_INDEX + 2).is_err());
assert!(constant_bank(MAX_PRIME_INDEX + 1).is_ok());
assert!(constant_bank(1).is_ok());
}
}