Brilliant numbers: Difference between revisions

→‎{{header|Python}}: change to numpy for efficiency
(added python)
(→‎{{header|Python}}: change to numpy for efficiency)
Line 933:
 
=={{header|Python}}==
Using primesieve and numpy modules
 
<lang Python>from primesieve.numpy import primes
from math import isqrt
import numpy as np
 
max_order = 9
blocksprimes = [primes(10**n, 10**(n + 1)) for n in range(max_order)]
 
def smallest_brilliant(thresh=0lb):
pos = 1
forroot blk= in blocks:isqrt(lb)
n = len(blk)
best = blk[-1]**2
 
for blk in if best < threshprimes:
n = len(blk)
if blk[-1]*blk[-1] < lb:
pos += n*(n + 1)//2
continue
 
hii = nnp.searchsorted(blk, -root, 1'left')
for i, p+= in enumerate(blk):[i]*blk[i] < lb
 
if (prod := p*p) >= thresh:
if not best = min(prod, best)i:
return blk[0]*blk[0], breakpos
 
bestp = blk[-:i + 1]**2
q = (lb - else:1)//p
idx = np.searchsorted(blk, q, 'right')
 
sel = idx < lo = in
p, idx = p[sel], while hi > lo:idx[sel]
q = if p*blk[(j := (lo + hi)//2)idx] >= thresh:
hi = j
else:
lo = j + 1
 
sel = q pos +>= hi - ip
p, q, idx if (prod := p*blk[hisel]), >=q[sel], thresh:idx[sel]
best = min(best, prod)
 
pos += np.sum(idx - np.arange(len(idx)))
return bestnp.min(p*q), pos
 
p, first100res = 0, []
p = 0
for i in range(100):
p, pos_ = smallest_brilliant(p + 1)
first100res.append(p)
 
print(f'Firstfirst 100: are {first100res}')
 
for ni in range(2*max_order*2):
p, posthresh = smallest_brilliant(10**n)i
p, pos = smallest_brilliant(thresh)
print(f'first aboveAbove 10**^{ni:2d}: {p:18d20d} at position #{pos:14d}')</lang>
{{out}}
<pre>Firstfirst 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: lo = j4 +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: hi121 =at j#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}}==