File tree Expand file tree Collapse file tree 5 files changed +47
-0
lines changed Expand file tree Collapse file tree 5 files changed +47
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -10,3 +10,4 @@ pub use special_functions::factorials::*;
1010pub use special_functions:: gamma:: * ;
1111pub use special_functions:: jacobi:: * ;
1212pub use special_functions:: legendre:: * ;
13+ pub use special_functions:: prime:: * ;
Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ pub mod factorials;
66pub mod gamma;
77pub mod jacobi;
88pub mod legendre;
9+ pub mod prime;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
2627using namespace boost ::math;
2728
@@ -101,4 +102,7 @@ void math_legendre_p_zeros(int l, double* out) {
101102}
102103double 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"
You can’t perform that action at this time.
0 commit comments