Skip to content

Commit 0ec3196

Browse files
committed
new file: Math/gaussian_integers.sf
1 parent 59d73f2 commit 0ec3196

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Math/gaussian_integers.sf

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/ruby
2+
3+
# Simple implementation of Gaussian integers.
4+
5+
# See also:
6+
# https://en.wikipedia.org/wiki/Gaussian_integer
7+
8+
class Gaussian(a, b) { # represents: a + b*i
9+
10+
method to_s {
11+
"Gaussian(#{a}, #{b})"
12+
}
13+
14+
method reals {
15+
(a,b)
16+
}
17+
18+
method ==(Gaussian c) {
19+
(a == c.a) && (b == c.b)
20+
}
21+
22+
method conjugate {
23+
Gaussian(a, -b)
24+
}
25+
26+
method norm {
27+
a*a + b*b
28+
}
29+
30+
method add (Gaussian z) {
31+
var (c,d) = (z.a, z.b)
32+
Gaussian(a+c, b+d)
33+
}
34+
35+
__CLASS__.alias_method(:add, '+')
36+
37+
method sub (Gaussian z) {
38+
var (c,d) = (z.a, z.b)
39+
Gaussian(a-c, b-d)
40+
}
41+
42+
__CLASS__.alias_method(:sub, '-')
43+
44+
method mul (Gaussian z) {
45+
var (c,d) = (z.a, z.b)
46+
Gaussian(a*c - b*d, a*d + b*c)
47+
}
48+
49+
__CLASS__.alias_method(:mul, '*')
50+
51+
method mod (Number m) {
52+
Gaussian(a % m, b % m)
53+
}
54+
55+
__CLASS__.alias_method(:mod, '%')
56+
57+
method pow(Number n) {
58+
var x = self
59+
var c = Gaussian(1, 0)
60+
61+
for bit in (n.digits(2)) {
62+
c *= x if bit
63+
x *= x
64+
}
65+
66+
return c
67+
}
68+
69+
__CLASS__.alias_method(:pow, '**')
70+
71+
method powmod(Number n, Number m) {
72+
73+
var x = self
74+
var c = Gaussian(1, 0)
75+
76+
for bit in (n.digits(2)) {
77+
(c *= x) %= m if bit #=
78+
(x *= x) %= m #=
79+
}
80+
81+
return c
82+
}
83+
}
84+
85+
var a = Gaussian(3,4)**10
86+
var b = Gaussian(3,4).powmod(97, 1234)
87+
88+
say a
89+
say b
90+
91+
# Run some tests
92+
assert_eq([a.reals], [reals(Gauss(3,4)**10)])
93+
assert_eq([b.reals], [reals(Gauss(3,4).powmod(97, 1234))])
94+
assert_eq([reals(a+b)], [reals(Gauss(reals(a)) + Gauss(reals(b)))])
95+
assert_eq([reals(a-b)], [reals(Gauss(reals(a)) - Gauss(reals(b)))])

Math/tribonacci_primality_test.sf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
# Several counter-examples to the strong version (with possible gaps):
3333
# 29111881, 213035761, 1312332001, 1750412161, 8585937361, 9856290601, 19036199881, 26244842401, 35252113601, 39897898921, 58318467481, ...
3434

35+
# See also:
36+
# https://trizenx.blogspot.com/2020/01/primality-testing-algorithms.html
37+
3538
# Matrix for p == {1,3} (mod 8)
3639
const A123 = Matrix(
3740
[1, 0, 0],

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ A nice collection of day-to-day Sidef scripts.
229229
* [Gamma function](./Math/gamma_function.sf)
230230
* [Gauss logarithm approx](./Math/gauss_logarithm_approx.sf)
231231
* [Gaussian elimination GF2 matrix](./Math/gaussian_elimination_GF2_matrix.sf)
232+
* [Gaussian integers](./Math/gaussian_integers.sf)
232233
* [Generalized continued fraction](./Math/generalized_continued_fraction.sf)
233234
* [Generalized continued fraction parts iter](./Math/generalized_continued_fraction_parts_iter.sf)
234235
* [Generalized expansion](./Math/generalized_expansion.sf)

0 commit comments

Comments
 (0)