From 7c2ffc3dc74dd860ca1b0191a631d6810985aa4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Fri, 13 Oct 2023 14:15:25 +0200 Subject: [PATCH] =?UTF-8?q?m=C3=B6bius(0)=20is=20well=20defined,=20so=20ju?= =?UTF-8?q?st=20take=20a=20u64=20instead=20of=20a=20NonZeroU64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benches/prime_benches.rs | 4 +--- src/lib.rs | 13 +++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/benches/prime_benches.rs b/benches/prime_benches.rs index 0d2ab4f..29eaa95 100644 --- a/benches/prime_benches.rs +++ b/benches/prime_benches.rs @@ -24,9 +24,7 @@ fn benchmarks(c: &mut Criterion) { b.iter(|| black_box(are_prime::<10_000>())) }); - let ints: Vec<_> = (1..1_000_000) - .map(|n| core::num::NonZeroU64::new(n).unwrap()) - .collect(); + let ints: Vec<_> = (1..1_000_000).map(|n| n).collect(); c.bench_function("möbius of first 1e6 integers", |b| { b.iter(|| { for &i in &ints { diff --git a/src/lib.rs b/src/lib.rs index 2283bd9..46a9a0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -347,13 +347,11 @@ pub const fn are_prime() -> [bool; N] { /// # Example /// ``` /// # use const_primes::moebius; -/// use core::num::NonZeroU64; -/// const N: NonZeroU64 = match NonZeroU64::new(1001) {Some(i) => i, None => panic!()}; +/// const N: u64 = 1001; /// const MÖBIUS1001: i8 = moebius(N); /// assert_eq!(MÖBIUS1001, -1); /// ``` -pub const fn moebius(x: core::num::NonZeroU64) -> i8 { - let mut x = x.get(); +pub const fn moebius(mut x: u64) -> i8 { let mut prime_count: u64 = 0; // We avoid code repetition with the use of this macro. @@ -531,16 +529,15 @@ mod test { #[test] fn check_möbius() { - use core::num::NonZeroU64; #[rustfmt::skip] - const TEST_CASES: [i8; 50] = [1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0]; + const TEST_CASES: [i8; 51] = [0, 1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0, 0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0, 0]; for (n, ans) in TEST_CASES.into_iter().enumerate() { - assert_eq!(moebius(NonZeroU64::new(n as u64 + 1).unwrap()), ans); + assert_eq!(moebius(n as u64), ans); } #[rustfmt::skip] const BIG_TEST_CASES: [i8; 51] = [0, -1, -1, 1, 0, -1, 1, 1, 0, -1, -1, 1, 0, -1, 0, -1, 0, 0, 1, -1, 0, -1, -1, -1, 0, 0, 0, 1, 0, 0, -1, -1, 0, -1, -1, 0, 0, 1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, 0]; for (n, ans) in BIG_TEST_CASES.into_iter().enumerate() { - assert_eq!(moebius(NonZeroU64::new(n as u64 + 1000).unwrap()), ans); + assert_eq!(moebius(n as u64 + 1000), ans); } }