55# See also:
66# https://en.wikipedia.org/wiki/Quadratic_integer
77
8- class Quadratic ( a , b , w = 2 ) { # represents: a + b*sqrt(w)
8+ class QuadraticInteger ( a , b , w = 2 ) { # represents: a + b*sqrt(w)
99
1010 method to_s {
11- "Quadratic (#{ a } , #{ b } , #{ w } )"
11+ "QuadraticInteger (#{ a } , #{ b } , #{ w } )"
1212 }
1313
14- method ==( Quadratic c ) {
14+ method ==( QuadraticInteger c ) {
1515 ( a == c . a ) && ( b == c . b ) && ( w == c . w )
1616 }
1717
1818 method conjugate {
19- Quadratic ( a , -b , w )
19+ QuadraticInteger ( a , -b , w )
2020 }
2121
2222 method norm {
2323 a *a - w *b *b
2424 }
2525
2626 method add ( Number c ) {
27- Quadratic ( a +c , b , w )
27+ QuadraticInteger ( a +c , b , w )
2828 }
2929
30- method add ( Quadratic z ) {
30+ method add ( QuadraticInteger z ) {
3131 var ( c , d ) = ( z . a , z . b )
32- Quadratic ( a +c , b +d , w )
32+ QuadraticInteger ( a +c , b +d , w )
3333 }
3434
3535 __CLASS__ . alias_method ( :add , '+' )
3636
3737 method sub ( Number c ) {
38- Quadratic ( a -c , b , w )
38+ QuadraticInteger ( a -c , b , w )
3939 }
4040
41- method sub ( Quadratic z ) {
41+ method sub ( QuadraticInteger z ) {
4242 var ( c , d ) = ( z . a , z . b )
43- Quadratic ( a -c , b -d , w )
43+ QuadraticInteger ( a -c , b -d , w )
4444 }
4545
4646 __CLASS__ . alias_method ( :sub , '-' )
4747
4848 method mul ( Number c ) {
49- Quadratic ( a *c , b *c , w )
49+ QuadraticInteger ( a *c , b *c , w )
5050 }
5151
52- method mul ( Quadratic z ) {
52+ method mul ( QuadraticInteger z ) {
5353 var ( c , d ) = ( z . a , z . b )
54- Quadratic ( a *c + b *d *w , a *d + b *c , w )
54+ QuadraticInteger ( a *c + b *d *w , a *d + b *c , w )
5555 }
5656
5757 __CLASS__ . alias_method ( :mul , '*' )
5858
5959 method mod ( Number m ) {
60- Quadratic ( a % m , b % m , w )
60+ QuadraticInteger ( a % m , b % m , w )
6161 }
6262
6363 __CLASS__ . alias_method ( :mod , '%' )
6464
6565 method pow ( Number n ) {
6666 var x = self
67- var c = Quadratic ( 1 , 0 , w )
67+ var c = QuadraticInteger ( 1 , 0 , w )
6868
6969 for bit in ( n . digits ( 2 ) ) {
7070 c *= x if bit
@@ -79,7 +79,7 @@ class Quadratic(a, b, w = 2) { # represents: a + b*sqrt(w)
7979 method powmod ( Number n , Number m ) {
8080
8181 var x = self
82- var c = Quadratic ( 1 , 0 , w )
82+ var c = QuadraticInteger ( 1 , 0 , w )
8383
8484 for bit in ( n . digits ( 2 ) ) {
8585 ( c *= x ) %= m if bit #=
@@ -100,11 +100,11 @@ func is_quadratic_pseudoprime (n, r=2) {
100100
101101 return true if ( r <= 0 )
102102
103- var x = Quadratic ( r , 1 , r +2 ) . powmod ( n , n )
103+ var x = QuadraticInteger ( r , 1 , r +2 ) . powmod ( n , n )
104104
105105 x . a == r || return false
106106
107- var y = Quadratic ( r , -1 , r +2 ) . powmod ( n , n )
107+ var y = QuadraticInteger ( r , -1 , r +2 ) . powmod ( n , n )
108108
109109 y . a == r || return false
110110
@@ -114,20 +114,20 @@ func is_quadratic_pseudoprime (n, r=2) {
114114say is_quadratic_pseudoprime ( 43 ) #=> true
115115say is_quadratic_pseudoprime ( 97 ) #=> true
116116
117- with ( Quadratic ( 1 , 1 , 2 ) ) { |q |
117+ with ( QuadraticInteger ( 1 , 1 , 2 ) ) { |q |
118118 say 15 . of { q . pow ( _ ) . a } #=> A001333
119119 say 15 . of { q . pow ( _ ) . b } #=> A000129
120120}
121121
122- with ( Quadratic ( 1 , 1 , 3 ) ) { |q |
122+ with ( QuadraticInteger ( 1 , 1 , 3 ) ) { |q |
123123 say 15 . of { q . pow ( _ ) . a } #=> A026150
124124 say 15 . of { q . pow ( _ ) . b } #=> A002605
125125}
126126
127127var n = ( 274177 -1 )
128128var m = ( 2 **64 + 1 )
129129
130- with ( Quadratic ( 3 , 4 , 2 ) ) { |q |
130+ with ( QuadraticInteger ( 3 , 4 , 2 ) ) { |q |
131131 var r = q . powmod ( n , m )
132132 say gcd ( r . a -1 , m ) #=> 2741177
133133 say gcd ( r . b , m ) #=> 2741177
@@ -140,7 +140,7 @@ func is_gaussian_quadratic_pseudoprime(n) {
140140 return false if ( n <= 1 )
141141 return true if ( n <= 3 )
142142
143- static x = Quadratic ( 1 , -1 , -2 )
143+ static x = QuadraticInteger ( 1 , -1 , -2 )
144144
145145 given ( n %8 ) {
146146 when ( [ 1 , 3 ] ) {
0 commit comments