Perfect totient numbers: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
(Added Applesoft BASIC, Chipmunk Basic, Gambas and MSX Basic. Grouping BASIC dialects)
Line 511: Line 511:
2199 3063 4359 4375 5571</pre>
2199 3063 4359 4375 5571</pre>


=={{header|BASIC256}}==
==={{header|Applesoft BASIC}}===
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">100 HOME
110 PRINT "The first 20 perfect totient numbers:"
120 n = 3
130 s = n : t = 0
140 x = s : s = 0
150 FOR i = 1 TO x-1
160 a = x : b = i
170 IF B > 0 THEN C = A : A = B : B = C - INT(C / B) * B : GOTO 170
180 IF a = 1 THEN s = s+1
190 NEXT i
200 t = t+s
210 IF s > 1 THEN GOTO 140
220 IF t = n THEN PRINT n;" "; : z = z+1
230 n = n+2
240 IF z < 20 THEN GOTO 130
250 END</syntaxhighlight>

==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">found = 0
<syntaxhighlight lang="freebasic">found = 0
Line 546: Line 565:
end function</syntaxhighlight>
end function</syntaxhighlight>


==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|QBasic}}
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">100 CLS
110 PRINT "The first 20 perfect totient numbers:"
120 n = 3
130 s = n : t = 0
140 x = s : s = 0
150 FOR i = 1 TO x-1
160 a = x : b = i
170 IF b > 0 THEN c = a : a = b : b = c MOD b : GOTO 170
180 IF a = 1 THEN s = s+1
190 NEXT i
200 t = t+s
210 IF s > 1 THEN GOTO 140
220 IF t = n THEN PRINT n;" "; : z = z+1
230 n = n+2
240 IF z < 20 THEN GOTO 130
250 END</syntaxhighlight>

==={{header|FreeBASIC}}===
Uses the code from the [[Totient_function#FreeBASIC|Totient Function]] example as an include.

<syntaxhighlight lang="freebasic">#include"totient.bas"

dim as uinteger found = 0, curr = 3, sum, toti

while found < 20
sum = totient(curr)
toti = sum
do
toti = totient(toti)
sum += toti
loop while toti <> 1
if sum = curr then
print sum
found += 1
end if
curr += 1
wend</syntaxhighlight>

==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim found As Integer = 0, curr As Integer = 3
Dim sum As Integer, toti As Integer
Print "The first 20 perfect totient numbers are:"
While found < 20
sum = totient(curr)
toti = sum
Do
toti = totient(toti)
sum += toti
Loop While toti <> 1
If sum = curr Then
Print sum; ", ";
found += 1
End If
curr += 1
Wend
Print Chr(8); Chr(8); " "

End

Function GCD(n As Integer, d As Integer) As Integer
If d = 0 Then Return n Else Return GCD(d, n Mod d)
End Function

Function Totient(n As Integer) As Integer
Dim m As Integer, phi As Integer = 0

For m = 1 To n
If GCD(m, n) = 1 Then phi += 1
Next
Return phi
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>

==={{header|GW-BASIC}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.

==={{header|MSX Basic}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.

==={{header|QBasic}}===
The [[#BASIC|BASIC]] solution works without any changes.
Also
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.


=={{header|bc}}==
=={{header|bc}}==
Line 1,049: Line 1,166:
{ 3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571 }
{ 3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571 }
</pre>
</pre>

=={{header|FreeBASIC}}==
Uses the code from the [[Totient_function#FreeBASIC|Totient Function]] example as an include.

<syntaxhighlight lang="freebasic">#include"totient.bas"

dim as uinteger found = 0, curr = 3, sum, toti

while found < 20
sum = totient(curr)
toti = sum
do
toti = totient(toti)
sum += toti
loop while toti <> 1
if sum = curr then
print sum
found += 1
end if
curr += 1
wend</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==