/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. require "euler"
  9. local last = 28123
  10. primes, prime_table = sieve_primes(last)
  11. function sum(t, b, e)
  12. local s = 1
  13. local i
  14. for i = b, e do
  15. local p = primes[i]
  16. local x = t[i]
  17. local m = 1
  18. for k = 1, x do
  19. m = m + math.pow(p, k)
  20. end
  21. s = s * m
  22. end
  23. return s
  24. end
  25. abundance = {}
  26. function is_abundant(i)
  27. local x = abundance[i]
  28. if x==nil then
  29. local d, n = prime_divisors(i, primes)
  30. x = sum(d, 1, n) - i
  31. abundance[i] = x
  32. end
  33. -- print(i, x)
  34. return x>i
  35. end
  36. function is_perfect(i)
  37. local x = abundance[i]
  38. if x==nil then
  39. local d, n = prime_divisors(i, primes)
  40. x = sum(d, 1, n) - i
  41. abundance[i] = x
  42. end
  43. -- print(i, x)
  44. return x==i
  45. end
  46. function test(n)
  47. local x, l = prime_divisors(n, primes)
  48. local k, v
  49. for k, v in pairs(x) do print(v, primes[k]) end
  50. k = sum(x, 1, l) - n
  51. return k
  52. end
  53. -- print(test(284))
  54. assert(test(284)==220)
  55. assert(test(4*9)==91-4*9)
  56. assert(test(6)==1+2+3)
  57. assert(test(12)==1+2+3+4+6)
  58. assert(test(2)==1)
  59. assert(test(4)==3)
  60. assert(test(220)==284)
  61. assert(is_perfect(28))
  62. assert(is_abundant(12))
  63. assert(not is_abundant(28))
  64. local i
  65. local a, abundant = 0, {}
  66. for i=12,last do
  67. if is_abundant(i) then
  68. a = a + 1
  69. abundant[a] = i
  70. end
  71. end
  72. -- for k, v in ipairs(abundant) do print(v) end
  73. local sum = 0
  74. for i=1,last do
  75. local k, v
  76. local found = false
  77. for k, v in ipairs(abundant) do
  78. local r = i-v
  79. if r<v then break end
  80. if is_abundant(r) then
  81. found = true
  82. break
  83. end
  84. end
  85. if not found then
  86. sum = sum + i
  87. end
  88. end
  89. print(sum)