Goldbach's comet: Difference between revisions

Content added Content deleted
(→‎{{header|ALGOL 68}}: Simplified also no need to calculate G(n) for all n to 000 000 - just need 2 000!! Corrected plot)
Line 35: Line 35:
# of prime pairs that sum to n, n even and > 2 #
# of prime pairs that sum to n, n even and > 2 #
# generates an ASCII scatter plot of G(n) up to G(2000) #
# generates an ASCII scatter plot of G(n) up to G(2000) #
# (Goldbach's Comet) #
PR read "primes.incl.a68" PR # include prime utilities #
PR read "primes.incl.a68" PR # include prime utilities #
INT max number = 1 000 000; # maximum number we will consider #
INT max prime = 1 000 000; # maximum number we will consider #
INT max plot = 2 000; # maximum G value for the comet #
# get a list of primes up to max number #
[]INT prime list = EXTRACTPRIMESUPTO max number FROMPRIMESIEVE PRIMESIEVE max number;
[]BOOL prime = PRIMESIEVE max prime; # sieve of primes to max prime #
[ 1 : max number ]INT g2; # table of G values: g2[n] = G(2n) #
[ 0 : max plot ]INT g2; # table of G values: g2[n] = G(2n) #
# construct the table of G values #
# construct the table of G values #
FOR n TO UPB g2 DO g2[ n ] := 0 OD;
FOR n FROM LWB g2 TO UPB g2 DO g2[ n ] := 0 OD;
g2[ 2 ] := 1; # 4 is the only sum of two even primes #
g2[ 4 ] := 1; # 4 is the only sum of two even primes #
FOR n FROM 2 TO UPB prime list DO
FOR p FROM 3 BY 2 TO max plot OVER 2 DO
INT p = prime list[ n ];
IF prime[ p ] THEN
g2[ p ] +:= 1;
g2[ p + p ] +:= 1;
FOR m FROM n + 1 TO UPB prime list
FOR q FROM p + 2 BY 2 TO max plot - p DO
WHILE INT sum = p + prime list[ m ];
IF prime[ q ] THEN
sum <= max number
g2[ p + q ] +:= 1
DO
FI
g2[ sum OVER 2 ] +:= 1
OD
OD
FI
OD;
OD;
# show the first hundred G values #
# show the first hundred G values #
FOR n FROM 2 TO 101 DO
INT c := 0;
FOR n FROM 4 BY 2 TO 202 DO
print( ( whole( g2[ n ], -4 ) ) );
print( ( whole( g2[ n ], -4 ) ) );
IF ( n - 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
IF ( c +:= 1 ) = 10 THEN print( ( newline ) ); c := 0 FI
OD;
OD;
# G( 1 000 000 ) #
# show G( 1 000 000 ) #
INT gm := 0;
print( ( "G(", whole( max number, 0 ), "): "
, whole( g2[ max number OVER 2 ], 0 )
FOR p FROM 3 TO max prime OVER 2 DO
, newline
IF prime[ p ] THEN
)
IF prime[ max prime - p ] THEN
);
gm +:= 1
FI
FI
OD;
print( ( "G(", whole( max prime, 0 ), "): ", whole( gm, 0 ), newline ) );
# find the maximum value of G up to the maximum plot size #
# find the maximum value of G up to the maximum plot size #
INT max plot = 2000;
INT max g := 0;
INT max g := 0;
FOR n FROM 2 BY 2 TO max plot DO
FOR n TO max plot OVER 2 DO
IF g2[ n ] > max g THEN max g := g2[ n ] FI
IF g2[ n ] > max g THEN max g := g2[ n ] FI
OD;
OD;
# draw an ASCII scatter plot of G, each position represents 10 G values #
# draw an ASCII scatter plot of G, each position represents 5 G values #
# the vertical axis is n, the horizontal axis is G(n) #
# the vertical axis is n/10, the horizontal axis is G(n) #
INT plot step = 10;
INT plot step = 10;
STRING plot value = " .-+=*%$&#@";
STRING plot value = " .-+=*%$&#@";
FOR g BY plot step TO ( max plot OVER 2 ) - plot step DO
FOR g FROM 0 BY plot step TO max plot - plot step DO
[ 0 : max g ]INT values;
[ 0 : max g ]INT values;
FOR v pos FROM LWB values TO UPB values DO values[ v pos ] := 0 OD;
FOR v pos FROM LWB values TO UPB values DO values[ v pos ] := 0 OD;
INT max v := 0;
INT max v := 0;
FOR g element FROM g TO g + ( plot step - 1 ) DO
FOR g element FROM g BY 2 TO g + ( plot step - 1 ) DO
INT g2 value = g2[ g element ];
INT g2 value = g2[ g element ];
values[ g2 value ] +:= 1;
values[ g2 value ] +:= 1;
IF g2 value > max v THEN max v := g2 value FI
IF g2 value > max v THEN max v := g2 value FI
OD;
OD;
print( ( IF g MOD 100 = 1 THEN "+" ELSE "|" FI ) );
print( ( IF g MOD 100 = 90 THEN "+" ELSE "|" FI ) );
FOR v pos FROM LWB values TO max v DO
FOR v pos FROM 1 TO max v DO # exclude 0 values from the plot #
print( ( plot value[ values[ v pos ] + 1 ] ) )
print( ( plot value[ values[ v pos ] + 1 ] ) )
OD;
OD;
Line 104: Line 109:
G(1000000): 5402
G(1000000): 5402
</pre><span style="font-size 8px"><pre>
</pre><span style="font-size 8px"><pre>
|+
+.=*
|.=
| +*-
| +=-.
| -+
| ...=-.
| -.-
| .-+....
| --.
| .=.- . .
| --.
| ..-.-...
| .. .-
| .-. + -.
| +..
| .-- ... ..
| -- .
| ...+. . -
+ ... . .
+ .+.- .. .
| .- -
| + + .. . .
| +. .
| . -+. . -
| ... . .
| -.- . . - .
| .....
| + ... .. . .
| .... .
| .--.. - .
| .. . ..
| .. ..- . .. .
| .. . . .
| -+ . .. . .
| .- . .
| - .. .- - .
| .. . ..
| . ..-. . . . .
+ ... . .
+ .+ - . .. .
| -.. .
| . -..- . -
| ... . .
| . +- . . . .
| - . ..
| ..-.. .. ..
| . + .
| . . +. . .-
| . .. . .
| - -- . . . .
| .- . .
| . .-. . . .. .
| .-. .
| .+ . .. - .
| - . . .
| ..-. . . . . .
| . . . ..
| -. -. . . . .
+ - .. .
+ .... ... -.
| ... . .
| . . ..... . . .
| .. . . .
| . ... . . . . . .
| . .- .
| . .... - . . .
| . . . . .
| . . + . . - .
| .. . ..
| ...... . . . .
| .- . .
| .+ .. . . -
| - . . .
| .. ..- . . . .
| .. . . .
| ..-. . ... .
| .. .. .
| -. .. .. . . .
+ . - . .
+ . -.. - . . .
| . .. . .
| ... .- . .. .
| .- . .
| . .-.. . .. .
| . .. . .
| . -.. . . . . .
| . .. . .
| .-.. . . . . .
| .-. .
| - .. . . - ..
| . - . .
| .+ - . . . .
| .. . . .
| ... .. . .. . .
| . ... .
| .-.. .. . . .
| . . . . .
| . - . . . . . . .
+ - . ..
+ + . . . . . . .
| ..- .
| - .. ... . . .
| - . . .
| .+ . . . . . .
| . . . ..
| .. -. . . .. .
| .- . .
| - .. . . . .. .
| .. . -
| . ...- . . . .
| - . -
| .. .- . . . . .
| -. . .
| .. . . .. . . . .
| .. . . .
| .. - ... . . .
| .. . . .
| - .- . . . . .
+ . -. .
+ .... .. . .. .
| ... . .
| . - - . . . . .
| . .. -
| .. - . . .. . .
| . ... .
| .. .-- . -
| . .. . .
| . .. - . . . . .
| ... . .
| .- .. . - . .
| . . . . .
| .. -.. . . . .
| . . . . .
| .- .. . . . . .
| . . . . .
| . =. . .. .
| + . .
| - + . . .. .
+ . . . . .
+ . . . . . . . .. .
| .. - .
| -- . . . . . .
| .... .
| . ... .. . . ..
| .- . .
| . ... .. . . . .
| . . . . .
| . ... .. . - .
| -- .
| . ..- . . . . .
| .. . . .
| . .-. . ... .
| - . ..
| . .. . .. . . . .
| .. . . .
| . .. .. . . . . .
| .. . . .
| ..... . . . . .
+ . . . . .
+ . . . . . . . - .
| . - . .
| . - . - . . . .
| .. . . .
| .-- . . . . .
| . .. . .
| - . .. . .. . .
| .. - .
| . . . .. . . . ..
| . .. . .
| . .. - . . . . .
| .. . . .
| . . . . .. . . . .
| -. . .
| - . . . . . . -
| . .. . .
| ... ... . . . .
| . . . . .
| . . .. - . . . .
+ -. . .
+ .- - . . - .
| - . . .
| . ... .. . . . .
| . . - .
| -. - . - . .
| - . . .
| . . . . . . . . . .
| .. . . .
| . . .- .. . . .
| . . . ..
| . - .- . . . .
| . .. . .
| . - .. . . . . .
| ... -
| . ... .. . . . .
| . . . . .
| . . -. . . . . .
| . . . . .
+ . . . . .
| - . . .
| . . . . .
| - . . .
| . . . . .
| - . . .
| .. . . .
| - . . .
| .. . . .
| . .. . .
+ . . . . .
| .- . .
| . .. . .
| . . . . .
| . .. . .
| . . . . .
| . . . . .
| . .. . .
| .. . . .
| - . . .
+ .- . .
| .. . . .
| .. . . .
| - . . .
| . . . . .
| . . . ..
| .. . . .
| .. . . .
| ... . .
| . .. . .
+ . . . . .
| - . . .
| . .. . .
| .. . . .
| - . . .
| . . - .
| .. . . .
| - . . .
| . -. .
| - . . .
+ - . ..
| . . . . .
| . . . . .
| .. . . .
| .. . . .
| . .. . .
| . . . ..
| .. . . .
| . . . . .
| . . . -
+ .. .. .
| . - . .
| . . . . .
| -. . .
| . . . . .
| . . - .
| .. . . .
| . .. . .
| . . . . .
| . . . . .
+ .. . . .
| . . . . .
| . . . . .
| . . . . .
| - . . .
| ... . .
| .. . . .
| . . . ..
| . . . . .
| . . . . .
+ . . . . .
| - . . .
| . .. . .
| . . . . .
| . . .. .
| - . . .
| . . . . .
| . .. . .
| .. . . .
| . . . . .
+ . . - .
| . - . .
| .. . -
| . . . . .
| . . . . .
| . . . . .
| .. . . .
| . . . . .
| . . . . .
| . . . . .
+ .. . . .
| . - . .
| . . .. .
| - . . .
| . . . . .
| .. . . .
| . . . . .
| . . . . .
| -. . .
| - . . .
+ .. . . .
</pre></span>
</pre></span>