-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadratic_integers.sf
172 lines (125 loc) · 3.71 KB
/
quadratic_integers.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/ruby
# Simple implementation of quadratic integers.
# See also:
# https://en.wikipedia.org/wiki/Quadratic_integer
class QuadraticInteger(a, b, w = 2) { # represents: a + b*sqrt(w)
method to_s {
"QuadraticInteger(#{a}, #{b}, #{w})"
}
method ==(QuadraticInteger c) {
(a == c.a) && (b == c.b) && (w == c.w)
}
method conjugate {
QuadraticInteger(a, -b, w)
}
method norm {
a*a - w*b*b
}
method add (Number c) {
QuadraticInteger(a+c, b, w)
}
method add (QuadraticInteger z) {
var (c,d) = (z.a, z.b)
QuadraticInteger(a+c, b+d, w)
}
__CLASS__.alias_method(:add, '+')
method sub (Number c) {
QuadraticInteger(a-c, b, w)
}
method sub (QuadraticInteger z) {
var (c,d) = (z.a, z.b)
QuadraticInteger(a-c, b-d, w)
}
__CLASS__.alias_method(:sub, '-')
method mul (Number c) {
QuadraticInteger(a*c, b*c, w)
}
method mul (QuadraticInteger z) {
var (c,d) = (z.a, z.b)
QuadraticInteger(a*c + b*d*w, a*d + b*c, w)
}
__CLASS__.alias_method(:mul, '*')
method mod (Number m) {
QuadraticInteger(a % m, b % m, w)
}
__CLASS__.alias_method(:mod, '%')
method pow(Number n) {
var x = self
var c = QuadraticInteger(1, 0, w)
for bit in (n.digits(2)) {
c *= x if bit
x *= x
}
return c
}
__CLASS__.alias_method(:pow, '**')
method powmod(Number n, Number m) {
var x = self
var c = QuadraticInteger(1, 0, w)
for bit in (n.digits(2)) {
(c *= x) %= m if bit #=
(x *= x) %= m #=
}
return c
}
}
#---------------------------------------------------------------------
# Determine if a given number is probably a prime number.
func is_quadratic_pseudoprime (n, r=2) {
return false if (n <= 1)
return true if (n <= 3)
return true if (r <= 0)
var x = QuadraticInteger(r, 1, r+2).powmod(n, n)
x.a == r || return false
var y = QuadraticInteger(r, -1, r+2).powmod(n, n)
y.a == r || return false
(x.b + y.b == n) && __FUNC__(n, r-1)
}
say is_quadratic_pseudoprime(43) #=> true
say is_quadratic_pseudoprime(97) #=> true
with (QuadraticInteger(1, 1, 2)) {|q|
say 15.of { q.pow(_).a } #=> A001333
say 15.of { q.pow(_).b } #=> A000129
}
with (QuadraticInteger(1, 1, 3)) {|q|
say 15.of { q.pow(_).a } #=> A026150
say 15.of { q.pow(_).b } #=> A002605
}
var n = (274177-1)
var m = (2**64 + 1)
with (QuadraticInteger(3, 4, 2)) {|q|
var r = q.powmod(n, m)
say gcd(r.a-1, m) #=> 2741177
say gcd(r.b, m) #=> 2741177
}
#---------------------------------------------------------------------
func is_gaussian_quadratic_pseudoprime(n) {
return false if (n <= 1)
return true if (n <= 3)
static x = QuadraticInteger(1, -1, -2)
given (n%8) {
when ([1,3]) {
var t = x.powmod(n-1, n)
(t.a==1 && t.b==0)
}
when ([5, 7]) {
var t = x.powmod(n+1, n)
(t.a==3 && t.b==0)
}
else {
false
}
}
}
assert([88561,107185,162401,221761,226801,334153,410041,665281,825265,1569457,1615681,2727649].all(is_gaussian_quadratic_pseudoprime))
assert([80375707,154287451,267559627,326266051,478614067,573183451,643767931,2433943891,4297753027].all(is_gaussian_quadratic_pseudoprime))
for n in (1..1e2) {
if (is_gaussian_quadratic_pseudoprime(n)) {
if (n.is_composite) {
say "Counter-example: #{n}"
}
}
elsif (n.is_prime) {
say "Missed prime: #{n}"
}
}