Goldbach's comet: Difference between revisions
Content added Content deleted
(make graph point marker size smaller) |
|||
Line 276: | Line 276: | ||
The value of G(1000000) is 5402 |
The value of G(1000000) is 5402 |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>from matplotlib.pyplot import scatter |
|||
from sympy import isprime |
|||
def prime_duo_partitions(n): |
|||
if isprime(n): |
|||
yield (n,) |
|||
for i in range(1, n//2 + 1): |
|||
if isprime(i) and isprime(n - i): |
|||
yield (i, n - i) |
|||
def g(n): |
|||
assert n > 2 and n % 2 == 0, 'n in goldbach function g(n) must be even' |
|||
return len(list(prime_duo_partitions(n))) |
|||
print('The first 100 G numbers are:') |
|||
col = 1 |
|||
for n in range(4, 204, 2): |
|||
print(str(g(n)).ljust(4), end = '\n' if (col % 10 == 0) else '') |
|||
col += 1 |
|||
print('\nThe value of G(1000000) is', g(1_000_000)) |
|||
x = range(4, 4002, 2) |
|||
y = [g(i) for i in x] |
|||
colors = [["red", "blue", "green"][(i // 2) % 3] for i in x] |
|||
scatter([i // 2 for i in x], y, marker='.', color = colors) |
|||
</lang>{{out}} |
|||
<pre> |
|||
The first 100 G numbers are: |
|||
1 1 1 2 1 2 2 2 2 3 |
|||
3 3 2 3 2 4 4 2 3 4 |
|||
3 4 5 4 3 5 3 4 6 3 |
|||
5 6 2 5 6 5 5 7 4 5 |
|||
8 5 4 9 4 5 7 3 6 8 |
|||
5 6 8 6 7 10 6 6 12 4 |
|||
5 10 3 7 9 6 5 8 7 8 |
|||
11 6 5 12 4 8 11 5 8 10 |
|||
5 6 13 9 6 11 7 7 14 6 |
|||
8 13 5 8 11 7 9 13 8 9 |
|||
The value of G(1000000) is 5402 |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |