Totient function: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add PL/M) |
Not a robot (talk | contribs) (Add PL/I) |
||
Line 4,127: | Line 4,127: | ||
Number of primes up to 100000 = 9592 |
Number of primes up to 100000 = 9592 |
||
</pre> |
</pre> |
||
=={{header|PL/I}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang="pli">totientFunction: procedure options(main); |
|||
totient: procedure(nn) returns(fixed); |
|||
declare (nn, n, i, tot) fixed; |
|||
n = nn; |
|||
tot = n; |
|||
do i=2 repeat(i+2) while(i*i <= n); |
|||
if mod(n,i) = 0 then do; |
|||
do while(mod(n,i) = 0); |
|||
n = n / i; |
|||
end; |
|||
tot = tot - tot / i; |
|||
end; |
|||
if i=2 then i=1; |
|||
end; |
|||
if n>1 then tot = tot - tot/n; |
|||
return(tot); |
|||
end totient; |
|||
showPrimeCount: procedure(n, primeCount); |
|||
declare (n, primeCount) fixed; |
|||
put skip edit('There are', primeCount, ' primes up to', n) (A,F(5),A,F(6)); |
|||
end showPrimeCount; |
|||
declare (n, primeCount, tot) fixed; |
|||
do n = 1 to 25; |
|||
tot = totient(n); |
|||
put edit('phi(', n, ') = ', tot) (A,F(2),A,F(2)); |
|||
if tot = n-1 then do; |
|||
put list('; prime'); |
|||
primeCount = primeCount + 1; |
|||
end; |
|||
put skip; |
|||
end; |
|||
call showPrimeCount(25, primeCount); |
|||
do n = 26 to 10000; |
|||
if totient(n) = n-1 then primeCount = primeCount + 1; |
|||
if n=100 | n=1000 | n=10000 then |
|||
call showPrimeCount(n, primeCount); |
|||
end; |
|||
end totientFunction;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>phi( 1) = 1 |
|||
phi( 2) = 1 ; prime |
|||
phi( 3) = 2 ; prime |
|||
phi( 4) = 2 |
|||
phi( 5) = 4 ; prime |
|||
phi( 6) = 2 |
|||
phi( 7) = 6 ; prime |
|||
phi( 8) = 4 |
|||
phi( 9) = 6 |
|||
phi(10) = 4 |
|||
phi(11) = 10 ; prime |
|||
phi(12) = 4 |
|||
phi(13) = 12 ; prime |
|||
phi(14) = 6 |
|||
phi(15) = 8 |
|||
phi(16) = 8 |
|||
phi(17) = 16 ; prime |
|||
phi(18) = 6 |
|||
phi(19) = 18 ; prime |
|||
phi(20) = 8 |
|||
phi(21) = 12 |
|||
phi(22) = 10 |
|||
phi(23) = 22 ; prime |
|||
phi(24) = 8 |
|||
phi(25) = 20 |
|||
There are 9 primes up to 25 |
|||
There are 25 primes up to 100 |
|||
There are 168 primes up to 1000 |
|||
There are 1229 primes up to 10000</pre> |
|||
=={{header|PL/M}}== |
=={{header|PL/M}}== |