Goldbach's comet: Difference between revisions

Added BASIC256. Grouping BASIC dialects
(New post.)
(Added BASIC256. Grouping BASIC dialects)
Line 494:
 
=={{Header|BASIC}}==
==={{header|BASIC256}}===
<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
g = 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 589 ⟶ 623:
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|C++}}==
Line 1,499 ⟶ 1,611:
 
[[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}}==
2,130

edits