euler/euler.rb

54 lines
862 B
Ruby

require 'prime'
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
class << self
def from_digit_list(list)
list.join('').to_i
end
def generate_pythagorean_triples(upper_bound)
[].tap do |triples|
(2..upper_bound).each do |a|
(a..upper_bound).each do |b|
c = Math.sqrt(a**2 + b**2)
triples << [a, b, c.to_i] if c % 1 == 0
end
end
end
end
def palindrome?(n)
num = n.to_s
(0..(num.length-1)/2).each do |i|
if !(num[i] == num[num.length-1-i])
return false
end
end
true
end
end
end