tidying up more solutions
parent
b62fdd41ad
commit
b8ab94b533
19
euler.rb
19
euler.rb
|
@ -1,8 +1,19 @@
|
|||
require 'prime'
|
||||
|
||||
class Integer
|
||||
def to_digit_list
|
||||
self.to_s.split('').map(&:to_i)
|
||||
def choose(k)
|
||||
factorial / (k.factorial * (self - k).factorial)
|
||||
end
|
||||
|
||||
def divisors
|
||||
divisors = []
|
||||
(1..Math.sqrt(self).to_i).each do |num|
|
||||
if self % num == 0
|
||||
divisors << num
|
||||
divisors << self / num
|
||||
end
|
||||
end
|
||||
divisors
|
||||
end
|
||||
|
||||
def factorial
|
||||
|
@ -15,8 +26,8 @@ class Integer
|
|||
i
|
||||
end
|
||||
|
||||
def choose(k)
|
||||
factorial / (k.factorial * (self - k).factorial)
|
||||
def to_digit_list
|
||||
self.to_s.split('').map(&:to_i)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
25
euler012.rb
25
euler012.rb
|
@ -1,17 +1,7 @@
|
|||
class Integer
|
||||
def divisors
|
||||
divisors = []
|
||||
(1..Math.sqrt(self).to_i).each do |num|
|
||||
if self % num == 0
|
||||
divisors << num
|
||||
divisors << self / num
|
||||
end
|
||||
end
|
||||
divisors
|
||||
end
|
||||
end
|
||||
require_relative 'euler'
|
||||
|
||||
triangle = Enumerator.new do |y|
|
||||
def triangle_enumerator
|
||||
Enumerator.new do |y|
|
||||
sum = 1
|
||||
next_num = 2
|
||||
loop do
|
||||
|
@ -20,11 +10,8 @@ triangle = Enumerator.new do |y|
|
|||
next_num += 1
|
||||
end
|
||||
end
|
||||
|
||||
val = triangle.next
|
||||
|
||||
while val.divisors.count < 500
|
||||
val = triangle.next
|
||||
end
|
||||
|
||||
puts val
|
||||
def solution
|
||||
triangle_enumerator.take_while { |x| x.divisors.count <= 500 }.last
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
puts (37107287533902102798797998220837590246510135740250 +
|
||||
def solution
|
||||
(37107287533902102798797998220837590246510135740250 +
|
||||
46376937677490009712648124896970078050417018260538 +
|
||||
74324986199524741059474233309513058123726617309629 +
|
||||
91942213363574161572522430563301811072406154908250 +
|
||||
|
@ -98,3 +99,4 @@ puts (37107287533902102798797998220837590246510135740250 +
|
|||
72107838435069186155435662884062257473692284509516 +
|
||||
20849603980134001723930671666823555245252804609722 +
|
||||
53503534226472524250874054075591789781264330331690).to_s[0..9]
|
||||
end
|
||||
|
|
23
euler014.rb
23
euler014.rb
|
@ -1,23 +1,16 @@
|
|||
def collatz(num)
|
||||
list = []
|
||||
current_num = num
|
||||
while current_num > 1
|
||||
list << current_num
|
||||
if current_num % 2 == 0
|
||||
current_num /= 2
|
||||
[].tap do |list|
|
||||
until num == 1
|
||||
if num.even?
|
||||
list << (num = num / 2)
|
||||
else
|
||||
current_num = 3*current_num + 1
|
||||
list << (num = (3 * num) + 1)
|
||||
end
|
||||
end
|
||||
list << 1
|
||||
end
|
||||
|
||||
def longest_chain(num)
|
||||
chain_length = {}
|
||||
(1..num).each do |starting_num|
|
||||
chain_length[starting_num] = collatz(starting_num).count
|
||||
end
|
||||
chain_length.sort_by { |key, value| value }.last
|
||||
end
|
||||
|
||||
puts longest_chain(999999)
|
||||
def solution
|
||||
(1..999999).map { |i| collatz(i).count }.each_with_index.max[1] + 1
|
||||
end
|
||||
|
|
19
euler015.rb
19
euler015.rb
|
@ -1,16 +1,9 @@
|
|||
def factorial(n)
|
||||
i = 1
|
||||
while (n > 0)
|
||||
i *= n
|
||||
n -= 1
|
||||
end
|
||||
i
|
||||
end
|
||||
|
||||
def combination(n, k)
|
||||
factorial(n) / (factorial(k) * factorial(n-k))
|
||||
end
|
||||
require_relative 'euler'
|
||||
|
||||
def lattice_paths(size)
|
||||
combination(2*size, size)
|
||||
(2*size).choose(size)
|
||||
end
|
||||
|
||||
def solution
|
||||
lattice_paths(20)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue