adds solutions for problems 38 and 39
parent
3c91e1fb8f
commit
22af3e98b1
|
@ -0,0 +1,22 @@
|
|||
def pandigital?(n)
|
||||
n.to_s.split('').map(&:to_i).sort == [1,2,3,4,5,6,7,8,9]
|
||||
end
|
||||
|
||||
def largest_pandigital_composition_up_to(n)
|
||||
largest_pandigital = 1
|
||||
|
||||
(2..n).each do |i|
|
||||
digit_list = []
|
||||
multiplier = 1
|
||||
until digit_list.length >= 9
|
||||
digit_list += (i * multiplier).to_s.split('').map(&:to_i)
|
||||
multiplier += 1
|
||||
end
|
||||
number = digit_list.join('').to_i
|
||||
if pandigital?(number)
|
||||
largest_pandigital = number if number > largest_pandigital
|
||||
end
|
||||
end
|
||||
|
||||
largest_pandigital
|
||||
end
|
|
@ -0,0 +1,31 @@
|
|||
def is_right_triangle?(a, b, c)
|
||||
a ** 2 + b ** 2 == c ** 2
|
||||
end
|
||||
|
||||
def generate_triangle_sides(perimeter)
|
||||
triangles = []
|
||||
(1..perimeter).each do |i|
|
||||
(i..perimeter-i).each do |j|
|
||||
triangles << [i, j, perimeter-i-j]
|
||||
end
|
||||
end
|
||||
triangles
|
||||
end
|
||||
|
||||
def integer_right_triangle_solutions(perimeter)
|
||||
triangles = generate_triangle_sides(perimeter)
|
||||
triangles.select { |x| is_right_triangle?(x[0], x[1], x[2]) }
|
||||
end
|
||||
|
||||
def p_with_max_solution_count_up_to(n)
|
||||
max_solution_count = 0
|
||||
p = 2
|
||||
(3..n).each do |i|
|
||||
solution_count = integer_right_triangle_solutions(i).count
|
||||
if solution_count > max_solution_count
|
||||
max_solution_count = solution_count
|
||||
p = i
|
||||
end
|
||||
end
|
||||
p
|
||||
end
|
Loading…
Reference in New Issue