Goldbach's comet: Difference between revisions

added RPL
(Added BASIC256. Grouping BASIC dialects)
(added RPL)
 
(3 intermediate revisions by 3 users not shown)
Line 495:
=={{Header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">#arraybase 1
print "The first 100 G numbers are:"
Line 523 ⟶ 524:
next i
end if
g =return cont
end function</syntaxhighlight>
{{out}}
Line 572 ⟶ 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 701 ⟶ 735:
 
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 833 ⟶ 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 1,487 ⟶ 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,516 ⟶ 1,601:
The value of G(1000000) is 5402.
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 1,619 ⟶ 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,151

edits