diff options
| author | chirvasitua <chirvasitua@gmail.com> | 2021-07-09 22:08:53 -0400 |
|---|---|---|
| committer | chirvasitua <chirvasitua@gmail.com> | 2021-07-09 22:08:53 -0400 |
| commit | 755f11616bb4aae27954768d4317320286516b5e (patch) | |
| tree | a8020c69355ac55527c1227b73de488175f0104e /challenge-106/stuart-little/lua | |
| parent | b227f71994d911e49e617f48a663e889c805a17b (diff) | |
| download | perlweeklychallenge-club-755f11616bb4aae27954768d4317320286516b5e.tar.gz perlweeklychallenge-club-755f11616bb4aae27954768d4317320286516b5e.tar.bz2 perlweeklychallenge-club-755f11616bb4aae27954768d4317320286516b5e.zip | |
1st commit on 106_lua
Diffstat (limited to 'challenge-106/stuart-little/lua')
| -rwxr-xr-x | challenge-106/stuart-little/lua/ch-1.lua | 16 | ||||
| -rwxr-xr-x | challenge-106/stuart-little/lua/ch-2.lua | 42 |
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-106/stuart-little/lua/ch-1.lua b/challenge-106/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..71c5eacbab --- /dev/null +++ b/challenge-106/stuart-little/lua/ch-1.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env lua + +-- run <script> <space-separated numbers> + +t={} +for _,v in ipairs(arg) do + table.insert(t,v) +end +table.sort(t) +maxDiff=0 +for k,v in ipairs(t) do + if (t[k+1] and t[k+1]-t[k] > maxDiff) then + maxDiff = t[k+1]-t[k] + end +end +print(("%d"):format(maxDiff)) diff --git a/challenge-106/stuart-little/lua/ch-2.lua b/challenge-106/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..239c29f572 --- /dev/null +++ b/challenge-106/stuart-little/lua/ch-2.lua @@ -0,0 +1,42 @@ +#!/usr/bin/env lua + +-- run <script> <numerator> <denominator> + +require 'luarocks.loader' +local bc = require('bc') + +function expIn(n,p) + local res=0 + while math.abs(n) > 0 do + if (n % p == 0) then res=res+1 end + n = n/p + end + return res +end + +function copr(n,d) + local smN = n % d + local exp,base=1, (10%d) + while ((base-1) % d ~= 0) do + exp=exp+1 + base = (base*10) % d + end + return math.floor(n/d), ("%0"..exp.."d"):format((smN*(bc.pow(10,exp)-1)/d):tostring()) +end + +function nonCopr(n,d) + local sgn = (n < 0) and "-" or "" + local smN = math.abs(n) % d + local exp2,exp5 = expIn(d,2), expIn(d,5) + local exp10 = math.max(exp2,exp5) + local smD=math.floor(d/(2^exp2 * 5^exp5)) + local nRep,rep = copr(((exp2 > exp5) and 5^(exp2-exp5) or 2^(exp5-exp2))*smN,smD) + return sgn, math.floor(math.abs(n)/d), exp10, nRep, rep +end + +function fmtFrac(n,d) + local sgn,int,shift,nRep,rep = nonCopr(tonumber(n),tonumber(d)) + return sgn .. int .. "." .. (nRep==0 and shift==0 and "" or ("%0"..shift.."d"):format(nRep)) .. (rep=="0" and "" or "("..rep..")") +end + +print(fmtFrac(arg[1],arg[2])) |
