annotate libinterp/corefcn/__isprimelarge__.cc @ 31051:5f015f2829b7

maint: Merge stable to default
author Arun Giridhar <arungiridhar@gmail.com>
date Wed, 01 Jun 2022 17:20:22 -0400
parents b65e4c635343 3a93a77dd4cf
children 068342cc93b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
1 ////////////////////////////////////////////////////////////////////////
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
2 //
30612
cfade47fc4fc __isprimelarge__.cc: Adjust range of copyright years (bug #61772).
Markus Mützel <markus.muetzel@gmx.de>
parents: 30564
diff changeset
3 // Copyright (C) 2021-2022 The Octave Project Developers
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
4 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
6 // distribution or <https://octave.org/copyright/>.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
7 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
8 // This file is part of Octave.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
9 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
11 // under the terms of the GNU General Public License as published by
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
13 // (at your option) any later version.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
14 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
18 // GNU General Public License for more details.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
19 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
21 // along with Octave; see the file COPYING. If not, see
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
22 // <https://www.gnu.org/licenses/>.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
23 //
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
24 ////////////////////////////////////////////////////////////////////////
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
25
30487
1f51aa6719e5 * __isprimelarge__.cc: Include config.h before other header files.
John W. Eaton <jwe@octave.org>
parents: 30359
diff changeset
26 #if defined (HAVE_CONFIG_H)
1f51aa6719e5 * __isprimelarge__.cc: Include config.h before other header files.
John W. Eaton <jwe@octave.org>
parents: 30359
diff changeset
27 # include "config.h"
1f51aa6719e5 * __isprimelarge__.cc: Include config.h before other header files.
John W. Eaton <jwe@octave.org>
parents: 30359
diff changeset
28 #endif
1f51aa6719e5 * __isprimelarge__.cc: Include config.h before other header files.
John W. Eaton <jwe@octave.org>
parents: 30359
diff changeset
29
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
30 #include "defun.h"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
31 #include "error.h"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
32 #include "ovl.h"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
33
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
34 OCTAVE_NAMESPACE_BEGIN
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
35
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
36 // This function implements the Schrage technique for modular multiplication.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
37 // The returned value is equivalent to "mod (a*b, modulus)"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
38 // but calculated without overflow.
30351
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
39 uint64_t
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
40 safemultiply (uint64_t a, uint64_t b, uint64_t modulus)
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
41 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
42 if (! a || ! b)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
43 return 0;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
44 else if (b == 1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
45 return a;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
46 else if (a == 1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
47 return b;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
48 else if (a > b)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
49 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
50 uint64_t tmp = a;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
51 a = b;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
52 b = tmp;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
53 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
54
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
55 uint64_t q = modulus / a;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
56 uint64_t r = modulus - q * a;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
57 uint64_t term1 = a * (b % q);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
58 uint64_t term2 = (r < q) ? r * (b / q) : safemultiply (r, b / q, modulus);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
59 return (term1 > term2) ? (term1 - term2) : (term1 + modulus - term2);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
60 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
61
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
62 // This function returns "mod (a^b, modulus)"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
63 // but calculated without overflow.
30351
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
64 uint64_t
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
65 safepower (uint64_t a, uint64_t b, uint64_t modulus)
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
66 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
67 uint64_t retval = 1;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
68 while (b > 0)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
69 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
70 if (b & 1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
71 retval = safemultiply (retval, a, modulus);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
72 b >>= 1;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
73 a = safemultiply (a, a, modulus);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
74 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
75 return retval;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
76 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
77
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
78 // This function implements a single round of Miller-Rabin primality testing.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
79 // Returns false if composite, true if pseudoprime for this divisor.
30351
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
80 bool
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
81 millerrabin (uint64_t div, uint64_t d, uint64_t r, uint64_t n)
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
82 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
83 uint64_t x = safepower (div, d, n);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
84 if (x == 1 || x == n-1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
85 return true;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
86
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
87 for (uint64_t j = 1; j < r; j++)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
88 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
89 x = safemultiply (x, x, n);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
90 if (x == n-1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
91 return true;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
92 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
93 return false;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
94 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
95
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
96 // This function uses the Miller-Rabin test to find out whether the input is
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
97 // prime or composite. The input is required to be a scalar 64-bit integer.
30351
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
98 bool
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
99 isprimescalar (uint64_t n)
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
100 {
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
101 // Fast return for even numbers.
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
102 // n==2 is excluded by the time this function is called.
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
103 if (! (n & 1))
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
104 return false;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
105
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
106 // n is now odd. Rewrite n as d * 2^r + 1, where d is odd.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
107 uint64_t d = n-1;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
108 uint64_t r = 0;
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
109 while (! (d & 1))
30351
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
110 {
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
111 d >>= 1;
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
112 r++;
4f4fa00edada maint: __isprimelarge__.cc: apply Octave coding style
Arun Giridhar <arungiridhar@gmail.com>
parents: 30261
diff changeset
113 }
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
114
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
115 // Miller-Rabin test with the first 12 primes.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
116 // If the number passes all 12 tests, then it is prime.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
117 // If it fails any, then it is composite.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
118 // The first 12 primes suffice to test all 64-bit integers.
31050
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
119 return millerrabin ( 2, d, r, n) && millerrabin ( 3, d, r, n)
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
120 && millerrabin ( 5, d, r, n) && millerrabin ( 7, d, r, n)
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
121 && millerrabin (11, d, r, n) && millerrabin (13, d, r, n)
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
122 && millerrabin (17, d, r, n) && millerrabin (19, d, r, n)
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
123 && millerrabin (23, d, r, n) && millerrabin (29, d, r, n)
3a93a77dd4cf Minor performance tweaks to isprime.m and __isprimelarge__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30612
diff changeset
124 && millerrabin (31, d, r, n) && millerrabin (37, d, r, n);
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
125
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
126 /*
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
127 Mathematical references for the curious as to why we need only
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
128 the 12 smallest primes for testing all 64-bit numbers:
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
129 (1) https://oeis.org/A014233
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
130 Comment: a(12) > 2^64. Hence the primality of numbers < 2^64 can be
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
131 determined by asserting strong pseudoprimality to all prime bases <= 37
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
132 (=prime(12)). Testing to prime bases <=31 does not suffice,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
133 as a(11) < 2^64 and a(11) is a strong pseudoprime
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
134 to all prime bases <= 31 (=prime(11)). - Joerg Arndt, Jul 04 2012
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
135 (2) https://arxiv.org/abs/1509.00864
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
136 Strong Pseudoprimes to Twelve Prime Bases
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
137 Jonathan P. Sorenson, Jonathan Webster
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
138
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
139 In addition, a source listed here: https://miller-rabin.appspot.com/
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
140 reports that all 64-bit numbers can be covered with only 7 divisors,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
141 namely 2, 325, 9375, 28178, 450775, 9780504, and 1795265022.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
142 There was no peer-reviewed article to back it up though,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
143 so this code uses the 12 primes <= 37.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
144 */
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
145
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
146 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
147
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
148 DEFUN (__isprimelarge__, args, ,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
149 doc: /* -*- texinfo -*-
30354
c980f938fdd8 doc: grammarcheck C++ files in libinterp/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30261
diff changeset
150 @deftypefn {} {@var{x} =} __isprimelarge__ (@var{n})
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
151 Use the Miller-Rabin test to find out whether the elements of N are prime or
30354
c980f938fdd8 doc: grammarcheck C++ files in libinterp/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30261
diff changeset
152 composite. The input N is required to be a vector or array of 64-bit integers.
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
153 You should call isprime(N) instead of directly calling this function.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
154
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
155 @seealso{isprime, factor}
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
156 @end deftypefn */)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
157 {
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
158 int nargin = args.length ();
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
159 if (nargin != 1)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
160 print_usage ();
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
161
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
162 // This function is intended for internal use by isprime.m,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
163 // so the following error handling should not be necessary. But it is
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
164 // probably good practice for any curious users calling it directly.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
165 uint64NDArray vec = args(0).xuint64_array_value
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
166 ("__isprimelarge__: unable to convert input. Call isprime() instead.");
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
167
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
168 boolNDArray retval (vec.dims(), false);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
169
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
170 for (octave_idx_type i = vec.numel() - 1; i >= 0; i--)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
171 retval(i) = isprimescalar (vec(i));
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
172 // Note: If vec(i) <= 37, this function could go into an infinite loop.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
173 // That situation does not arise when calling this from isprime.m
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
174 // but it could arise if the user calls this function directly with low input
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
175 // or negative input.
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
176 // But it turns out that adding this validation:
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
177 // "if (vec(i) <= 37) then raise an error else call isprimescalar (vec(i))"
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
178 // slows this function down by over 20% for some inputs,
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
179 // so it is better to leave all the input validation in isprime.m
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
180 // and not add it here. The function DOCSTRING now explicitly says:
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
181 // "You should call isprime(N) instead of directly calling this function."
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
182
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
183 return ovl (retval);
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
184 }
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
185
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
186 /*
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
187 %!assert (__isprimelarge__ (41:50), logical ([1 0 1 0 0 0 1 0 0 0]))
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
188 %!assert (__isprimelarge__ (uint64 (12345)), false)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
189 %!assert (__isprimelarge__ (uint64 (2147483647)), true)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
190 %!assert (__isprimelarge__ (uint64 (2305843009213693951)), true)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
191 %!assert (__isprimelarge__ (uint64 (18446744073709551557)), true)
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
192
30346
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
193 %!assert (__isprimelarge__ ([uint64(12345), uint64(2147483647), ...
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
194 %! uint64(2305843009213693951), ...
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
195 %! uint64(18446744073709551557)]),
91c6288781ba maint: Shorten some long lines in libinterp to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 30261
diff changeset
196 %! logical ([0 1 1 1]))
30261
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
197
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
198 %!error <unable to convert input> (__isprimelarge__ ({'foo'; 'bar'}))
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
199 */
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
200
a49c635b179d isprime.m: Speed up for large integers using Miller-Rabin test (bug #61312).
Arun Giridhar <arungiridhar@gmail.com>
parents:
diff changeset
201 OCTAVE_NAMESPACE_END