Skip to content

Commit a5a5f9d

Browse files
committed
modified: Math/nth_composite.sf
modified: Math/nth_prime_power.sf modified: Math/nth_squarefree.sf
1 parent 8391f98 commit a5a5f9d

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

Math/nth_composite.sf

+38-6
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,53 @@
1010
# See also:
1111
# https://oeis.org/A002808
1212

13+
func composite_count_lower(n) {
14+
n - n.prime_count_upper - 1
15+
}
16+
17+
func composite_count_upper(n) {
18+
n - n.prime_count_lower - 1
19+
}
20+
21+
func simple_composite_lower(n) {
22+
int(n + n/log(n) + n/(log(n)**2))
23+
}
24+
25+
func simple_composite_upper(n) {
26+
int(n + n/log(n) + (3*n)/(log(n)**2))
27+
}
28+
29+
func nth_composite_lower(n) {
30+
bsearch_min(simple_composite_lower(n), simple_composite_upper(n), {|k|
31+
composite_count_upper(k) <=> n
32+
})
33+
}
34+
35+
func nth_composite_upper(n) {
36+
bsearch_max(simple_composite_lower(n), simple_composite_upper(n), {|k|
37+
composite_count_lower(k) <=> n
38+
})
39+
}
40+
1341
func nth_composite(n) {
1442

1543
n == 0 && return 1 # not composite, but...
1644
n <= 0 && return NaN
1745
n == 1 && return 4
1846

1947
# Lower and upper bounds from A002808 (for n >= 4).
20-
var min = int(n + n/log(n) + n/(log(n)**2))
21-
var max = int(n + n/log(n) + (3*n)/(log(n)**2))
48+
#var min = int(n + n/log(n) + n/(log(n)**2))
49+
#var max = int(n + n/log(n) + (3*n)/(log(n)**2))
50+
51+
# Better bounds for the n-th composite number
52+
var min = nth_composite_lower(n)
53+
var max = nth_composite_upper(n)
2254

2355
if (n < 4) {
2456
min = 4;
2557
max = 8;
2658
}
2759

28-
#~ var k = bsearch_le(min, max, {|k|
29-
#~ (k - prime_count(k) - 1) <=> n
30-
#~ })
31-
3260
var k = 0
3361
var c = 0
3462

@@ -88,3 +116,7 @@ C(10^9) = 1053422339
88116
C(10^10) = 10475688327
89117
C(10^11) = 104287176419
90118
C(10^12) = 1039019056246
119+
C(10^13) = 10358018863853
120+
C(10^14) = 103307491450820
121+
C(10^15) = 1030734020030318
122+
C(10^16) = 10287026204717358

Math/nth_prime_power.sf

+20-17
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ func nth_prime_power(n) {
7676
}
7777

7878
for n in (1..10) {
79-
var p = nth_prime_power(10**n)
80-
assert(p.is_prime_power)
81-
assert_eq(p.prime_power_count, 10**n)
82-
#assert_eq(10**n -> nth_prime_power, p)
83-
say "P(10^#{n}) = #{p}"
79+
var pp = nth_prime_power(10**n)
80+
assert(pp.is_prime_power)
81+
assert_eq(pp.prime_power_count, 10**n)
82+
assert_eq(10**n -> nth_prime_power, pp)
83+
say "PP(10^#{n}) = #{pp}"
8484
}
8585

8686
assert_eq(
@@ -89,15 +89,18 @@ assert_eq(
8989
)
9090

9191
__END__
92-
P(10^1) = 16
93-
P(10^2) = 419
94-
P(10^3) = 7517
95-
P(10^4) = 103511
96-
P(10^5) = 1295953
97-
P(10^6) = 15474787
98-
P(10^7) = 179390821
99-
P(10^8) = 2037968761
100-
P(10^9) = 22801415981
101-
P(10^10) = 252096675073
102-
P(10^11) = 2760723662941
103-
P(10^12) = 29996212395727
92+
PP(10^1) = 16
93+
PP(10^2) = 419
94+
PP(10^3) = 7517
95+
PP(10^4) = 103511
96+
PP(10^5) = 1295953
97+
PP(10^6) = 15474787
98+
PP(10^7) = 179390821
99+
PP(10^8) = 2037968761
100+
PP(10^9) = 22801415981
101+
PP(10^10) = 252096675073
102+
PP(10^11) = 2760723662941
103+
PP(10^12) = 29996212395727
104+
PP(10^13) = 323780470283789
105+
PP(10^14) = 3475385632514321
106+
PP(10^15) = 37124507635789309

Math/nth_squarefree.sf

+4
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ S(10^11) = 164493406178
9494
S(10^12) = 1644934067511
9595
S(10^13) = 16449340668746
9696
S(10^14) = 164493406685659
97+
S(10^15) = 1644934066850410
98+
S(10^16) = 16449340668485215
99+
S(10^17) = 164493406684817902
100+
S(10^18) = 1644934066848209910

0 commit comments

Comments
 (0)