Jump to content

Tau function: Difference between revisions

Add Cowgol
(Add BASIC)
(Add Cowgol)
Line 375:
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9</pre>
 
=={{header|Cowgol}}==
{{trans|C}}
<lang cowgol>include "cowgol.coh";
 
typedef N is uint8;
sub tau(n: N): (total: N) is
total := 1;
while n & 1 == 0 loop
total := total + 1;
n := n >> 1;
end loop;
var p: N := 3;
while p*p <= n loop
var count: N := 1;
while n%p == 0 loop
count := count + 1;
n := n / p;
end loop;
total := total * count;
p := p + 2;
end loop;
if n>1 then
total := total << 1;
end if;
end sub;
 
sub print2(n: uint8) is
print_char(' ');
if n<10
then print_char(' ');
else print_i8(n/10);
end if;
print_i8(n%10);
end sub;
 
var n: N := 1;
while n <= 100 loop
print2(tau(n));
if n % 20 == 0 then print_nl(); end if;
n := n + 1;
end loop;</lang>
{{out}}
<pre> 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
4 4 2 8 3 4 4 6 2 8 2 6 4 4 4 9 2 4 4 8
2 8 2 6 6 4 2 10 3 6 4 6 2 8 4 8 4 4 2 12
2 4 6 7 4 8 2 6 4 8 2 12 2 4 6 6 4 8 2 10
5 4 2 12 4 4 4 8 2 12 4 6 4 4 4 12 2 6 6 9</pre>
=={{header|D}}==
{{trans|C++}}
2,119

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.