-
Notifications
You must be signed in to change notification settings - Fork 0
/
omega_primes.sf
77 lines (59 loc) · 2.86 KB
/
omega_primes.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
#!/usr/bin/ruby
# a(n) is the smallest n-gonal number with exactly n distinct prime factors.
# https://oeis.org/A358862
# Known terms:
# 66, 44100, 11310, 103740, 3333330, 185040240, 15529888374, 626141842326, 21647593547580, 351877410344460, 82634328555218440
# PARI/GP program:
#`(
# General version
omega_polygonals(A, B, n, k) = A=max(A, vecprod(primes(n))); (f(m, p, j) = my(list=List()); forprime(q=p, sqrtnint(B\m, j), my(v=m*q, r=nextprime(q+1)); while(v <= B, if(j==1, if(v>=A && ispolygonal(v,k), listput(list, v)), if(v*r <= B, list=concat(list, f(v, r, j-1)))); v *= q)); list); vecsort(Vec(f(1, 2, n)));
a(n, k=n) = if(n < 3, return()); my(x=vecprod(primes(n)), y=2*x); while(1, my(v=omega_polygonals(x, y, n, k)); if(#v >= 1, return(v[1])); x=y+1; y=2*x); \\ ~~~~
# Optimized version, using residues of prime factors
omega_polygonals(A, B, n, k) = A=max(A, vecprod(primes(n))); (f(m, p, j) = my(list=List()); forprime(q=p, sqrtnint(B\m, j), my(s=q%k); if(s==1||s==2||s==3||s==5||s==7||s==9||s==11||s==13||s==15, my(v=m*q, r=nextprime(q+1)); while(v <= B, if(j==1, if(v>=A && ispolygonal(v,k), listput(list, v)), if(v*r <= B, list=concat(list, f(v, r, j-1)))); v *= q))); list); vecsort(Vec(f(1, 2, n)));
a(n, k=n) = if(n < 3, return()); my(x=vecprod(primes(n)), y=2*x); while(1, my(v=omega_polygonals(x, y, n, k)); if(#v >= 1, return(v[1])); x=y+1; y=2*x); \\ ~~~~
)
# Find the residues of the prime factors of n-gonal numbers:
# for n in (3..20) { say [n, 1000.of {|k| polygonal(k, n) }.map{.factor.map{.mod(n)}}.flat.uniq.sort] }
# Resides for each n = 3..n:
#`(
[3, [0, 1, 2]]
[4, [1, 2, 3]]
[5, [0, 1, 2, 3, 4]]
[6, [1, 2, 3, 5]]
[7, [0, 1, 2, 3, 4, 5, 6]]
[8, [1, 2, 3, 5, 7]]
[9, [1, 2, 3, 4, 5, 7, 8]]
[10, [1, 2, 3, 5, 7, 9]]
[11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
[12, [1, 2, 3, 5, 7, 11]]
[13, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
[14, [1, 2, 3, 5, 7, 9, 11, 13]]
[15, [1, 2, 3, 4, 5, 7, 8, 11, 13, 14]]
[16, [1, 2, 3, 5, 7, 9, 11, 13, 15]]
[17, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]
[18, [1, 2, 3, 5, 7, 11, 13, 17]]
[19, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]
[20, [1, 2, 3, 5, 7, 9, 11, 13, 17, 19]]
)
func upper_bound(n, from = 2, upto = 2*from) {
say "\n:: Searching an upper-bound for a(#{n})\n"
loop {
var count = n.omega_prime_count(from, upto)
if (count > 0) {
say "Sieving range: [#{from}, #{upto}]"
say "This range contains: #{count.commify} elements\n"
n.omega_primes_each(from, upto, {|v|
if (v.is_polygonal(n)) {
say "a(#{n}) = #{v}"
return v
}
})
}
from = upto+1
upto *= 2
}
}
upper_bound(14)
__END__
a(14) = 2383985537862979050
a(15) = 239213805711830629680