/* Goldbach's comet (Rosetta code) in Picat. http://rosettacode.org/wiki/Goldbach%27s_comet """ The Goldbach function is studied in relation to Goldbach's conjecture. The function g(E) is defined for all even integers E>2 to be the number of different ways in which E can be expressed as the sum of two primes. Examples G(4) = 1, since 4 can only be expressed as the sum of one distinct pair of primes (4 = 2 + 2) G(22) = 3, since 22 can be expressed as the sum of 3 distinct pairs of primes (22 = 11 + 11 = 5 + 17 = 3 + 19) Task - Find and show (preferably, in a neat 10x10 table) the first 100 G numbers (that is: the result of the G function described above, for the first 100 even numbers >= 4) - Find and display the value of G(1000000) Stretch Calculate the values of G up to 2000 (inclusive) and display the results in a scatter 2d-chart, aka the Goldbach's Comet """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import v3_utils. import util. import cp. main => go. go ?=> println("First 100 G numbers:"), foreach({G,I} in zip(take([G: T in 1..300, G=g(T),G>0],100),1..100)) printf("%2d %s",G,cond(I mod 10 == 0,"\n","")) end, nl, printf("G(1_000_000): %d\n", g(1_000_000)). g(N) = cond((N > 2, N mod 2 == 0), {1 : I in 1..N // 2, prime(I),prime(N-I)}.len, 0). % too slow g2(N) = Count => P = primes(N), Count = 0, foreach(I in 1..P.len, J in I..P.len) if P[I]+P[J] == N then Count := Count + 1 end end.