Strong and weak primes: Difference between revisions
Content added Content deleted
m (promoted to (full) task status.) |
(→{{header|Perl 6}}: Add Python.) |
||
Line 599: | Line 599: | ||
Count of balanced primes <= 1,000,000: 2,994 |
Count of balanced primes <= 1,000,000: 2,994 |
||
Count of balanced primes <= 10,000,000: 21,837</pre> |
Count of balanced primes <= 10,000,000: 21,837</pre> |
||
=={{header|Python}}== |
|||
Using the popular [http://www.numpy.org numpy] library for fast prime generation. |
|||
COmputes and shows the requested output then adds similar output for the "balanced" case where <code>prime(p) == [prime(p-1) + prime(p+1)] ÷ 2<code>. |
|||
<lang python>import numpy as np |
|||
def primesfrom2to(n): |
|||
# https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 |
|||
""" Input n>=6, Returns a array of primes, 2 <= p < n """ |
|||
sieve = np.ones(n//3 + (n%6==2), dtype=np.bool) |
|||
sieve[0] = False |
|||
for i in range(int(n**0.5)//3+1): |
|||
if sieve[i]: |
|||
k=3*i+1|1 |
|||
sieve[ ((k*k)//3) ::2*k] = False |
|||
sieve[(k*k+4*k-2*k*(i&1))//3::2*k] = False |
|||
return np.r_[2,3,((3*np.nonzero(sieve)[0]+1)|1)] |
|||
p = primes10m = primesfrom2to(10_000_000) |
|||
s = strong10m = [t for s, t, u in zip(p, p[1:], p[2:]) |
|||
if t > (s + u) / 2] |
|||
w = weak10m = [t for s, t, u in zip(p, p[1:], p[2:]) |
|||
if t < (s + u) / 2] |
|||
b = balanced10m = [t for s, t, u in zip(p, p[1:], p[2:]) |
|||
if t == (s + u) / 2] |
|||
print('The first 36 strong primes:', s[:36]) |
|||
print('The count of the strong primes below 1,000,000:', |
|||
sum(1 for p in s if p < 1_000_000)) |
|||
print('The count of the strong primes below 10,000,000:', len(s)) |
|||
print('\nThe first 37 weak primes:', w[:37]) |
|||
print('The count of the weak primes below 1,000,000:', |
|||
sum(1 for p in w if p < 1_000_000)) |
|||
print('The count of the weak primes below 10,000,000:', len(w)) |
|||
print('\n\nThe first 10 balanced primes:', b[:10]) |
|||
print('The count of balanced primes below 1,000,000:', |
|||
sum(1 for p in b if p < 1_000_000)) |
|||
print('The count of balanced primes below 10,000,000:', len(b)) |
|||
print('\nTOTAL primes below 1,000,000:', |
|||
sum(1 for pr in p if pr < 1_000_000)) |
|||
print('TOTAL primes below 10,000,000:', len(p))</lang> |
|||
{{out}} |
|||
<pre>The first 36 strong primes: [11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439] |
|||
The count of the strong primes below 1,000,000: 37723 |
|||
The count of the strong primes below 10,000,000: 320991 |
|||
The first 37 weak primes: [3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401] |
|||
The count of the weak primes below 1,000,000: 37780 |
|||
The count of the weak primes below 10,000,000: 321749 |
|||
The first 10 balanced primes: [5, 53, 157, 173, 211, 257, 263, 373, 563, 593] |
|||
The count of balanced primes below 1,000,000: 2994 |
|||
The count of balanced primes below 10,000,000: 21837 |
|||
TOTAL primes below 1,000,000: 78498 |
|||
TOTAL primes below 10,000,000: 664579</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |