Radical of an integer: Difference between revisions

Content deleted Content added
Not a robot (talk | contribs)
Add Cowgol
Not a robot (talk | contribs)
Add CLU
Line 117:
6: 2285
7: 8</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">distinct_factors = iter (n: int) yields (int)
if n // 2 = 0 then
yield(2)
while n // 2 = 0 do n := n / 2 end
end
 
div: int := 3
while div <= n do
if n // div = 0 then
yield(div)
while n // div = 0 do n := n / div end
end
div := div + 2
end
end distinct_factors
 
radical = proc (n: int) returns (int)
rad: int := 1
for fac: int in distinct_factors(n) do
rad := rad * fac
end
return(rad)
end radical
 
dpf_count = proc (n: int) returns (int)
count: int := 0
for fac: int in distinct_factors(n) do
count := count + 1
end
return(count)
end dpf_count
 
show_first_50 = proc (out: stream)
stream$putl(out, "Radicals of 1..50:")
for n: int in int$from_to(1, 50) do
stream$putright(out, int$unparse(radical(n)), 5)
if n // 10 = 0 then stream$putl(out, "") end
end
end show_first_50
 
show_radical = proc (out: stream, n: int)
stream$puts(out, "The radical of ")
stream$puts(out, int$unparse(n))
stream$puts(out, " is ")
stream$puts(out, int$unparse(radical(n)))
stream$putl(out, ".")
end show_radical
 
find_distribution = proc (out: stream)
dist: array[int] := array[int]$fill(0, 8, 0)
for n: int in int$from_to(1, 1000000) do
stream$putright(out, int$unparse(n) || "\r", 8)
dpf: int := dpf_count(n)
dist[dpf] := dist[dpf] + 1
end
 
stream$putl(out, "Distribution of radicals:")
for dpf: int in array[int]$indexes(dist) do
stream$putright(out, int$unparse(dpf), 1)
stream$puts(out, ": ")
stream$putright(out, int$unparse(dist[dpf]), 8)
stream$putl(out, "")
end
end find_distribution
 
start_up = proc ()
po: stream := stream$primary_output()
 
show_first_50(po)
stream$putl(po, "")
 
for n: int in array[int]$elements(array[int]$[99999, 499999, 999999]) do
show_radical(po, n)
end
stream$putl(po, "")
 
find_distribution(po)
end start_up</syntaxhighlight>
{{out}}
<pre>Radicals of 1..50:
1 2 3 2 5 6 7 2 3 10
11 6 13 14 15 2 17 6 19 10
21 22 23 6 5 26 3 14 29 30
31 2 33 34 35 6 37 38 39 10
41 42 43 22 15 46 47 6 7 10
 
The radical of 99999 is 33333.
The radical of 499999 is 3937.
The radical of 999999 is 111111.
 
Distribution of radicals:
0: 1
1: 78734
2: 288726
3: 379720
4: 208034
5: 42492
6: 2285
7: 8</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";