aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-12 18:56:29 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-12 18:56:29 +0100
commit49f7f459092f538b5468e255ff4e8ebac490d70a (patch)
treefa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/roger-bell-west/lua/ch-2.lua
parent1711da4f548d3134248cd5e02a13d71762d5e4cb (diff)
parent72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff)
downloadperlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz
perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2
perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip
Merge remote-tracking branch 'upstream/master'
# Conflicts: # challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-159/roger-bell-west/lua/ch-2.lua97
1 files changed, 97 insertions, 0 deletions
diff --git a/challenge-159/roger-bell-west/lua/ch-2.lua b/challenge-159/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..1e79067d53
--- /dev/null
+++ b/challenge-159/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,97 @@
+#! /usr/bin/lua
+
+function genprimes(mx)
+ local primesh = {}
+ for i = 2, 3 do
+ primesh[i] = true
+ end
+ for i = 6, mx, 6 do
+ for j = i-1, i+1, 2 do
+ if j <= mx then
+ primesh[j]=true
+ end
+ end
+ end
+ local q={2,3,5,7}
+ local p=table.remove(q,1)
+ local mr=math.floor(math.sqrt(mx))
+ while p <= mr do
+ if primesh[p] ~= nil then
+ for i = p*p,mx,p do
+ primesh[i] = nil
+ end
+ end
+ if #q < 2 then
+ table.insert(q,q[#q]+4)
+ table.insert(q,q[#q]+2)
+ end
+ p=table.remove(q,1)
+ end
+ local primes = {}
+ for k,v in pairs(primesh) do
+ table.insert(primes,k)
+ end
+ table.sort(primes)
+ return primes
+end
+
+function primefactor(n)
+ local f={}
+ local m=n
+ for k,p in pairs(genprimes(math.floor(math.sqrt(m)))) do
+ while m % p == 0 do
+ m=math.floor(m/p)
+ if f[p] == nil then
+ f[p]=1
+ else
+ f[p] = f[p] + 1
+ end
+ if m==1 then
+ break
+ end
+ end
+ end
+ if m>1 then
+ if f[m] == nil then
+ f[m]=1
+ else
+ f[m] = f[m] + 1
+ end
+ end
+ return f
+end
+
+function moebius(n)
+ local z=0
+ for k,v in pairs(primefactor(n)) do
+ if v > 1 then
+ return 0
+ end
+ z = z + 1
+ end
+ if z % 2 == 0 then
+ return 1
+ end
+ return -1
+end
+
+if moebius(5) == -1 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if moebius(10) == 1 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if moebius(20) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")