Perfect totient numbers: Difference between revisions

m
(Added XPL0 example.)
 
(6 intermediate revisions by 5 users not shown)
Line 511:
2199 3063 4359 4375 5571</pre>
 
==={{header|BASIC256Applesoft 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}}
<syntaxhighlight lang="freebasic">found = 0
Line 546 ⟶ 565:
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}}==
Line 1,034 ⟶ 1,151:
{{out}}
<pre>3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func totient n .
tot = n
i = 2
while i <= sqrt n
if n mod i = 0
while n mod i = 0
n = n div i
.
tot -= tot div i
.
if i = 2
i = 1
.
i += 2
.
if n > 1
tot -= tot div n
.
return tot
.
n = 1
while cnt < 20
tot = n
sum = 0
while tot <> 1
tot = totient tot
sum += tot
.
if sum = n
write n & " "
cnt += 1
.
n += 2
.
</syntaxhighlight>
 
{{out}}
<pre>
3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571
</pre>
 
=={{header|Factor}}==
Line 1,049 ⟶ 1,209:
{ 3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571 }
</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}}==
Line 1,693 ⟶ 1,832:
{{out}}
<pre>{3,9,15,27,39,81,111,183,243,255,327,363,471,729,2187,2199,3063,4359,4375,5571}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
totientseq(n):=block(
[depth:1,L:n],
while totient(L)#1 do (L:totient(L),depth:depth+1),
j:n,
makelist(j:totient(j),depth),
append([n],%%))$
 
perfect_totient_p(n):=if n=1 then false else is(equal(n,apply("+",rest(totientseq(n)))))$
 
/* Function that returns a list of the first len perfect totient numbers */
perfect_totient_count(len):=block(
[i:1,count:0,result:[]],
while count<len do (if perfect_totient_p(i) then (result:endcons(i,result),count:count+1),i:i+1),
result)$
 
/*Test cases */
perfect_totient_count(20);
</syntaxhighlight>
{{out}}
<pre>
[3,9,15,27,39,81,111,183,243,255,327,363,471,729,2187,2199,3063,4359,4375,5571]
</pre>
 
 
=={{header|Miranda}}==
Line 2,568 ⟶ 2,733:
The first 20 perfect totient numbers are:
[3,9,15,27,39,81,111,183,243,255,327,363,471,729,2187,2199,3063,4359,4375,5571]
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« 0 OVER
'''WHILE''' DUP 1 ≠ '''REPEAT'''
EULER SWAP OVER + SWAP
'''END''' DROP ==
» '<span style="color:blue">PTOT?</span>' STO
« → n
« { } 1
'''WHILE''' n '''REPEAT'''
'''IF''' DUP <span style="color:blue">PTOT?</span> '''THEN'''
SWAP OVER + SWAP
'n' 1 STO-
'''END'''
2 + <span style="color:grey">@ Perfect totient numbers are odd</span>
'''END''' DROP
» » '<span style="color:blue">TASK</span>' STO
 
20 <span style="color:blue">TASK</span>
{{out}}
<pre>
1: { 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571 }
</pre>
 
Line 2,595 ⟶ 2,785:
<pre>3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571
</pre>
 
 
=={{header|Rust}}==
Line 2,774 ⟶ 2,963:
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
The version using Euler's product formula.
<syntaxhighlight lang="ecmascriptwren">varimport totient = Fn".new/math" {for |n|Int
var tot = n
var i = 2
while (i*i <= n) {
if (n%i == 0) {
while(n%i == 0) n = (n/i).floor
tot = tot - (tot/i).floor
}
if (i == 2) i = 1
i = i + 2
}
if (n > 1) tot = tot - (tot/n).floor
return tot
}
 
var perfect = []
Line 2,796 ⟶ 2,973:
var sum = 0
while (tot != 1) {
tot = totientInt.calltotient(tot)
sum = sum + tot
}
1,150

edits