From 22af3e98b152231d3ee8e5f2fcac75fd6894b6ae Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Fri, 14 Nov 2014 02:13:22 -0800 Subject: [PATCH] adds solutions for problems 38 and 39 --- euler038.rb | 22 ++++++++++++++++++++++ euler039.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 euler038.rb create mode 100644 euler039.rb diff --git a/euler038.rb b/euler038.rb new file mode 100644 index 0000000..8f9784a --- /dev/null +++ b/euler038.rb @@ -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 diff --git a/euler039.rb b/euler039.rb new file mode 100644 index 0000000..a4a5de0 --- /dev/null +++ b/euler039.rb @@ -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