De Polignac numbers: Difference between revisions

m
(→‎{{header|J}}: include stretch goal)
Line 45:
273421</syntaxhighlight>
(We use 999 here for the 1000th number because 0 is the first J index.)
 
=={{header|Python}}==
<syntaxhighlight lang="python">''' Rosetta code rosettacode.org/wiki/De_Polignac_numbers '''
 
from sympy import isprime
from math import log
from numpy import ndarray
 
max_value = 1_000_000
 
all_primes = [i for i in range(max_value + 1) if isprime(i)]
powers_of_2 = [2**i for i in range(int(log(max_value, 2)))]
 
allvalues = ndarray(max_value, dtype=bool)
allvalues[:] = True
 
for i in all_primes:
for j in powers_of_2:
if i + j < max_value:
allvalues[i + j] = False
dePolignac = [n for n in range(1, max_value) if n & 1 == 1 and allvalues[n]]
print(len(dePolignac))
print(dePolignac[-1])
 
 
print('First fifty de Polignac numbers:')
for i, n in enumerate(dePolignac[:50]):
print(f'{n:5}', end='\n' if (i + 1) % 10 == 0 else '')
print(f'\nOne thousandth: {dePolignac[999]:,}')
print(f'\nTen thousandth: {dePolignac[9999]:,}')
</syntaxhighlight>{{out}}
<pre>
First fifty de Polignac numbers:
1 127 149 251 331 337 373 509 599 701
757 809 877 905 907 959 977 997 1019 1087
1199 1207 1211 1243 1259 1271 1477 1529 1541 1549
1589 1597 1619 1649 1657 1719 1759 1777 1783 1807
1829 1859 1867 1927 1969 1973 1985 2171 2203 2213
 
One thousandth: 31,941
 
Ten thousandth: 273,421
</pre>
 
 
=={{header|Raku}}==
4,107

edits