From 7fa04fa018297bafb148533581bf9756b560c256 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 2 Sep 2021 19:59:59 +0200 Subject: [PATCH] big: Fix M-R. --- core/math/big/example.odin | 39 +++++--------------------------------- core/math/big/prime.odin | 8 ++++---- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/core/math/big/example.odin b/core/math/big/example.odin index 49df357d6..b2e3f82bd 100644 --- a/core/math/big/example.odin +++ b/core/math/big/example.odin @@ -93,45 +93,16 @@ demo :: proc() { err: Error; prime: bool; - foo := [4]f64{1, 2, 4, 5}; - fmt.println(foo.rrr); + trials := 1; - trials := 15; + set(c, "3317044064679887385961981"); { SCOPED_TIMING(.is_prime); - for p in _private_prime_table[2:] { - - set(a, p); - prime, err = internal_int_is_prime(a, trials); - if !prime || err != nil { - fmt.printf("%v wrongly flagged as composite\n", p); - } - - set(a, p - 1); - prime, err = internal_int_is_prime(a, trials); - if prime || err != nil { - fmt.printf("%v wrongly flagged as prime\n", p); - } - - set(a, p + 1); - prime, err = internal_int_is_prime(a, trials); - if prime || err != nil { - fmt.printf("%v wrongly flagged as prime\n", p); - } - } - } - Timings[.is_prime].count = len(_private_prime_table[2:]) * 3; - - internal_set(a, "3317044064679887385961981"); - - { - SCOPED_TIMING(.is_prime); - prime, err = internal_int_is_prime(a, trials); - if prime || err != nil { - print("Wrongly flagged as prime: ", a); - } + prime, err = internal_int_is_prime(c, trials); } + //print("prime: ", c); + fmt.printf("%v %v\n", prime, err); } main :: proc() { diff --git a/core/math/big/prime.odin b/core/math/big/prime.odin index f97d4fe81..47a9b2974 100644 --- a/core/math/big/prime.odin +++ b/core/math/big/prime.odin @@ -223,7 +223,7 @@ internal_int_prime_miller_rabin :: proc(a, b: ^Int, allocator := context.allocat /* Ensure `b` > 1. */ - if internal_gt(b, 1) { return false, nil; } + if internal_lte(b, 1) { return false, nil; } /* Get `n1` = `a` - 1. @@ -291,10 +291,10 @@ internal_int_prime_miller_rabin :: proc(a, b: ^Int, allocator := context.allocat `a` is the big Int to test for primality. `miller_rabin_trials` can be one of the following: - < 0: For `a` up to 3_317_044_064_679_887_385_961_981, set `miller_rabin_trials` to negative to run a predetermined + `< 0`: For `a` up to 3_317_044_064_679_887_385_961_981, set `miller_rabin_trials` to negative to run a predetermined number of trials for a deterministic answer. - = 0: Run Miller-Rabin with bases 2, 3 and one random base < `a`. Non-deterministic. - > 0: Run Miller-Rabin with bases 2, 3 and `miller_rabin_trials` number of random bases. Non-deterministic. + `= 0`: Run Miller-Rabin with bases 2, 3 and one random base < `a`. Non-deterministic. + `> 0`: Run Miller-Rabin with bases 2, 3 and `miller_rabin_trials` number of random bases. Non-deterministic. `miller_rabin_only`: `false` Also use either Frobenius-Underwood or Lucas-Selfridge, depending on the compile-time `MATH_BIG_USE_FROBENIUS_TEST` choice.