big: Fix M-R.

This commit is contained in:
Jeroen van Rijn
2021-09-02 19:59:59 +02:00
parent 31918d3b8f
commit 7fa04fa018
2 changed files with 9 additions and 38 deletions

View File

@@ -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() {

View File

@@ -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.