Brilliant numbers: Difference between revisions

Content added Content deleted
(added python)
(→‎{{header|Python}}: change to numpy for efficiency)
Line 933: Line 933:


=={{header|Python}}==
=={{header|Python}}==
Using primesieve and numpy modules

<lang Python>from primesieve.numpy import primes
<lang Python>from primesieve.numpy import primes
from math import isqrt
import numpy as np


max_order = 9
max_order = 9
blocks = [primes(10**n, 10**(n + 1)) for n in range(max_order)]
primes = [primes(10**n, 10**(n + 1)) for n in range(max_order)]


def smallest_brilliant(thresh=0):
def smallest_brilliant(lb):
pos = 1
pos = 1
for blk in blocks:
root = isqrt(lb)
n = len(blk)
best = blk[-1]**2


if best < thresh:
for blk in primes:
n = len(blk)
if blk[-1]*blk[-1] < lb:
pos += n*(n + 1)//2
pos += n*(n + 1)//2
continue
continue


hi = n - 1
i = np.searchsorted(blk, root, 'left')
for i, p in enumerate(blk):
i += blk[i]*blk[i] < lb

if (prod := p*p) >= thresh:
best = min(prod, best)
if not i:
break
return blk[0]*blk[0], pos

p = blk[:i + 1]
q = (lb - 1)//p
idx = np.searchsorted(blk, q, 'right')


lo = i
sel = idx < n
while hi > lo:
p, idx = p[sel], idx[sel]
if p*blk[(j := (lo + hi)//2)] >= thresh:
q = blk[idx]
hi = j
else:
lo = j + 1


pos += hi - i
sel = q >= p
if (prod := p*blk[hi]) >= thresh:
p, q, idx = p[sel], q[sel], idx[sel]
best = min(best, prod)


pos += np.sum(idx - np.arange(len(idx)))
return best, pos
return np.min(p*q), pos


p, first100 = 0, []
res = []
p = 0
for i in range(100):
for i in range(100):
p, pos = smallest_brilliant(p + 1)
p, _ = smallest_brilliant(p + 1)
first100.append(p)
res.append(p)


print(f'First 100: {first100}')
print(f'first 100 are {res}')


for n in range(2*max_order):
for i in range(max_order*2):
p, pos = smallest_brilliant(10**n)
thresh = 10**i
p, pos = smallest_brilliant(thresh)
print(f'first above 10**{n:2d}: {p:18d} at position {pos:14d}')</lang>
print(f'Above 10^{i:2d}: {p:20d} at #{pos}')</lang>
{{out}}
{{out}}
<pre>First 100: [4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, 143, 169, 187, 209, 221, 247, 253, 289, 299, 319, 323, 341, 361, 377, 391, 403, 407, 437, 451, 473, 481, 493, 517, 527, 529, 533, 551, 559, 583, 589,
<pre>first 100 are [4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, 143, 169, 187, 209, 221, 247, 253, 289, 299, 319, 323, 341, 361, 377, 391, 403, 407, 437, 451, 473, 481, 493, 517, 527, 529, 533, 551, 559, 583, 589, 611, 629, 649, 667, 671, 689, 697, 703, 713, 731, 737, 767, 779, 781, 793, 799, 803, 817, 841, 851, 869, 871, 893, 899, 901, 913, 923, 943, 949, 961, 979, 989, 1003, 1007, 1027, 1037, 1067, 1073, 1079, 1081, 1121, 1139, 1147, 1157, 1159, 1189, 1207, 1219, 1241, 1247, 1261, 1271, 1273, 1333, 1343, 1349, 1357, 1363, 1369]
Above 10^ 0: 4 at #1
611, 629, 649, 667, 671, 689, 697, 703, 713, 731, 737, 767, 779, 781, 793, 799, 803, 817, 841, 851, 869, 871, 893, 899, 901, 913, 923, 943, 949, 961, 979, 989, 1003, 1007, 1027, 1037, 1067, 1073, 1079, 10
Above 10^ 1: 10 at #4
81, 1121, 1139, 1147, 1157, 1159, 1189, 1207, 1219, 1241, 1247, 1261, 1271, 1273, 1333, 1343, 1349, 1357, 1363, 1369]
Above 10^ 2: 121 at #11
first above 10** 0: 4 at position 1
Above 10^ 3: 1003 at #74
first above 10** 1: 10 at position 4
Above 10^ 4: 10201 at #242
first above 10** 2: 121 at position 11
Above 10^ 5: 100013 at #2505
first above 10** 3: 1003 at position 74
Above 10^ 6: 1018081 at #10538
first above 10** 4: 10201 at position 242
Above 10^ 7: 10000043 at #124364
first above 10** 5: 100013 at position 2505
Above 10^ 8: 100140049 at #573929
first above 10** 6: 1018081 at position 10538
Above 10^ 9: 1000000081 at #7407841
first above 10** 7: 10000043 at position 124364
Above 10^10: 10000600009 at #35547995
first above 10** 8: 100140049 at position 573929
Above 10^11: 100000000147 at #491316167
first above 10** 9: 1000000081 at position 7407841
Above 10^12: 1000006000009 at #2409600866
first above 10**10: 10000600009 at position 35547995
Above 10^13: 10000000000073 at #34896253010
first above 10**11: 100000000147 at position 491316167
Above 10^14: 100000380000361 at #174155363187
first above 10**12: 1000006000009 at position 2409600866
Above 10^15: 1000000000000003 at #2601913448897
first above 10**13: 10000000000073 at position 34896253010
Above 10^16: 10000001400000049 at #13163230391313
first above 10**14: 100000380000361 at position 174155363187
Above 10^17: 100000000000000831 at #201431415980419</pre>
first above 10**15: 1000000000000003 at position 2601913448897
first above 10**16: 10000001400000049 at position 13163230391313
first above 10**17: 100000000000000831 at position 201431415980419 </pre>


=={{header|Raku}}==
=={{header|Raku}}==