Skip to content

Commit 202fb99

Browse files
authored
Merge pull request #19 from jorenham/prime
2 parents a5bf496 + c34095e commit 202fb99

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

src/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ unsafe extern "C" {
6666
pub fn math_legendre_p_prime(l: c_int, x: f64) -> f64;
6767
pub fn math_legendre_p_zeros(l: c_int, out: *mut f64);
6868
pub fn math_legendre_q(l: c_uint, x: f64) -> f64;
69+
70+
// boost/math/special_functions/prime.hpp
71+
pub fn math_prime(n: c_uint) -> u32;
6972
}

src/math/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub use special_functions::factorials::*;
1010
pub use special_functions::gamma::*;
1111
pub use special_functions::jacobi::*;
1212
pub use special_functions::legendre::*;
13+
pub use special_functions::prime::*;

src/math/special_functions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pub mod factorials;
66
pub mod gamma;
77
pub mod jacobi;
88
pub mod legendre;
9+
pub mod prime;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::ffi;
2+
use core::ffi::c_uint;
3+
4+
/// Corresponds to `static const unsigned max_prime` in `boost/math/special_functions/prime.hpp`
5+
const MAX_PRIME: u32 = 9_999;
6+
7+
/// Fast table lookup to the first 10,000 prime numbers
8+
///
9+
/// The first prime number (`n=0`) is `2` (as `1` isn't terribly useful in practice).
10+
///
11+
/// The function will panic if `n >= 10_000`.
12+
///
13+
/// Corresponds to `boost::math::prime(n)` in C++.
14+
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/number_series/primes.html>
15+
pub fn prime(n: u32) -> u32 {
16+
if n > MAX_PRIME {
17+
panic!("Argument n out of range: got {n}");
18+
}
19+
unsafe { ffi::math_prime(n as c_uint) }
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_prime() {
28+
assert_eq!(prime(0), 2);
29+
assert_eq!(prime(54), 257);
30+
assert_eq!(prime(9_999), 104_729);
31+
}
32+
33+
#[test]
34+
#[should_panic(expected = "Argument n out of range: got 10000")]
35+
fn test_prime_out_of_range() {
36+
let _ = prime(10_000);
37+
}
38+
}

wrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <boost/math/special_functions/gamma.hpp>
2323
#include <boost/math/special_functions/jacobi.hpp>
2424
#include <boost/math/special_functions/legendre.hpp>
25+
#include <boost/math/special_functions/prime.hpp>
2526

2627
using namespace boost::math;
2728

@@ -101,4 +102,7 @@ void math_legendre_p_zeros(int l, double* out) {
101102
}
102103
double math_legendre_q(unsigned l, double x) { return legendre_q(l, x); }
103104

105+
// boost/math/special_functions/prime.hpp
106+
std::uint32_t math_prime(unsigned n) { return prime(n); }
107+
104108
} // extern "C"

0 commit comments

Comments
 (0)