From bce7622b27c05e2df8b9569d4562075baabc77f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Fri, 13 Oct 2023 15:13:29 +0200 Subject: [PATCH] =?UTF-8?q?Add=20comment=20to=20m=C3=B6bius=20macro,=20and?= =?UTF-8?q?=20a=20not=20about=20contribution=20to=20rosettacode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 46a9a0e..86a9eb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -351,11 +351,14 @@ pub const fn are_prime() -> [bool; N] { /// const MÖBIUS1001: i8 = moebius(N); /// assert_eq!(MÖBIUS1001, -1); /// ``` +// I have added this code to Rosettacode: https://www.rosettacode.org/wiki/M%C3%B6bius_function#Rust +// as of the writing of this comment. pub const fn moebius(mut x: u64) -> i8 { let mut prime_count: u64 = 0; - // We avoid code repetition with the use of this macro. - macro_rules! handle_factor { + // If x is divisible by the given factor this macro counts the factor and divides it out. + // It then returns zero if x is still divisible by the factor. + macro_rules! handle_factors { ($($factor:expr),+) => { $( if x % $factor == 0 { @@ -370,13 +373,13 @@ pub const fn moebius(mut x: u64) -> i8 { } // Handle 2 and 3 separately, since the wheel will not find these factors. - handle_factor!(2, 3); + handle_factors!(2, 3); // Handle the remaining factors <= √x with the wheel. let mut i = 5; let bound = isqrt(x); while i <= bound { - handle_factor!(i, i + 2); + handle_factors!(i, i + 2); i += 6; }