Advertisement
opencomputerstest2

primefact.lua

Apr 24th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.87 KB | None | 0 0
  1. --[[local primes = {}
  2.  
  3. local function isPrime(n)
  4.   local cached = primes[n]
  5.   if cached ~= nil then
  6.     return cached
  7.   end
  8.   for i = 2, math.sqrt(n) do
  9.     if n % i == 0 then
  10.       primes[n] = false
  11.       return false
  12.     end
  13.   end
  14.   primes[n] = true
  15.   return true
  16. end
  17.  
  18. local function primeFactorization(n)
  19.   local finale = {}
  20.   local newNumber = n
  21.   for i = 2, n do
  22.     if isPrime(i) then
  23.       while newNumber % i == 0 do
  24.         finale[#finale + 1] = i
  25.         newNumber = newNumber / i
  26.       end
  27.     end
  28.   end
  29.   return table.concat(finale, ", ")
  30. end]]
  31.  
  32. local a={}local function b(c)local d=a[c]if d~=nil then return d end;for e=2,math.sqrt(c)do if c%e==0 then a[c]=false;return false end end;a[c]=true;return true end;return function(c)local g={}local h=c;for e=2,c do if b(e)then while h%e==0 do g[#g+1]=e;h=h/e end end end;return table.concat(g,", ")end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement