Skip to content

Commit 62aab90

Browse files
committed
new file: Math/cyclotomic_polynomials_expansion_native.sf
new file: Math/quadratic_frobenius_primality_test.sf
1 parent 887fad0 commit 62aab90

File tree

5 files changed

+140
-46
lines changed

5 files changed

+140
-46
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/ruby
2+
3+
# Expansion of cyclotomic polynomials.
4+
5+
# See also:
6+
# https://rosettacode.org/wiki/Cyclotomic_Polynomial
7+
# https://en.wikipedia.org/wiki/Cyclotomic_polynomial
8+
# https://metacpan.org/pod/Math::Polynomial
9+
10+
func cyclotomic(n) is cached {
11+
var x = Poly(n, 1)-1
12+
return x if (n == 1)
13+
var y = n.divisors.grep { _ < n }.prod {|d| __FUNC__(d) }
14+
x / y
15+
}
16+
17+
say "First 30 cyclotomic polynomials:"
18+
for k in (1..30) {
19+
say ("Φ(#{k}) = ", cyclotomic(k))
20+
}
21+
22+
__END__
23+
First 30 cyclotomic polynomials:
24+
Φ(1) = x - 1
25+
Φ(2) = x + 1
26+
Φ(3) = x^2 + x + 1
27+
Φ(4) = x^2 + 1
28+
Φ(5) = x^4 + x^3 + x^2 + x + 1
29+
Φ(6) = x^2 - x + 1
30+
Φ(7) = x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
31+
Φ(8) = x^4 + 1
32+
Φ(9) = x^6 + x^3 + 1
33+
Φ(10) = x^4 - x^3 + x^2 - x + 1
34+
Φ(11) = x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
35+
Φ(12) = x^4 - x^2 + 1
36+
Φ(13) = x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
37+
Φ(14) = x^6 - x^5 + x^4 - x^3 + x^2 - x + 1
38+
Φ(15) = x^8 - x^7 + x^5 - x^4 + x^3 - x + 1
39+
Φ(16) = x^8 + 1
40+
Φ(17) = x^16 + x^15 + x^14 + x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
41+
Φ(18) = x^6 - x^3 + 1
42+
Φ(19) = x^18 + x^17 + x^16 + x^15 + x^14 + x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
43+
Φ(20) = x^8 - x^6 + x^4 - x^2 + 1
44+
Φ(21) = x^12 - x^11 + x^9 - x^8 + x^6 - x^4 + x^3 - x + 1
45+
Φ(22) = x^10 - x^9 + x^8 - x^7 + x^6 - x^5 + x^4 - x^3 + x^2 - x + 1
46+
Φ(23) = x^22 + x^21 + x^20 + x^19 + x^18 + x^17 + x^16 + x^15 + x^14 + x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
47+
Φ(24) = x^8 - x^4 + 1
48+
Φ(25) = x^20 + x^15 + x^10 + x^5 + 1
49+
Φ(26) = x^12 - x^11 + x^10 - x^9 + x^8 - x^7 + x^6 - x^5 + x^4 - x^3 + x^2 - x + 1
50+
Φ(27) = x^18 + x^9 + 1
51+
Φ(28) = x^12 - x^10 + x^8 - x^6 + x^4 - x^2 + 1
52+
Φ(29) = x^28 + x^27 + x^26 + x^25 + x^24 + x^23 + x^22 + x^21 + x^20 + x^19 + x^18 + x^17 + x^16 + x^15 + x^14 + x^13 + x^12 + x^11 + x^10 + x^9 + x^8 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
53+
Φ(30) = x^8 + x^7 - x^5 - x^4 - x^3 + x + 1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/ruby
2+
3+
# A simple implemenetation of the Frobenius Quadratic pseudoprimality test.
4+
5+
# Conditions:
6+
# 1. Make sure n is odd and is not a perfect power.
7+
# 2. Find the smallest odd prime p such that kronecker(p, n) = -1.
8+
# 3. Check if (1 + sqrt(p))^n == (1 - sqrt(p)) mod n.
9+
10+
# Generalized test:
11+
# 1. Make sure n is odd and is not a perfect power.
12+
# 2. Find the smallest squarefree number c such that kronecker(c, n) = -1.
13+
# 3. Check if (a + b*sqrt(c))^n == (a - b*sqrt(c)) mod n, where a,b,c are all coprime with n.
14+
15+
# No counter-examples are known to this test.
16+
17+
func is_frobenius_pseudoprime(n) {
18+
19+
return false if (n <= 1)
20+
return true if (n == 2)
21+
return false if n.is_even
22+
return false if n.is_power
23+
24+
var c = (3..Inf -> lazy.grep { .is_prime }.first {|p|
25+
var k = kronecker(p, n)
26+
return false if ((k == 0) && (p != n))
27+
k == -1
28+
})
29+
30+
var q = Quadratic(1, 1, c)
31+
q.powmod(n,n) == q.conj.mod(n)
32+
}
33+
34+
var FPP_list = 100.by(is_frobenius_pseudoprime)
35+
say FPP_list
36+
37+
assert_eq(FPP_list, 100.nprimes)
38+
39+
__END__
40+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]

Math/symbolic_math.sf

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

1010
class Base() {}
1111
class Power() < Base {}
12-
class Fraction() < Base {}
12+
class main::Fraction() < Base {}
1313
class Sum() < Base {}
1414
class Product() < Base {}
1515
class Log() < Base {}
1616
class Exp() < Base {}
1717
class Symbol() < Base {}
1818

1919
class Base {
20-
#const I = Exp(Fraction(1, 2) * Log(-1))
21-
has I = Power(-1, Fraction(1, 2))
20+
#const I = Exp(main::Fraction(1, 2) * Log(-1))
21+
has I = Power(-1, main::Fraction(1, 2))
2222

2323
method sin {
24-
Fraction(Exp(I * self) - Exp(-I * self), (2*I))
24+
main::Fraction(Exp(I * self) - Exp(-I * self), (2*I))
2525
}
2626

2727
method cos {
28-
Fraction(Exp(-I * self) + Exp(I * self), 2)
28+
main::Fraction(Exp(-I * self) + Exp(I * self), 2)
2929
}
3030

3131
method i {
@@ -59,7 +59,7 @@ class Symbol(name) {
5959
}
6060
6161
method inv {
62-
Fraction(1, self)
62+
main::Fraction(1, self)
6363
}
6464
6565
method simplify {
@@ -79,14 +79,14 @@ class Symbol(name) {
7979
}
8080
}
8181
82-
class Fraction(num, den) {
82+
class main::Fraction(num, den) {
8383
8484
method +(Number o) {
85-
self + Fraction(o, 1)
85+
self + main::Fraction(o, 1)
8686
}
8787
88-
method +(Fraction o) {
89-
Fraction(
88+
method +(main::Fraction o) {
89+
main::Fraction(
9090
num*o.den + o.num*den,
9191
den*o.den
9292
)
@@ -100,7 +100,7 @@ class Fraction(num, den) {
100100
self + o.neg
101101
}
102102
103-
method -(Fraction o) {
103+
method -(main::Fraction o) {
104104
self + o.neg
105105
}
106106
@@ -109,26 +109,26 @@ class Fraction(num, den) {
109109
}
110110
111111
method *(Number o) {
112-
Fraction(num*o, den)
112+
main::Fraction(num*o, den)
113113
}
114114
115-
method *(Fraction o) {
116-
Fraction(num*o.num, den*o.den)
115+
method *(main::Fraction o) {
116+
main::Fraction(num*o.num, den*o.den)
117117
}
118118
119119
method *(Object o) {
120120
Product(self, o)
121121
}
122122
123123
method /(Number o) {
124-
Fraction(
124+
main::Fraction(
125125
num,
126126
den * o
127127
)
128128
}
129129

130-
method /(Fraction o) {
131-
Fraction(
130+
method /(main::Fraction o) {
131+
main::Fraction(
132132
num * o.den,
133133
den * o.num,
134134
)
@@ -141,14 +141,14 @@ class Fraction(num, den) {
141141
method **(Number o) {
142142
if (o < 0) {
143143
var a = o.abs
144-
Fraction(den**a, num**a)
144+
main::Fraction(den**a, num**a)
145145
}
146146
else {
147-
Fraction(num**o, den**o)
147+
main::Fraction(num**o, den**o)
148148
}
149149
}
150150

151-
method ==(Fraction o) {
151+
method ==(main::Fraction o) {
152152
(o.num == num) &&
153153
(o.den == den)
154154
}
@@ -158,23 +158,23 @@ class Fraction(num, den) {
158158
}
159159

160160
method inv {
161-
Fraction(den, num)
161+
main::Fraction(den, num)
162162
}
163163

164164
method neg {
165-
Fraction(num.neg, den)
165+
main::Fraction(num.neg, den)
166166
}
167167

168168
method simplify {
169-
Fraction(num.simplify, den.simplify)
169+
main::Fraction(num.simplify, den.simplify)
170170
}
171171

172172
method numeric {
173173
num.numeric / den.numeric
174174
}
175175

176176
method to_s {
177-
"Fraction(#{num}, #{den})"
177+
"main::Fraction(#{num}, #{den})"
178178
}
179179
}
180180

@@ -237,7 +237,7 @@ class Sum(*values) {
237237
}
238238

239239
method inv {
240-
Fraction(1, self)
240+
main::Fraction(1, self)
241241
}
242242

243243
method neg {
@@ -397,14 +397,14 @@ class Exp(v) {
397397

398398
class Log(v) {
399399
method *(Object o) {
400-
#Fraction(self, o.inv)
400+
#main::Fraction(self, o.inv)
401401
Product(self, o)
402402
#Log(Power(v, o))
403403
#o * self
404404
}
405405

406406
method /(Object o) {
407-
#Fraction(self, o)
407+
#main::Fraction(self, o)
408408
o.inv * self
409409
#Product(self, o.inv)
410410
#Log(Power(v, o.inv))
@@ -427,7 +427,7 @@ class Log(v) {
427427
}
428428

429429
method inv {
430-
Fraction(1, self)
430+
main::Fraction(1, self)
431431
}
432432

433433
method neg {
@@ -468,7 +468,7 @@ class Power(v, n) {
468468
Power(v, n * o)
469469
}
470470

471-
method **(Fraction o) {
471+
method **(main::Fraction o) {
472472
Power(v, n * o)
473473
}
474474

@@ -551,7 +551,7 @@ class Number {
551551
o + self
552552
}
553553

554-
method +(Fraction o) {
554+
method +(main::Fraction o) {
555555
o + self
556556
}
557557

@@ -560,7 +560,7 @@ class Number {
560560
}
561561

562562
# Subtraction
563-
method -(Fraction o) {
563+
method -(main::Fraction o) {
564564
o.neg + self
565565
}
566566

@@ -589,7 +589,7 @@ class Number {
589589
}
590590

591591
# Multiplication
592-
method *(Fraction o) {
592+
method *(main::Fraction o) {
593593
o * self
594594
}
595595

@@ -626,7 +626,7 @@ class Number {
626626
o.inv * self
627627
}
628628

629-
method /(Fraction o) {
629+
method /(main::Fraction o) {
630630
o.inv * self
631631
}
632632
@@ -648,7 +648,7 @@ class Number {
648648
649649
# Other
650650
method inv {
651-
Fraction(1, self)
651+
main::Fraction(1, self)
652652
}
653653
654654
method simplify {
@@ -662,30 +662,30 @@ class Number {
662662
663663
if (__MAIN__ == __FILE__) {
664664
665-
var n = Power(5, Fraction(1,2))
665+
var n = Power(5, main::Fraction(1,2))
666666
say n
667667
say n**2
668668
say n**3
669-
say n**Fraction(2, 1)
669+
say n**main::Fraction(2, 1)
670670
671671
say Exp(Log(5) * 3).simplify
672672
say Power(3, Log(Exp(Sum(3, 4)))).simplify
673673
674674
say Log(42)-Log(6)
675-
say Product(3, 4, 5)*Fraction(1, 3)
675+
say Product(3, 4, 5)*main::Fraction(1, 3)
676676
say Log(42)/Log(6)
677677
say Log(Symbol(:x))
678678
say Log(Symbol(:x))+Log(Symbol(:y))
679-
say Fraction(Symbol(:a), Symbol(:b))+Fraction(Symbol(:c), Symbol(:d))
679+
say main::Fraction(Symbol(:a), Symbol(:b))+main::Fraction(Symbol(:c), Symbol(:d))
680680

681681
say "\n=> Summing..."
682682

683-
var sum = Fraction(0, 1)
683+
var sum = main::Fraction(0, 1)
684684

685685
for n in (0..10) {
686-
sum += Fraction(1, n!)
687-
#sum += Fraction(Symbol(:x), n!)
688-
#sum += Power(n!, Fraction(1, 2)).inv
686+
sum += main::Fraction(1, n!)
687+
#sum += main::Fraction(Symbol(:x), n!)
688+
#sum += Power(n!, main::Fraction(1, 2)).inv
689689
say sum
690690
}
691691

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ A nice collection of day-to-day Sidef scripts.
175175
* [Cyclotomic factorization method](./Math/cyclotomic_factorization_method.sf)
176176
* [Cyclotomic polynomial](./Math/cyclotomic_polynomial.sf)
177177
* [Cyclotomic polynomials expansion](./Math/cyclotomic_polynomials_expansion.sf)
178+
* [Cyclotomic polynomials expansion native](./Math/cyclotomic_polynomials_expansion_native.sf)
178179
* [Derivative numerical approximation](./Math/derivative_numerical_approximation.sf)
179180
* [Difference of matrices factorization method](./Math/difference_of_matrices_factorization_method.sf)
180181
* [Difference of powers factorization method](./Math/difference_of_powers_factorization_method.sf)
@@ -460,6 +461,7 @@ A nice collection of day-to-day Sidef scripts.
460461
* [Pythagorean means](./Math/pythagorean_means.sf)
461462
* [Quadratic-integer factorization method](./Math/quadratic-integer_factorization_method.sf)
462463
* [Quadratic formula solution approx](./Math/quadratic_formula_solution_approx.sf)
464+
* [Quadratic frobenius primality test](./Math/quadratic_frobenius_primality_test.sf)
463465
* [Quadratic integers](./Math/quadratic_integers.sf)
464466
* [Quaternion integer primality test](./Math/quaternion_integer_primality_test.sf)
465467
* [Quaternion integers](./Math/quaternion_integers.sf)

Text/smart_word_wrap_lazy.sf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
# Author: Daniel "Trizen" Șuteu
44
# License: GPLv3
55
# Date: 15th October 2013
6-
# http://trizenx.blogspot.com
7-
# http://trizenx.blogspot.ro/2013/11/smart-word-wrap.html
8-
# Email: <echo dHJpemVueEBnbWFpbC5jb20K | base64 -d>
6+
# https://trizenx.blogspot.com
7+
# https://trizenx.blogspot.com/2013/11/smart-word-wrap.html
98

109
# Smart word wrap algorithm
11-
# See: http://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness
10+
# See: https://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness
1211

1312
class SmartWordWrap {
1413

0 commit comments

Comments
 (0)