PageRenderTime 25ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/23.lua

http://github.com/badgerman/euler
Lua | 101 lines | 78 code | 12 blank | 11 comment | 12 complexity | b321b53fa6ea9eb395085b10eeda590a MD5 | raw file
  1-- A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
  2--
  3-- A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.
  4--
  5-- As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
  6--
  7-- Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
  8
  9require "euler"
 10
 11local last = 28123
 12primes, prime_table = sieve_primes(last)
 13
 14function sum(t, b, e)
 15  local s = 1
 16  local i
 17  for i = b, e do
 18    local p = primes[i]
 19    local x = t[i]
 20    local m = 1
 21    for k = 1, x do
 22      m = m + math.pow(p, k)
 23    end
 24    s = s * m
 25  end
 26  return s
 27end
 28
 29abundance = {}
 30
 31function is_abundant(i)
 32  local x = abundance[i]
 33  if x==nil then 
 34    local d, n = prime_divisors(i, primes)
 35    x = sum(d, 1, n) - i
 36    abundance[i] = x
 37  end
 38  -- print(i, x)
 39  return x>i
 40end
 41
 42function is_perfect(i)
 43  local x = abundance[i]
 44  if x==nil then 
 45    local d, n = prime_divisors(i, primes)
 46    x = sum(d, 1, n) - i
 47    abundance[i] = x
 48  end
 49  -- print(i, x)
 50  return x==i
 51end
 52
 53function test(n)
 54  local x, l = prime_divisors(n, primes)
 55  local k, v
 56  for k, v in pairs(x) do print(v, primes[k]) end
 57  k = sum(x, 1, l) - n
 58  return k
 59end
 60
 61-- print(test(284))
 62assert(test(284)==220)
 63assert(test(4*9)==91-4*9)
 64assert(test(6)==1+2+3)
 65assert(test(12)==1+2+3+4+6)
 66assert(test(2)==1)
 67assert(test(4)==3)
 68assert(test(220)==284)
 69
 70assert(is_perfect(28))
 71assert(is_abundant(12))
 72assert(not is_abundant(28))
 73
 74local i
 75local a, abundant = 0, {}
 76for i=12,last do
 77  if is_abundant(i) then
 78    a = a + 1
 79    abundant[a] = i
 80  end
 81end
 82
 83-- for k, v in ipairs(abundant) do print(v) end
 84
 85local sum = 0
 86for i=1,last do
 87  local k, v
 88  local found = false
 89  for k, v in ipairs(abundant) do
 90    local r = i-v
 91    if r<v then break end
 92    if is_abundant(r) then 
 93      found = true
 94      break
 95    end
 96  end
 97  if not found then
 98    sum = sum + i
 99  end
100end
101print(sum)