Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use classes

  • Loading branch information...
commit eb702da9f28306a0b647befe3c77ca24f5807f64 1 parent edcc0d3
@mconigliaro authored
Showing with 842 additions and 643 deletions.
  1. +5 −4 Rakefile
  2. +23 −5 lib/project-euler.rb
  3. +11 −0 lib/project-euler/problems/001.rb
  4. +15 −0 lib/project-euler/problems/002.rb
  5. +16 −0 lib/project-euler/problems/003.rb
  6. +19 −0 lib/project-euler/problems/004.rb
  7. +25 −0 lib/project-euler/problems/005.rb
  8. +12 −0 lib/project-euler/problems/006.rb
  9. +11 −0 lib/project-euler/problems/007.rb
  10. +20 −0 lib/project-euler/problems/008.rb
  11. +19 −0 lib/project-euler/problems/009.rb
  12. +11 −0 lib/project-euler/problems/010.rb
  13. +30 −0 lib/project-euler/problems/011.rb
  14. +18 −0 lib/project-euler/problems/012.rb
  15. +11 −0 lib/project-euler/problems/013.rb
  16. +40 −0 lib/project-euler/problems/014.rb
  17. +11 −0 lib/project-euler/problems/015.rb
  18. +11 −0 lib/project-euler/problems/016.rb
  19. +44 −0 lib/project-euler/problems/017.rb
  20. +13 −0 lib/project-euler/problems/019.rb
  21. +11 −0 lib/project-euler/problems/020.rb
  22. +19 −0 lib/project-euler/problems/021.rb
  23. +13 −0 lib/project-euler/problems/022.rb
  24. +11 −0 lib/project-euler/problems/024.rb
  25. +15 −0 lib/project-euler/problems/025.rb
  26. +14 −0 lib/project-euler/problems/026.rb
  27. +29 −0 lib/project-euler/problems/027.rb
  28. +14 −0 lib/project-euler/problems/028.rb
  29. +18 −0 lib/project-euler/problems/029.rb
  30. +14 −0 lib/project-euler/problems/030.rb
  31. +13 −0 lib/project-euler/problems/034.rb
  32. +18 −0 lib/project-euler/problems/035.rb
  33. +18 −0 lib/project-euler/problems/036.rb
  34. +18 −0 lib/project-euler/problems/037.rb
  35. +16 −0 lib/project-euler/problems/040.rb
  36. +16 −0 lib/project-euler/problems/041.rb
  37. +11 −0 lib/project-euler/problems/042.rb
  38. +18 −0 lib/project-euler/problems/043.rb
  39. +23 −0 lib/project-euler/problems/044.rb
  40. +19 −0 lib/project-euler/problems/045.rb
  41. +11 −0 lib/project-euler/problems/048.rb
  42. +31 −0 lib/project-euler/problems/049.rb
  43. +32 −0 lib/project-euler/problems/050.rb
  44. +16 −0 lib/project-euler/problems/052.rb
  45. +19 −0 lib/project-euler/problems/056.rb
  46. +20 −0 lib/project-euler/problems/062.rb
  47. +20 −0 lib/project-euler/problems/063.rb
  48. +0 −7 lib/project-euler/solutions/001.rb
  49. +0 −11 lib/project-euler/solutions/002.rb
  50. +0 −12 lib/project-euler/solutions/003.rb
  51. +0 −15 lib/project-euler/solutions/004.rb
  52. +0 −21 lib/project-euler/solutions/005.rb
  53. +0 −8 lib/project-euler/solutions/006.rb
  54. +0 −7 lib/project-euler/solutions/007.rb
  55. +0 −16 lib/project-euler/solutions/008.rb
  56. +0 −15 lib/project-euler/solutions/009.rb
  57. +0 −7 lib/project-euler/solutions/010.rb
  58. +0 −26 lib/project-euler/solutions/011.rb
  59. +0 −14 lib/project-euler/solutions/012.rb
  60. +0 −7 lib/project-euler/solutions/013.rb
  61. +0 −36 lib/project-euler/solutions/014.rb
  62. +0 −7 lib/project-euler/solutions/015.rb
  63. +0 −7 lib/project-euler/solutions/016.rb
  64. +0 −41 lib/project-euler/solutions/017.rb
  65. +0 −9 lib/project-euler/solutions/019.rb
  66. +0 −7 lib/project-euler/solutions/020.rb
  67. +0 −15 lib/project-euler/solutions/021.rb
  68. +0 −9 lib/project-euler/solutions/022.rb
  69. +0 −7 lib/project-euler/solutions/024.rb
  70. +0 −11 lib/project-euler/solutions/025.rb
  71. +0 −10 lib/project-euler/solutions/026.rb
  72. +0 −25 lib/project-euler/solutions/027.rb
  73. +0 −10 lib/project-euler/solutions/028.rb
  74. +0 −14 lib/project-euler/solutions/029.rb
  75. +0 −10 lib/project-euler/solutions/030.rb
  76. +0 −9 lib/project-euler/solutions/034.rb
  77. +0 −14 lib/project-euler/solutions/035.rb
  78. +0 −14 lib/project-euler/solutions/036.rb
  79. +0 −14 lib/project-euler/solutions/037.rb
  80. +0 −12 lib/project-euler/solutions/040.rb
  81. +0 −12 lib/project-euler/solutions/041.rb
  82. +0 −7 lib/project-euler/solutions/042.rb
  83. +0 −14 lib/project-euler/solutions/043.rb
  84. +0 −19 lib/project-euler/solutions/044.rb
  85. +0 −15 lib/project-euler/solutions/045.rb
  86. +0 −7 lib/project-euler/solutions/048.rb
  87. +0 −27 lib/project-euler/solutions/049.rb
  88. +0 −28 lib/project-euler/solutions/050.rb
  89. +0 −12 lib/project-euler/solutions/052.rb
  90. +0 −14 lib/project-euler/solutions/056.rb
  91. +0 −16 lib/project-euler/solutions/062.rb
  92. +0 −16 lib/project-euler/solutions/063.rb
View
9 Rakefile
@@ -19,8 +19,8 @@ task :solve, :problem_id do |t,args|
case
when args.problem_id.nil?
- private_methods.select { |obj|
- obj =~ /^(problem|answer)_\d+/
+ ProjectEuler.constants.select { |obj|
+ obj =~ /^Problem\d+/
}.inject([]) { |memo,obj|
memo + [/\d+/.match(obj)[0].to_i]
}.sort
@@ -30,7 +30,7 @@ task :solve, :problem_id do |t,args|
when args.problem_id == "latest"
[File.basename(Dir[File.expand_path(File.join(File.dirname(__FILE__),
- "lib", "project-euler", "solutions", "*.rb"))].max { |x,y|
+ "lib", "project-euler", "problems", "*.rb"))].max { |x,y|
File.mtime(x) <=> File.mtime(y)
}, ".rb").to_i]
@@ -39,7 +39,8 @@ task :solve, :problem_id do |t,args|
results = []
times << Benchmark.realtime do
- results = [send("solution_#{problem_id}"), send("answer_#{problem_id}")]
+ c = ProjectEuler::const_get("Problem#{problem_id}").new
+ results = [ c.solve, c.answer ]
end
printf("%-16s%s\n", sprintf("%s%s", results.first, results.first != results.last ? "(!)" : nil),
View
28 lib/project-euler.rb
@@ -6,12 +6,30 @@
require "date"
require "pp"
-[["project-euler", "*.rb"], ["project-euler", "solutions", "*.rb"]].each do |path|
- Dir[File.expand_path(File.join(File.dirname(__FILE__), *path))].each do |file|
- require file
+module ProjectEuler
+
+ class AbstractProblem
+
+ attr_reader :answer
+
+ def initialize
+ @answer = nil
+ end
+
+ def solve
+ raise NotImplementedError.new("#{self.class.name}##{__method__} is not implemented.")
+ end
+
+ def data
+ File.open(File.join(File.dirname(__FILE__), "project-euler", "data", "%03d.txt" % (/\d+$/.match(self.class.name)[0])), "r")
+ end
+
end
+
end
-def open_data(file)
- File.open(File.join(File.dirname(__FILE__), "project-euler", "data", file), "r")
+[["project-euler", "*.rb"], ["project-euler", "problems", "*.rb"]].each do |path|
+ Dir[File.expand_path(File.join(File.dirname(__FILE__), *path))].each do |file|
+ require file
+ end
end
View
11 lib/project-euler/problems/001.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem1 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 233168
+ end
+
+ def solve
+ (1...1000).select { |obj| obj % 3 == 0 || obj % 5 == 0 }.inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
15 lib/project-euler/problems/002.rb
@@ -0,0 +1,15 @@
+class ProjectEuler::Problem2 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 4613732
+ end
+
+ def solve
+ fib = [1, 2]
+ until fib[-1] > 4000000
+ fib << fib[-2] + fib[-1]
+ end
+ fib[0..-1].select(&:even?).inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
16 lib/project-euler/problems/003.rb
@@ -0,0 +1,16 @@
+class ProjectEuler::Problem3 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 6857
+ end
+
+ def solve
+ n = 600851475143
+ primes = primes_up_to(n**0.5)
+ until n.prime?
+ n = n / primes.find { |i| n % i == 0 }
+ end
+ n
+ end
+
+end
View
19 lib/project-euler/problems/004.rb
@@ -0,0 +1,19 @@
+class ProjectEuler::Problem4 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 906609
+ end
+
+ def solve
+ range = (100..999)
+ max = 0
+ range.each do |i|
+ range.each do |j|
+ p = i * j
+ max = p if p > max && p.to_s == p.to_s.reverse
+ end
+ end
+ max
+ end
+
+end
View
25 lib/project-euler/problems/005.rb
@@ -0,0 +1,25 @@
+class ProjectEuler::Problem5 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 232792560
+ end
+
+ def solve
+ divisors = (11..19)
+ gcd = divisors.max
+ n = gcd + 1
+ found = nil
+ while found.nil?
+ divisors.each do |d|
+ if n % d != 0
+ n += gcd + 1
+ break
+ elsif d == gcd
+ found = n
+ end
+ end
+ end
+ found
+ end
+
+end
View
12 lib/project-euler/problems/006.rb
@@ -0,0 +1,12 @@
+class ProjectEuler::Problem6 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 25164150
+ end
+
+ def solve
+ range = (1..100)
+ range.inject(0) { |memo,obj| memo + obj } ** 2 - range.inject(0) { |memo,obj| memo + obj ** 2 }
+ end
+
+end
View
11 lib/project-euler/problems/007.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem7 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 104743
+ end
+
+ def solve
+ 10001.primes.last
+ end
+
+end
View
20 lib/project-euler/problems/008.rb
@@ -0,0 +1,20 @@
+class ProjectEuler::Problem8 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 40824
+ end
+
+ def solve
+ n = data.read.gsub("\n", "")
+ i = 0
+ chunk = 5
+ max = 0
+ while i + chunk <= n.length
+ sum = n[i,chunk].chars.inject(1) { |memo,obj| memo * obj.to_i }
+ max = sum if sum > max
+ i += 1
+ end
+ max
+ end
+
+end
View
19 lib/project-euler/problems/009.rb
@@ -0,0 +1,19 @@
+class ProjectEuler::Problem9 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 31875000
+ end
+
+ def solve
+ n = 1000
+ (3..n).each do |i|
+ (4..n).each do |j|
+ a = j ** 2 - i ** 2
+ b = 2 * i * j
+ c = i ** 2 + j ** 2
+ return a * b * c if a + b + c == n
+ end
+ end
+ end
+
+end
View
11 lib/project-euler/problems/010.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem10 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 142913828922
+ end
+
+ def solve
+ primes_up_to(2000000).inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
30 lib/project-euler/problems/011.rb
@@ -0,0 +1,30 @@
+class ProjectEuler::Problem11 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 70600674
+ end
+
+ def solve
+ grid = data.readlines.inject([]) { |memo,obj| memo << obj.split.map(&:to_i) }
+ chunk = 4
+ max = 0
+ (0...grid.length).each do |i|
+ (0...grid.first.length).each do |j|
+
+ l2r = dl2r = t2b = dr2l = 1
+ (0...chunk).each do |k|
+ l2r *= grid[i][j+k] || 0 rescue 0
+ dl2r *= grid[i+k][j+k] || 0 rescue 0
+ t2b *= grid[i+k][j] || 0 rescue 0
+ dr2l *= grid[i+k][j-k] || 0 rescue 0
+ end
+ [l2r, dl2r, t2b, dr2l].each do |p|
+ max = p if p > max
+ end
+
+ end
+ end
+ max
+ end
+
+end
View
18 lib/project-euler/problems/012.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem12 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 76576500
+ end
+
+ def solve
+ target = 500
+ found = nil
+ while found.nil?
+ i = i + 1 rescue 1
+ sum = sum + i rescue 1
+ found = sum if sum.divisor_count > target
+ end
+ found
+ end
+
+end
View
11 lib/project-euler/problems/013.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem13 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 5537376230
+ end
+
+ def solve
+ data.readlines.map(&:to_i).inject(0) { |memo,obj| memo + obj }.to_s[0,10].to_i
+ end
+
+end
View
40 lib/project-euler/problems/014.rb
@@ -0,0 +1,40 @@
+class ProjectEuler::Problem14 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 837799
+ end
+
+ def solve
+ memo = {}
+ max_start = nil
+ max_len = 0
+
+ (1..1000000).each do |i|
+ cur_last = i
+ cur_len = 1
+
+ while cur_last > 1
+ if memo.has_key?(cur_last)
+ memo[i] = cur_len = memo[cur_last] + cur_len
+ cur_last = 1
+ else
+ if cur_last.even?
+ cur_last = cur_last / 2
+ else
+ cur_last = 3 * cur_last + 1
+ end
+ memo[i] = cur_len
+ cur_len += 1
+ end
+ end
+
+ if cur_len > max_len
+ max_start = i
+ max_len = cur_len
+ end
+ end
+
+ max_start
+ end
+
+end
View
11 lib/project-euler/problems/015.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem15 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 137846528820
+ end
+
+ def solve
+ PascalsTriangle.get_cell(20*2, 20)
+ end
+
+end
View
11 lib/project-euler/problems/016.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem16 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 1366
+ end
+
+ def solve
+ (2**1000).to_s.chars.inject(0) { |memo,obj| memo + obj.to_i }
+ end
+
+end
View
44 lib/project-euler/problems/017.rb
@@ -0,0 +1,44 @@
+class ProjectEuler::Problem17 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 21124
+ end
+
+ def solve
+ numbers = {
+ :digits => [ nil, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ],
+ :teens => [ nil, "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ],
+ :tens => [ nil, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ]
+ }
+
+ all_words_length = 0
+ (1..1000).each do |i|
+
+ words = []
+ num = i
+
+ if i == 1000
+ words << "one thousand"
+ else
+ if (100..999).include?(num)
+ words << "#{numbers[:digits][num / 100]} hundred"
+ num = num % 100
+ end
+
+ if (1..9).include?(num)
+ words << numbers[:digits][num]
+ elsif 10.step(90,10).include?(num)
+ words << numbers[:tens][num / 10]
+ elsif (11..19).include?(num)
+ words << numbers[:teens][num % 10]
+ elsif (21..99).include?(num)
+ words << "#{numbers[:tens][num / 10]} #{numbers[:digits][num % 10]}"
+ end
+ end
+
+ all_words_length += words.join(" and ").gsub(" ", "").length
+ end
+ all_words_length
+ end
+
+end
View
13 lib/project-euler/problems/019.rb
@@ -0,0 +1,13 @@
+class ProjectEuler::Problem19 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 171
+ end
+
+ def solve
+ (Date.parse("1 Jan 1901") .. Date.parse("31 Dec 2000")).step(7).inject(0) do |memo,obj|
+ obj.mday == 1 ? memo + 1 : memo
+ end
+ end
+
+end
View
11 lib/project-euler/problems/020.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem20 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 648
+ end
+
+ def solve
+ (1..100).inject(1) { |memo,obj| memo * obj }.to_s.chars.inject(0) { |memo,obj| memo + obj.to_i }
+ end
+
+end
View
19 lib/project-euler/problems/021.rb
@@ -0,0 +1,19 @@
+class ProjectEuler::Problem21 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 31626
+ end
+
+ def solve
+ pairs = []
+ (1...10000).each do |i|
+ a = i.positive_proper_divisors.inject(0) { |memo,obj| memo + obj }
+ if a > 0
+ b = a.positive_proper_divisors.inject(0) { |memo,obj| memo + obj }
+ pairs += [a, b] if b == i && a != b
+ end
+ end
+ pairs.uniq.inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
13 lib/project-euler/problems/022.rb
@@ -0,0 +1,13 @@
+class ProjectEuler::Problem22 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 871198282
+ end
+
+ def solve
+ data.read.gsub('"', "").split(",").sort.inject([]) do |scores,name|
+ scores << name.value * (scores.length + 1)
+ end.inject(0) { |total,score| total + score }
+ end
+
+end
View
11 lib/project-euler/problems/024.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem24 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 2783915460
+ end
+
+ def solve
+ (0..9).to_a.permutation.sort.to_a[999999].join.to_i
+ end
+
+end
View
15 lib/project-euler/problems/025.rb
@@ -0,0 +1,15 @@
+class ProjectEuler::Problem25 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 4782
+ end
+
+ def solve
+ fib = [1, 2]
+ until fib.last.to_s.length == 1000
+ fib << fib[-2] + fib[-1]
+ end
+ fib.length + 1
+ end
+
+end
View
14 lib/project-euler/problems/026.rb
@@ -0,0 +1,14 @@
+class ProjectEuler::Problem26 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 983
+ end
+
+ def solve
+ precision = 2000
+ primes_up_to(1000).max_by do |obj|
+ /^0\.\d*(\d{7,}?)\1+\d*?$/.match(BigDecimal.new("1").div(obj, precision).to_s("F"))[1].length rescue 0
+ end
+ end
+
+end
View
29 lib/project-euler/problems/027.rb
@@ -0,0 +1,29 @@
+class ProjectEuler::Problem27 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = -59231
+ end
+
+ def solve
+ max = {
+ :coefficients => [],
+ :count => 0
+ }
+ (-1000..1000).each do |i|
+ (-1000..1000).each do |j|
+ n = 0
+ while (n**2 + i * n + j).prime?
+ n += 1
+ end
+ if n > max[:count]
+ max = {
+ :coefficients => [i, j],
+ :count => n
+ }
+ end
+ end
+ end
+ max[:coefficients].inject(1) { |memo,obj| memo * obj }
+ end
+
+end
View
14 lib/project-euler/problems/028.rb
@@ -0,0 +1,14 @@
+class ProjectEuler::Problem28 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 669171001
+ end
+
+ def solve
+ width = 1001
+ s = Spiral.new
+ s.add_layers(width - 1)
+ width.times.inject(0) { |memo,obj| memo += s.spiral[obj][obj] + s.spiral[width - 1 - obj][obj] } - 1
+ end
+
+end
View
18 lib/project-euler/problems/029.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem29 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 9183
+ end
+
+ def solve
+ range = (2..100)
+ terms = []
+ range.each do |i|
+ range.each do |j|
+ terms << i ** j
+ end
+ end
+ terms.sort.uniq.count
+ end
+
+end
View
14 lib/project-euler/problems/030.rb
@@ -0,0 +1,14 @@
+class ProjectEuler::Problem30 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 443839
+ end
+
+ def solve
+ pow = 5
+ (1..(9**pow)*9).select do |i|
+ i > 1 && i == i.digits.inject(0) { |memo,obj| memo + obj ** pow }
+ end.inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
13 lib/project-euler/problems/034.rb
@@ -0,0 +1,13 @@
+class ProjectEuler::Problem34 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 40730
+ end
+
+ def solve
+ (3..50000).select do |i|
+ i == i.digits.inject(0) { |memo,obj| memo + obj.factorial }
+ end.inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
18 lib/project-euler/problems/035.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem35 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 55
+ end
+
+ def solve
+ cprimes = 0
+ primes_up_to(1000000).each do |i|
+ digits = i.digits
+ cprimes +=1 if digits.length == 1 || (0...digits.length-1).all? do
+ (digits << digits.shift).join.to_i.prime?
+ end
+ end
+ cprimes
+ end
+
+end
View
18 lib/project-euler/problems/036.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem36 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 872187
+ end
+
+ def solve
+ sum = 0
+ range = (1..1000000)
+ range.each do |i|
+ dec = i.to_s
+ bin = i.to_s(2)
+ sum += i if dec == dec.reverse && bin == bin.reverse
+ end
+ sum
+ end
+
+end
View
18 lib/project-euler/problems/037.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem37 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 748317
+ end
+
+ def solve
+ t_primes = []
+ Prime.each { |prime|
+ t_primes << prime if prime >= 11 && prime.to_s.length.times.all? { |n|
+ prime.to_s[0 .. (n + 1) * -1].to_i.prime? && prime.to_s[n .. -1].to_i.prime?
+ }
+ break if t_primes.length == 11
+ }
+ t_primes.inject(0) { |memo,obj| memo + obj }
+ end
+
+end
View
16 lib/project-euler/problems/040.rb
@@ -0,0 +1,16 @@
+class ProjectEuler::Problem40 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 210
+ end
+
+ def solve
+ digits = [1, 10, 100, 1000, 10000, 100000, 1000000]
+ digit_str = ""
+ while digit_str.length < digits.last
+ digit_str << (n = n + 1 rescue 1).to_s
+ end
+ digits.inject(1) { |memo,obj| memo * digit_str[obj - 1, 1].to_i }
+ end
+
+end
View
16 lib/project-euler/problems/041.rb
@@ -0,0 +1,16 @@
+class ProjectEuler::Problem41 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 7652413
+ end
+
+ def solve
+ 9.downto(1).each do |i|
+ i.downto(1).to_a.permutation.sort.reverse.each do |j|
+ n = j.join.to_i
+ return n if n.prime?
+ end
+ end
+ end
+
+end
View
11 lib/project-euler/problems/042.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem42 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 162
+ end
+
+ def solve
+ data.read.gsub('"', "").split(",").select { |word| word.value.triangular? }.length
+ end
+
+end
View
18 lib/project-euler/problems/043.rb
@@ -0,0 +1,18 @@
+class ProjectEuler::Problem43 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 16695334890
+ end
+
+ def solve
+ primes = 7.primes
+ (0..9).to_a.permutation.select { |n|
+ (1..7).all? { |i|
+ n[i..i+2].join.to_i % primes[i - 1] == 0
+ }
+ }.inject(0) { |memo,obj|
+ memo + obj.join.to_i
+ }
+ end
+
+end
View
23 lib/project-euler/problems/044.rb
@@ -0,0 +1,23 @@
+class ProjectEuler::Problem44 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 5482660
+ end
+
+ def solve
+ found = false
+ i = 1
+ ps = []
+ until found
+ p1 = pentagonal(i)
+ if p2 = ps.find { |p2| (p1 - p2).pentagonal? && (p1 + p2).pentagonal? }
+ found = p1 - p2
+ else
+ ps << p1
+ i += 1
+ end
+ end
+ found
+ end
+
+end
View
19 lib/project-euler/problems/045.rb
@@ -0,0 +1,19 @@
+class ProjectEuler::Problem45 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 1533776805
+ end
+
+ def solve
+ h = []
+ found = nil
+ while found.nil?
+ i = i + 1 rescue 144
+ p = pentagonal(i)
+ h << hexagonal(i)
+ found = p if h.include?(p)
+ end
+ found
+ end
+
+end
View
11 lib/project-euler/problems/048.rb
@@ -0,0 +1,11 @@
+class ProjectEuler::Problem48 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 9110846700
+ end
+
+ def solve
+ (1..1000).inject(0) { |memo,obj| memo + obj ** obj }.to_s[-10,10].to_i
+ end
+
+end
View
31 lib/project-euler/problems/049.rb
@@ -0,0 +1,31 @@
+class ProjectEuler::Problem49 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 296962999629
+ end
+
+ def solve
+ primes_up_to(9999).select { |i|
+ i >= 1000 && ![1487, 4817, 8147].include?(i)
+ }.inject(Hash.new({})) { |memo,obj|
+ key = obj.digits.sort
+ memo[key] = memo[key] + [obj] rescue [obj]
+ memo
+ }.inject([]) { |memo,obj|
+ memo + [obj.last]
+ }.select { |obj|
+ obj.length >= 3
+ }.inject([]) { |memo,obj|
+ while obj.length >= 3 && memo.length != 3
+ if obj[-1] - obj[-2] == obj[-2] - obj[-3]
+ memo = obj[-3,3]
+ else
+ obj.pop
+ memo = []
+ end
+ end
+ memo
+ }.inject("") { |memo,obj| memo + obj.to_s }.to_i
+ end
+
+end
View
32 lib/project-euler/problems/050.rb
@@ -0,0 +1,32 @@
+class ProjectEuler::Problem50 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 997651
+ end
+
+ def solve
+ max = 1000000
+ primes = primes_up_to(max)
+ primes_length = primes.length
+
+ last = 0
+ sum = 0
+ until sum > max
+ last += 1
+ sum = primes[0, last].inject(0) { |memo,obj| memo + obj }
+ end
+
+ sum = 0
+ until sum.prime?
+ offset = 0
+ until offset + last > primes_length - 1 || sum.prime?
+ sum = primes[offset...last].inject(0) { |memo,obj| memo + obj }
+ sum > max ? break : offset += 1
+ end
+ last -= 1
+ end
+
+ sum
+ end
+
+end
View
16 lib/project-euler/problems/052.rb
@@ -0,0 +1,16 @@
+class ProjectEuler::Problem52 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 142857
+ end
+
+ def solve
+ found = nil
+ while found.nil?
+ i = i + 1 rescue 1
+ found = i if (2..6).all? { |j| (i * j).digits.sort == i.digits.sort }
+ end
+ found
+ end
+
+end
View
19 lib/project-euler/problems/056.rb
@@ -0,0 +1,19 @@
+class ProjectEuler::Problem56 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 972
+ end
+
+
+ def solve
+ max = 0
+ (1..99).each do |i|
+ (1..99).each do |j|
+ sum = (i ** j).digits.inject(0) { |memo,obj| memo + obj }
+ max = sum if sum > max
+ end
+ end
+ max
+ end
+
+end
View
20 lib/project-euler/problems/062.rb
@@ -0,0 +1,20 @@
+class ProjectEuler::Problem62 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 127035954683
+ end
+
+ def solve
+ n = 5
+ squares = Hash.new([])
+ found = found
+ until found
+ cube = cubic((i = i + 1 rescue 1))
+ key = cube.digits.sort
+ squares[key] += [cube]
+ found = squares[key].min if squares[key].length == n
+ end
+ found
+ end
+
+end
View
20 lib/project-euler/problems/063.rb
@@ -0,0 +1,20 @@
+class ProjectEuler::Problem63 < ProjectEuler::AbstractProblem
+
+ def initialize
+ @answer = 49
+ end
+
+ def solve
+ numbers = []
+ pow = 1
+ while pow < 22 # FIXME
+ until (n = (i = i + 1 rescue 1) ** pow).digits.length > pow
+ numbers << n if n.digits.length == pow
+ end
+ i = 0
+ pow += 1
+ end
+ numbers.count
+ end
+
+end
View
7 lib/project-euler/solutions/001.rb
@@ -1,7 +0,0 @@
-def solution_1
- (1...1000).select { |obj| obj % 3 == 0 || obj % 5 == 0 }.inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_1
- 233168
-end
View
11 lib/project-euler/solutions/002.rb
@@ -1,11 +0,0 @@
-def solution_2
- fib = [1, 2]
- until fib[-1] > 4000000
- fib << fib[-2] + fib[-1]
- end
- fib[0..-1].select(&:even?).inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_2
- 4613732
-end
View
12 lib/project-euler/solutions/003.rb
@@ -1,12 +0,0 @@
-def solution_3
- n = 600851475143
- primes = primes_up_to(n**0.5)
- until n.prime?
- n = n / primes.find { |i| n % i == 0 }
- end
- n
-end
-
-def answer_3
- 6857
-end
View
15 lib/project-euler/solutions/004.rb
@@ -1,15 +0,0 @@
-def solution_4
- range = (100..999)
- max = 0
- range.each do |i|
- range.each do |j|
- p = i * j
- max = p if p > max && p.to_s == p.to_s.reverse
- end
- end
- max
-end
-
-def answer_4
- 906609
-end
View
21 lib/project-euler/solutions/005.rb
@@ -1,21 +0,0 @@
-def solution_5
- divisors = (11..19)
- gcd = divisors.max
- n = gcd + 1
- found = nil
- while found.nil?
- divisors.each do |d|
- if n % d != 0
- n += gcd + 1
- break
- elsif d == gcd
- found = n
- end
- end
- end
- found
-end
-
-def answer_5
- 232792560
-end
View
8 lib/project-euler/solutions/006.rb
@@ -1,8 +0,0 @@
-def solution_6
- range = (1..100)
- range.inject(0) { |memo,obj| memo + obj } ** 2 - range.inject(0) { |memo,obj| memo + obj ** 2 }
-end
-
-def answer_6
- 25164150
-end
View
7 lib/project-euler/solutions/007.rb
@@ -1,7 +0,0 @@
-def solution_7
- 10001.primes.last
-end
-
-def answer_7
- 104743
-end
View
16 lib/project-euler/solutions/008.rb
@@ -1,16 +0,0 @@
-def solution_8
- n = open_data("008.txt").read.gsub("\n", "")
- i = 0
- chunk = 5
- max = 0
- while i + chunk <= n.length
- sum = n[i,chunk].chars.inject(1) { |memo,obj| memo * obj.to_i }
- max = sum if sum > max
- i += 1
- end
- max
-end
-
-def answer_8
- 40824
-end
View
15 lib/project-euler/solutions/009.rb
@@ -1,15 +0,0 @@
-def solution_9
- n = 1000
- (3..n).each do |i|
- (4..n).each do |j|
- a = j ** 2 - i ** 2
- b = 2 * i * j
- c = i ** 2 + j ** 2
- return a * b * c if a + b + c == n
- end
- end
-end
-
-def answer_9
- 31875000
-end
View
7 lib/project-euler/solutions/010.rb
@@ -1,7 +0,0 @@
-def solution_10
- primes_up_to(2000000).inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_10
- 142913828922
-end
View
26 lib/project-euler/solutions/011.rb
@@ -1,26 +0,0 @@
-def solution_11
- grid = open_data("011.txt").readlines.inject([]) { |memo,obj| memo << obj.split.map(&:to_i) }
- chunk = 4
- max = 0
- (0...grid.length).each do |i|
- (0...grid.first.length).each do |j|
-
- l2r = dl2r = t2b = dr2l = 1
- (0...chunk).each do |k|
- l2r *= grid[i][j+k] || 0 rescue 0
- dl2r *= grid[i+k][j+k] || 0 rescue 0
- t2b *= grid[i+k][j] || 0 rescue 0
- dr2l *= grid[i+k][j-k] || 0 rescue 0
- end
- [l2r, dl2r, t2b, dr2l].each do |p|
- max = p if p > max
- end
-
- end
- end
- max
-end
-
-def answer_11
- 70600674
-end
View
14 lib/project-euler/solutions/012.rb
@@ -1,14 +0,0 @@
-def solution_12
- target = 500
- found = nil
- while found.nil?
- i = i + 1 rescue 1
- sum = sum + i rescue 1
- found = sum if sum.divisor_count > target
- end
- found
-end
-
-def answer_12
- 76576500
-end
View
7 lib/project-euler/solutions/013.rb
@@ -1,7 +0,0 @@
-def solution_13
- open_data("013.txt").readlines.map(&:to_i).inject(0) { |memo,obj| memo + obj }.to_s[0,10].to_i
-end
-
-def answer_13
- 5537376230
-end
View
36 lib/project-euler/solutions/014.rb
@@ -1,36 +0,0 @@
-def solution_14
- memo = {}
- max_start = nil
- max_len = 0
-
- (1..1000000).each do |i|
- cur_last = i
- cur_len = 1
-
- while cur_last > 1
- if memo.has_key?(cur_last)
- memo[i] = cur_len = memo[cur_last] + cur_len
- cur_last = 1
- else
- if cur_last.even?
- cur_last = cur_last / 2
- else
- cur_last = 3 * cur_last + 1
- end
- memo[i] = cur_len
- cur_len += 1
- end
- end
-
- if cur_len > max_len
- max_start = i
- max_len = cur_len
- end
- end
-
- max_start
-end
-
-def answer_14
- 837799
-end
View
7 lib/project-euler/solutions/015.rb
@@ -1,7 +0,0 @@
-def solution_15
- PascalsTriangle.get_cell(20*2, 20)
-end
-
-def answer_15
- 137846528820
-end
View
7 lib/project-euler/solutions/016.rb
@@ -1,7 +0,0 @@
-def solution_16
- (2**1000).to_s.chars.inject(0) { |memo,obj| memo + obj.to_i }
-end
-
-def answer_16
- 1366
-end
View
41 lib/project-euler/solutions/017.rb
@@ -1,41 +0,0 @@
-def solution_17
-
- numbers = {
- :digits => [ nil, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ],
- :teens => [ nil, "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ],
- :tens => [ nil, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ]
- }
-
- all_words_length = 0
- (1..1000).each do |i|
-
- words = []
- num = i
-
- if i == 1000
- words << "one thousand"
- else
- if (100..999).include?(num)
- words << "#{numbers[:digits][num / 100]} hundred"
- num = num % 100
- end
-
- if (1..9).include?(num)
- words << numbers[:digits][num]
- elsif 10.step(90,10).include?(num)
- words << numbers[:tens][num / 10]
- elsif (11..19).include?(num)
- words << numbers[:teens][num % 10]
- elsif (21..99).include?(num)
- words << "#{numbers[:tens][num / 10]} #{numbers[:digits][num % 10]}"
- end
- end
-
- all_words_length += words.join(" and ").gsub(" ", "").length
- end
- all_words_length
-end
-
-def answer_17
- 21124
-end
View
9 lib/project-euler/solutions/019.rb
@@ -1,9 +0,0 @@
-def solution_19
- (Date.parse("1 Jan 1901") .. Date.parse("31 Dec 2000")).step(7).inject(0) do |memo,obj|
- obj.mday == 1 ? memo + 1 : memo
- end
-end
-
-def answer_19
- 171
-end
View
7 lib/project-euler/solutions/020.rb
@@ -1,7 +0,0 @@
-def solution_20
- (1..100).inject(1) { |memo,obj| memo * obj }.to_s.chars.inject(0) { |memo,obj| memo + obj.to_i }
-end
-
-def answer_20
- 648
-end
View
15 lib/project-euler/solutions/021.rb
@@ -1,15 +0,0 @@
-def solution_21
- pairs = []
- (1...10000).each do |i|
- a = i.positive_proper_divisors.inject(0) { |memo,obj| memo + obj }
- if a > 0
- b = a.positive_proper_divisors.inject(0) { |memo,obj| memo + obj }
- pairs += [a, b] if b == i && a != b
- end
- end
- pairs.uniq.inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_21
- 31626
-end
View
9 lib/project-euler/solutions/022.rb
@@ -1,9 +0,0 @@
-def solution_22
- open_data("022.txt").read.gsub('"', "").split(",").sort.inject([]) do |scores,name|
- scores << name.value * (scores.length + 1)
- end.inject(0) { |total,score| total + score }
-end
-
-def answer_22
- 871198282
-end
View
7 lib/project-euler/solutions/024.rb
@@ -1,7 +0,0 @@
-def solution_24
- (0..9).to_a.permutation.sort.to_a[999999].join.to_i
-end
-
-def answer_24
- 2783915460
-end
View
11 lib/project-euler/solutions/025.rb
@@ -1,11 +0,0 @@
-def solution_25
- fib = [1, 2]
- until fib.last.to_s.length == 1000
- fib << fib[-2] + fib[-1]
- end
- fib.length + 1
-end
-
-def answer_25
- 4782
-end
View
10 lib/project-euler/solutions/026.rb
@@ -1,10 +0,0 @@
-def solution_26
- precision = 2000
- primes_up_to(1000).max_by do |obj|
- /^0\.\d*(\d{7,}?)\1+\d*?$/.match(BigDecimal.new("1").div(obj, precision).to_s("F"))[1].length rescue 0
- end
-end
-
-def answer_26
- 983
-end
View
25 lib/project-euler/solutions/027.rb
@@ -1,25 +0,0 @@
-def solution_27
- max = {
- :coefficients => [],
- :count => 0
- }
- (-1000..1000).each do |i|
- (-1000..1000).each do |j|
- n = 0
- while (n**2 + i * n + j).prime?
- n += 1
- end
- if n > max[:count]
- max = {
- :coefficients => [i, j],
- :count => n
- }
- end
- end
- end
- max[:coefficients].inject(1) { |memo,obj| memo * obj }
-end
-
-def answer_27
- -59231
-end
View
10 lib/project-euler/solutions/028.rb
@@ -1,10 +0,0 @@
-def solution_28
- width = 1001
- s = Spiral.new
- s.add_layers(width - 1)
- width.times.inject(0) { |memo,obj| memo += s.spiral[obj][obj] + s.spiral[width - 1 - obj][obj] } - 1
-end
-
-def answer_28
- 669171001
-end
View
14 lib/project-euler/solutions/029.rb
@@ -1,14 +0,0 @@
-def solution_29
- range = (2..100)
- terms = []
- range.each do |i|
- range.each do |j|
- terms << i ** j
- end
- end
- terms.sort.uniq.count
-end
-
-def answer_29
- 9183
-end
View
10 lib/project-euler/solutions/030.rb
@@ -1,10 +0,0 @@
-def solution_30
- pow = 5
- (1..(9**pow)*9).select do |i|
- i > 1 && i == i.digits.inject(0) { |memo,obj| memo + obj ** pow }
- end.inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_30
- 443839
-end
View
9 lib/project-euler/solutions/034.rb
@@ -1,9 +0,0 @@
-def solution_34
- (3..50000).select do |i|
- i == i.digits.inject(0) { |memo,obj| memo + obj.factorial }
- end.inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_34
- 40730
-end
View
14 lib/project-euler/solutions/035.rb
@@ -1,14 +0,0 @@
-def solution_35
- cprimes = 0
- primes_up_to(1000000).each do |i|
- digits = i.digits
- cprimes +=1 if digits.length == 1 || (0...digits.length-1).all? do
- (digits << digits.shift).join.to_i.prime?
- end
- end
- cprimes
-end
-
-def answer_35
- 55
-end
View
14 lib/project-euler/solutions/036.rb
@@ -1,14 +0,0 @@
-def solution_36
- sum = 0
- range = (1..1000000)
- range.each do |i|
- dec = i.to_s
- bin = i.to_s(2)
- sum += i if dec == dec.reverse && bin == bin.reverse
- end
- sum
-end
-
-def answer_36
- 872187
-end
View
14 lib/project-euler/solutions/037.rb
@@ -1,14 +0,0 @@
-def solution_37
- t_primes = []
- Prime.each { |prime|
- t_primes << prime if prime >= 11 && prime.to_s.length.times.all? { |n|
- prime.to_s[0 .. (n + 1) * -1].to_i.prime? && prime.to_s[n .. -1].to_i.prime?
- }
- break if t_primes.length == 11
- }
- t_primes.inject(0) { |memo,obj| memo + obj }
-end
-
-def answer_37
- 748317
-end
View
12 lib/project-euler/solutions/040.rb
@@ -1,12 +0,0 @@
-def solution_40
- digits = [1, 10, 100, 1000, 10000, 100000, 1000000]
- digit_str = ""
- while digit_str.length < digits.last
- digit_str << (n = n + 1 rescue 1).to_s
- end
- digits.inject(1) { |memo,obj| memo * digit_str[obj - 1, 1].to_i }
-end
-
-def answer_40
- 210
-end
View
12 lib/project-euler/solutions/041.rb
@@ -1,12 +0,0 @@
-def solution_41
- 9.downto(1).each do |i|
- i.downto(1).to_a.permutation.sort.reverse.each do |j|
- n = j.join.to_i
- return n if n.prime?
- end
- end
-end
-
-def answer_41
- 7652413
-end
View
7 lib/project-euler/solutions/042.rb
@@ -1,7 +0,0 @@
-def solution_42
- open_data("042.txt").read.gsub('"', "").split(",").select { |word| word.value.triangular? }.length
-end
-
-def answer_42
- 162
-end
View
14 lib/project-euler/solutions/043.rb
@@ -1,14 +0,0 @@
-def solution_43
- primes = 7.primes
- (0..9).to_a.permutation.select { |n|
- (1..7).all? { |i|
- n[i..i+2].join.to_i % primes[i - 1] == 0
- }
- }.inject(0) { |memo,obj|
- memo + obj.join.to_i
- }
-end
-
-def answer_43
- 16695334890
-end
View
19 lib/project-euler/solutions/044.rb
@@ -1,19 +0,0 @@
-def solution_44
- found = false
- i = 1
- ps = []
- until found
- p1 = pentagonal(i)
- if p2 = ps.find { |p2| (p1 - p2).pentagonal? && (p1 + p2).pentagonal? }
- found = p1 - p2
- else
- ps << p1
- i += 1
- end
- end
- found
-end
-
-def answer_44
- 5482660
-end
View
15 lib/project-euler/solutions/045.rb
@@ -1,15 +0,0 @@
-def solution_45
- h = []
- found = nil
- while found.nil?
- i = i + 1 rescue 144
- p = pentagonal(i)
- h << hexagonal(i)
- found = p if h.include?(p)
- end
- found
-end
-
-def answer_45
- 1533776805
-end
View
7 lib/project-euler/solutions/048.rb
@@ -1,7 +0,0 @@
-def solution_48
- (1..1000).inject(0) { |memo,obj| memo + obj ** obj }.to_s[-10,10].to_i
-end
-
-def answer_48
- 9110846700
-end
View
27 lib/project-euler/solutions/049.rb
@@ -1,27 +0,0 @@
-def solution_49
- primes_up_to(9999).select { |i|
- i >= 1000 && ![1487, 4817, 8147].include?(i)
- }.inject(Hash.new({})) { |memo,obj|
- key = obj.digits.sort
- memo[key] = memo[key] + [obj] rescue [obj]
- memo
- }.inject([]) { |memo,obj|
- memo + [obj.last]
- }.select { |obj|
- obj.length >= 3
- }.inject([]) { |memo,obj|
- while obj.length >= 3 && memo.length != 3
- if obj[-1] - obj[-2] == obj[-2] - obj[-3]
- memo = obj[-3,3]
- else
- obj.pop
- memo = []
- end
- end
- memo
- }.inject("") { |memo,obj| memo + obj.to_s }.to_i
-end
-
-def answer_49
- 296962999629
-end
View
28 lib/project-euler/solutions/050.rb
@@ -1,28 +0,0 @@
-def solution_50
- max = 1000000
- primes = primes_up_to(max)
- primes_length = primes.length
-
- last = 0
- sum = 0
- until sum > max
- last += 1
- sum = primes[0, last].inject(0) { |memo,obj| memo + obj }
- end
-
- sum = 0
- until sum.prime?
- offset = 0
- until offset + last > primes_length - 1 || sum.prime?
- sum = primes[offset...last].inject(0) { |memo,obj| memo + obj }
- sum > max ? break : offset += 1
- end
- last -= 1
- end
-
- sum
-end
-
-def answer_50
- 997651
-end
View
12 lib/project-euler/solutions/052.rb
@@ -1,12 +0,0 @@
-def solution_52
- found = nil
- while found.nil?
- i = i + 1 rescue 1
- found = i if (2..6).all? { |j| (i * j).digits.sort == i.digits.sort }
- end
- found
-end
-
-def answer_52
- 142857
-end
View
14 lib/project-euler/solutions/056.rb
@@ -1,14 +0,0 @@
-def solution_56
- max = 0
- (1..99).each do |i|
- (1..99).each do |j|
- sum = (i ** j).digits.inject(0) { |memo,obj| memo + obj }
- max = sum if sum > max
- end
- end
- max
-end
-
-def answer_56
- 972
-end
View
16 lib/project-euler/solutions/062.rb
@@ -1,16 +0,0 @@
-def solution_62
- n = 5
- squares = Hash.new([])
- found = found
- until found
- cube = cubic((i = i + 1 rescue 1))
- key = cube.digits.sort
- squares[key] += [cube]
- found = squares[key].min if squares[key].length == n
- end
- found
-end
-
-def answer_62
- 127035954683
-end
View
16 lib/project-euler/solutions/063.rb
@@ -1,16 +0,0 @@
-def solution_63
- numbers = []
- pow = 1
- while pow < 22 # FIXME
- until (n = (i = i + 1 rescue 1) ** pow).digits.length > pow
- numbers << n if n.digits.length == pow
- end
- i = 0
- pow += 1
- end
- numbers.count
-end
-
-def answer_63
- 49
-end
Please sign in to comment.
Something went wrong with that request. Please try again.