diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-07-12 10:48:31 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-07-12 10:48:31 -0400 |
| commit | 34a6514808c066bee4e7f3d7d8bdeb67db056392 (patch) | |
| tree | 05d0e268045ef3d6f971ec0e0c3eb1a48bdb7edd /challenge-106/stuart-little/lua | |
| parent | b59f8f4008bb8ec491a9e89f097f04ce54aed4c0 (diff) | |
| parent | 1aa7b6eaba2a58fc1ef0612373e3aed6b61f345d (diff) | |
| download | perlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.tar.gz perlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.tar.bz2 perlweeklychallenge-club-34a6514808c066bee4e7f3d7d8bdeb67db056392.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
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])) |
