Radical of an integer: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(Added Lua) |
||
Line 1,086: | Line 1,086: | ||
----------------------------------------- |
----------------------------------------- |
||
Overall total: 78735 |
Overall total: 78735 |
||
</pre> |
|||
=={{header|Lua}}== |
|||
{{Trans|ALGOL 68}} |
|||
<syntaxhighlight lang="lua"> |
|||
do -- find the radicals of some integers - the radical of n is the product |
|||
-- of the distinct prime factors of n, the radical of 1 is 1 |
|||
local maxNumber = 1000000; -- maximum number we will consider |
|||
local upfc, radical = {}, {} -- unique prime factor counts and radicals |
|||
for i = 1, maxNumber do |
|||
upfc[ i ] = 0 |
|||
radical[ i ] = 1 |
|||
end |
|||
for i = 2, maxNumber do |
|||
if upfc[ i ] == 0 then |
|||
radical[ i ] = i |
|||
upfc[ i ] = 1 |
|||
for j = i + i, maxNumber, i do |
|||
upfc[ j ] = upfc[ j ] + 1 |
|||
radical[ j ] = radical[ j ] * i |
|||
end |
|||
end |
|||
end |
|||
-- show the radicals of the first 50 positive integers |
|||
io.write( "Radicals of 1 to 50:\n" ) |
|||
for i = 1, 50 do |
|||
io.write( string.format( "%5d", radical[ i ] ), ( i % 10 == 0 and "\n" or "" ) ) |
|||
end |
|||
-- radicals of some specific numbers |
|||
io.write( "\n" ) |
|||
io.write( "Radical of 99999: ", radical[ 99999 ], "\n" ) |
|||
io.write( "Radical of 499999: ", radical[ 499999 ], "\n" ) |
|||
io.write( "Radical of 999999: ", radical[ 999999 ], "\n" ) |
|||
io.write( "\n" ) |
|||
-- show the distribution of the unique prime factor counts |
|||
local dpfc = {} |
|||
for i = 1, maxNumber do |
|||
local count = dpfc[ upfc[ i ] ] |
|||
dpfc[ upfc[ i ] ] = ( count == null and 1 or count + 1 ) |
|||
end |
|||
io.write( "Distribution of radicals:\n" ) |
|||
for i = 0, #dpfc do |
|||
io.write( string.format( "%2d", i ), ": ", dpfc[ i ], "\n" ) |
|||
end |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Radicals of 1 to 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 |
|||
Radical of 99999: 33333 |
|||
Radical of 499999: 3937 |
|||
Radical of 999999: 111111 |
|||
Distribution of radicals: |
|||
0: 1 |
|||
1: 78734 |
|||
2: 288726 |
|||
3: 379720 |
|||
4: 208034 |
|||
5: 42492 |
|||
6: 2285 |
|||
7: 8 |
|||
</pre> |
</pre> |
||