|
10 | 10 | # See also: |
11 | 11 | # https://oeis.org/A002808 |
12 | 12 |
|
| 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 | + |
13 | 41 | func nth_composite(n) { |
14 | 42 |
|
15 | 43 | n == 0 && return 1 # not composite, but... |
16 | 44 | n <= 0 && return NaN |
17 | 45 | n == 1 && return 4 |
18 | 46 |
|
19 | 47 | # 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) |
22 | 54 |
|
23 | 55 | if (n < 4) { |
24 | 56 | min = 4; |
25 | 57 | max = 8; |
26 | 58 | } |
27 | 59 |
|
28 | | - #~ var k = bsearch_le(min, max, {|k| |
29 | | - #~ (k - prime_count(k) - 1) <=> n |
30 | | - #~ }) |
31 | | - |
32 | 60 | var k = 0 |
33 | 61 | var c = 0 |
34 | 62 |
|
@@ -88,3 +116,7 @@ C(10^9) = 1053422339 |
88 | 116 | C(10^10) = 10475688327 |
89 | 117 | C(10^11) = 104287176419 |
90 | 118 | C(10^12) = 1039019056246 |
| 119 | +C(10^13) = 10358018863853 |
| 120 | +C(10^14) = 103307491450820 |
| 121 | +C(10^15) = 1030734020030318 |
| 122 | +C(10^16) = 10287026204717358 |
0 commit comments