Goldbach's comet: Difference between revisions

added RPL
(New post.)
(added RPL)
 
(5 intermediate revisions by 4 users not shown)
Line 494:
 
=={{Header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">#arraybase 1
print "The first 100 G numbers are:"
 
col = 1
for n = 4 to 202 step 2
print rjust(string(g(n)), 4);
if col mod 10 = 0 then print
col += 1
next n
 
print : print "G(1000000) = "; g(1000000)
end
 
function isPrime(v)
if v <= 1 then return False
for i = 2 to int(sqrt(v))
if v mod i = 0 then return False
next i
return True
end function
 
function g(n)
cont = 0
if n mod 2 = 0 then
for i = 2 to (1/2) * n
if isPrime(i) = 1 and isPrime(n - i) = 1 then cont += 1
next i
end if
return cont
end function</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Function isPrime(Byval ValorEval As Uinteger) As Boolean
Line 538 ⟶ 573:
 
The value of G(1000000) is 5402</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Use "isprime.bas"
 
Public Sub Main()
Print "The first 100 G numbers are:"
Dim n As Integer, col As Integer = 1
For n = 4 To 202 Step 2
Print Format$(Str(g(n)), "####");
If col Mod 10 = 0 Then Print
col += 1
Next
Print "\nG(1.000.000) = "; g(1000000)
End
 
Function g(n As Integer) As Integer
 
Dim i As Integer, count As Integer = 0
If n Mod 2 = 0 Then
For i = 2 To n \ 2 '(1/2) * n
If isPrime(i) And isPrime(n - i) Then count += 1
Next
End If
Return count
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{Header|SmileBASIC}}===
Line 589 ⟶ 657:
NEXT
NEXT</syntaxhighlight>
 
==={{header|uBasic/4tH}}===
{{trans|FreeBASIC}}
For performance reasons only '''g(100000)''' is calculated. The value "810" has been verified and is correct.
<syntaxhighlight lang="text">Print "The first 100 G numbers are:"
c = 1
 
For n = 4 To 202 Step 2
Print Using "___#"; FUNC(_g(n));
If (c % 10) = 0 Then Print
c = c + 1
Next
 
Print "\nThe value of G(100000) is "; FUNC(_g(100000))
 
End
 
_isPrime
Param (1)
Local (1)
If a@ < 2 Then Return (0)
For b@ = 2 To FUNC(_Sqrt(a@))
If (a@ % b@) = 0 Then Unloop: Return (0)
Next
Return (1)
_g
Param (1)
Local (2)
c@ = 0
If (a@ % 2) = 0 Then 'n in goldbach function g(n) must be even
For b@ = 2 To a@/2
If FUNC(_isPrime(b@)) * FUNC(_isPrime(a@ - b@)) Then c@ = c@ + 1
Next
EndIf
Return (c@)
 
_Sqrt
Param (1)
Local (3)
 
Let b@ = 1
Let c@ = 0
 
Do Until b@ > a@
Let b@ = b@ * 4
Loop
 
Do While b@ > 1
Let b@ = b@ / 4
Let d@ = a@ - c@ - b@
Let c@ = c@ / 2
If d@ > -1 Then
Let a@ = d@
Let c@ = c@ + b@
Endif
Loop
 
Return (c@)</syntaxhighlight>
{{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(100000) is 810
 
0 OK, 0:210 </pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">import isprime
 
print "The first 100 G numbers are:"
 
col = 1
for n = 4 to 202 step 2
print g(n) using ("####");
if mod(col, 10) = 0 print
col = col + 1
next n
 
print "\nG(1000000) = ", g(1000000)
end
 
sub g(n)
count = 0
if mod(n, 2) = 0 then
for i = 2 to (1/2) * n
if isPrime(i) and isPrime(n - i) count = count + 1
next i
fi
return count
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{Header|C++}}==
Line 721 ⟶ 895:
 
<syntaxhighlight lang="easylang">
procfunc isprimeisprim n . r .
rif n mod 2 = 10 and n > 2
if n <= 1return 0
r = 0
break 1
.
if n mod 2i = 03
sq = ifsqrt n = 2
while i <= break 1sq
.
r = 0
break 1
.
for i = 3 step 2 to sqrt n
if n mod i = 0
r =return 0
break 2
.
i += 2
.
return 1
.
procfunc goldbach n . cnt .
cntfor i = 02 to n div 2
for i = 1if toisprim ni div= 21
call isprime i rcnt += isprim (n - i)
if r = 1
call isprime n - i r
cnt += r
.
.
return cnt
.
numfmt 0 3
for n = 4 step 2 to 202
callwrite goldbach n r
write r
if n mod 20 = 2
print ""
.
.
callprint goldbach 1000000 r
print r
</syntaxhighlight>
 
Line 799 ⟶ 963:
 
Instead, for G(1e6), we find all primes less than a million, subtract each from 1 million and count how many of the differences are prime and cut that in half. We cut that sum in half because this approach counts each pair twice (once with the smallest value first, again with the smallest value second -- since 1e6 is not the square of a prime we do not have a prime which appears twice in one of these sums).
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import javax.imageio.ImageIO;
 
public final class GoldbachsComet {
 
public static void main(String[] aArgs) {
initialisePrimes(2_000_000);
System.out.println("The first 100 Goldbach numbers:");
for ( int n = 2; n < 102; n++ ) {
System.out.print(String.format("%3d%s", goldbachFunction(2 * n), ( n % 10 == 1 ? "\n" : "" )));
}
System.out.println();
System.out.println("The 1,000,000th Goldbach number = " + goldbachFunction(1_000_000));
createImage();
}
private static void createImage() {
final int width = 1040;
final int height = 860;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
List<Color> colours = List.of( Color.BLUE, Color.GREEN, Color.RED );
for ( int n = 2; n < 2002; n++ ) {
graphics.setColor(colours.get(n % 3));
graphics.fillOval(n / 2, height - 5 * goldbachFunction(2 * n), 10, 10);
}
try {
ImageIO.write(image, "png", new File("GoldbachsCometJava.png"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static int goldbachFunction(int aNumber) {
if ( aNumber <= 2 || aNumber % 2 == 1 ) {
throw new AssertionError("Argument must be even and greater than 2: " + aNumber);
}
int result = 0;
for ( int i = 1; i <= aNumber / 2; i++ ) {
if ( primes[i] && primes[aNumber - i] ) {
result += 1;
}
}
return result;
}
private static void initialisePrimes(int aLimit) {
primes = new boolean[aLimit];
for ( int i = 2; i < aLimit; i++ ) {
primes[i] = true;
}
for ( int n = 2; n < Math.sqrt(aLimit); n++ ) {
for ( int k = n * n; k < aLimit; k += n ) {
primes[k] = false;
}
}
}
private static boolean[] primes;
 
}
</syntaxhighlight>
{{ out }}
[[Media:GoldbachsCometJava.png]]
<pre>
The first 100 Goldbach numbers:
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 1,000,000th Goldbach number = 5402
</pre>
 
=={{header|jq}}==
Line 1,278 ⟶ 1,539:
 
'''Stretch goal:''' (offsite SVG image) [https://raw.githubusercontent.com/thundergnat/rc/master/img/Goldbachs-Comet-Raku.svg Goldbachs-Comet-Raku.svg]
 
=={{header|RPL}}==
{{works with|HP|49}}
« '''IF''' 2 MOD '''THEN'''
"GOLDB Error: Odd number" DOERR
'''ELSE'''
0
2 PICK3 2 / CEIL '''FOR''' j
'''IF''' j ISPRIME? '''THEN'''
'''IF''' OVER j - ISPRIME? '''THEN''' 1 + '''END'''
'''END'''
'''NEXT''' NIP
'''END'''
» '<span style="color:blue">GOLDB</span>' STO
« « n <span style="color:blue">GOLDB</span> » 'n' 4 202 2 SEQ
OBJ→ DROP { 10 10 } →ARRY
1000000 <span style="color:blue">GOLDB</span> "G(1000000)" →TAG
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: [[ 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 ]]
1: G(1000000): 5402
</pre>
 
=={{header|Ruby}}==
Line 1,307 ⟶ 1,601:
The value of G(1000000) is 5402.
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 1,402 ⟶ 1,697:
 
[[Media:Goldbach's comet rust.svg]]
 
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
For performance reasons only '''g(100000)''' is calculated. The value "810" has been verified and is correct.
<syntaxhighlight lang="text">Print "The first 100 G numbers are:"
c = 1
 
For n = 4 To 202 Step 2
Print Using "___#"; FUNC(_g(n));
If (c % 10) = 0 Then Print
c = c + 1
Next
 
Print "\nThe value of G(100000) is "; FUNC(_g(100000))
 
End
 
_isPrime
Param (1)
Local (1)
If a@ < 2 Then Return (0)
For b@ = 2 To FUNC(_Sqrt(a@))
If (a@ % b@) = 0 Then Unloop: Return (0)
Next
Return (1)
_g
Param (1)
Local (2)
c@ = 0
If (a@ % 2) = 0 Then 'n in goldbach function g(n) must be even
For b@ = 2 To a@/2
If FUNC(_isPrime(b@)) * FUNC(_isPrime(a@ - b@)) Then c@ = c@ + 1
Next
EndIf
Return (c@)
 
_Sqrt
Param (1)
Local (3)
 
Let b@ = 1
Let c@ = 0
 
Do Until b@ > a@
Let b@ = b@ * 4
Loop
 
Do While b@ > 1
Let b@ = b@ / 4
Let d@ = a@ - c@ - b@
Let c@ = c@ / 2
If d@ > -1 Then
Let a@ = d@
Let c@ = c@ + b@
Endif
Loop
 
Return (c@)</syntaxhighlight>
{{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(100000) is 810
 
0 OK, 0:210 </pre>
 
=={{header|Wren}}==
Line 1,488 ⟶ 1,705:
{{libheader|Wren-plot}}
This follows the Raku example in plotting the first two thousand G values rather than the values up to G(2000) in order to produce an image something like the image in the Wikipedia article.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
import "./math2" for Int
1,150

edits