Totient function: Difference between revisions

Add Cowgol
(Add Cowgol)
Line 1,775:
</pre>
 
=={{header|Cowgol}}==
{{trans|C}}
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub totient(n: uint32): (tot: uint32) is
tot := n;
 
var i: uint32 := 2;
while i*i <= n loop
if n%i == 0 then
while n%i == 0 loop
n := n/i;
end loop;
tot := tot - tot/i;
end if;
if i == 2 then
i := 1;
end if;
i := i + 2;
end loop;
 
if n > 1 then
tot := tot - tot/n;
end if;
end sub;
 
var count: uint16 := 0;
 
print("N\tTotient\tPrime\n");
var n: uint32 := 1;
while n <= 25 loop
var tot := totient(n);
print_i32(n);
print_char('\t');
print_i32(tot);
print_char('\t');
if n-1 == tot then
count := count + 1;
print("Yes\n");
else
print("No\n");
end if;
n := n + 1;
end loop;
 
print("Number of primes up to 25:\t");
print_i16(count);
print_nl();
 
while n <= 100000 loop
tot := totient(n);
if n-1 == tot then
count := count + 1;
end if;
if n == 100 or n == 1000 or n % 10000 == 0 then
print("Number of primes up to ");
print_i32(n);
print(":\t");
print_i16(count);
print_nl();
end if;
n := n + 1;
end loop;</syntaxhighlight>
{{out}}
<pre>N Totient Prime
1 1 No
2 1 Yes
3 2 Yes
4 2 No
5 4 Yes
6 2 No
7 6 Yes
8 4 No
9 6 No
10 4 No
11 10 Yes
12 4 No
13 12 Yes
14 6 No
15 8 No
16 8 No
17 16 Yes
18 6 No
19 18 Yes
20 8 No
21 12 No
22 10 No
23 22 Yes
24 8 No
25 20 No
Number of primes up to 25: 9
Number of primes up to 100: 25
Number of primes up to 1000: 168
Number of primes up to 10000: 1229
Number of primes up to 20000: 2262
Number of primes up to 30000: 3245
Number of primes up to 40000: 4203
Number of primes up to 50000: 5133
Number of primes up to 60000: 6057
Number of primes up to 70000: 6935
Number of primes up to 80000: 7837
Number of primes up to 90000: 8713
Number of primes up to 100000: 9592</pre>
=={{header|D}}==
{{trans|C}}
2,114

edits