Arithmetic derivative: Difference between revisions

Added Lua
imported>Algol68G
No edit summary
(Added Lua)
Line 751:
D for 10^19 divided by 7 is 19000000000000000000
D for 10^20 divided by 7 is 200000000000000000000
</pre>
 
=={{header|Lua}}==
{{Trans|ALGOL 68|with condensed output and only showing D values up to 10^17 as 10^18 onwards overflows}}
Tested with Lua 5.1 (LuaJIT and OpenResty), 5.3.6 and 5.4.6.<br/>
Lua 5.2.4 didn't like the string.format. Also the format of the larger D values appears to be sensitive to the Lua version.
<syntaxhighlight lang="lua">
do local function lagarias (n) -- Lagarias arithmetic derivative
if n < 0
then return -lagarias (-n)
elseif n == 0 or n == 1
then return 0
else local function smallPf (j, k) -- Smallest prime factor
if j % k == 0 then return k else return smallPf (j, k + 1) end
end
local f = smallPf (n, 2) local q = math.floor (n / f)
if q == 1
then return 1
else return q * lagarias (f) + f * lagarias (q)
end
end
end
for n = -99,100
do io.write (string.format("%6d", lagarias (n)))
if n % 10 == 0 then io.write ("\n") end
end
io.write ("\n")
for n = 1,17 -- 18, 19 and 20 would overflow
do local m = 10 ^ n
io.write ("D(", string.format ("%d", m), ") / 7 = ", math.floor (lagarias (m) / 7), "\n")
end
end
</syntaxhighlight>
{{out}}
<pre>
-75 -77 -1 -272 -24 -49 -34 -96 -20 -123
-1 -140 -32 -45 -22 -124 -1 -43 -108 -176
-1 -71 -18 -80 -55 -39 -1 -156 -1 -59
-26 -72 -1 -61 -18 -192 -51 -33 -1 -92
-1 -31 -22 -92 -16 -81 -1 -56 -20 -45
-14 -112 -1 -25 -39 -48 -1 -41 -1 -68
-16 -21 -1 -60 -12 -19 -14 -80 -1 -31
-1 -32 -27 -15 -10 -44 -1 -13 -10 -24
-1 -21 -1 -32 -8 -9 -1 -16 -1 -7
-6 -12 -1 -5 -1 -4 -1 -1 0 0
0 1 1 4 1 5 1 12 6 7
1 16 1 9 8 32 1 21 1 24
10 13 1 44 10 15 27 32 1 31
1 80 14 19 12 60 1 21 16 68
1 41 1 48 39 25 1 112 14 45
20 56 1 81 16 92 22 31 1 92
1 33 51 192 18 61 1 72 26 59
1 156 1 39 55 80 18 71 1 176
108 43 1 124 22 45 32 140 1 123
20 96 34 49 24 272 1 77 75 140
 
D(10) / 7 = 1
D(100) / 7 = 20
D(1000) / 7 = 300
D(10000) / 7 = 4000
D(100000) / 7 = 50000
D(1000000) / 7 = 600000
D(10000000) / 7 = 7000000
D(100000000) / 7 = 80000000
D(1000000000) / 7 = 900000000
D(10000000000) / 7 = 10000000000
D(100000000000) / 7 = 110000000000
D(1000000000000) / 7 = 1200000000000
D(10000000000000) / 7 = 13000000000000
D(100000000000000) / 7 = 140000000000000
D(1000000000000000) / 7 = 1500000000000000
D(10000000000000000) / 7 = 16000000000000000
D(100000000000000000) / 7 = 170000000000000000
</pre>
 
3,045

edits