adds two more solutions

master
Evan Hemsley 2014-11-20 13:54:55 -08:00
parent 4ba5651e9d
commit b62fdd41ad
3 changed files with 42 additions and 0 deletions

View File

@ -4,6 +4,20 @@ class Integer
def to_digit_list
self.to_s.split('').map(&:to_i)
end
def factorial
i = 1
n = self
while (n > 0)
i *= n
n -= 1
end
i
end
def choose(k)
factorial / (k.factorial * (self - k).factorial)
end
end
module Euler

11
euler053.rb Normal file
View File

@ -0,0 +1,11 @@
require_relative 'euler'
def solution
result = 0
(1..100).each do |n|
(1..n).each do |k|
result += 1 if n.choose(k) > 1000000
end
end
result
end

17
euler055.rb Normal file
View File

@ -0,0 +1,17 @@
require_relative 'euler'
def reverse_and_add(n)
n + Euler.from_digit_list(n.to_digit_list.reverse)
end
def lychrel_number?(n)
50.times do
n = reverse_and_add(n)
return false if Euler.palindrome?(n)
end
true
end
def solution
(1..10000).count { |x| lychrel_number?(x) }
end