From 786860f59f46f5548d22577e0a661938840a5ff4 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 19 Nov 2014 14:50:42 -0800 Subject: [PATCH] 5 more solutions --- euler047.rb | 5 +++++ euler048.rb | 3 +++ euler049.rb | 23 +++++++++++++++++++++++ euler050.rb | 22 ++++++++++++++++++++++ euler051.rb | 28 ++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 euler047.rb create mode 100644 euler048.rb create mode 100644 euler049.rb create mode 100644 euler050.rb create mode 100644 euler051.rb diff --git a/euler047.rb b/euler047.rb new file mode 100644 index 0000000..3344c30 --- /dev/null +++ b/euler047.rb @@ -0,0 +1,5 @@ +require 'prime' + +def solution_up_to(n) + (1..n).map { |x| Prime.prime_division(x).count }.each_cons(4).find_index { |x| x.all? { |y| y == 4} } + 1 +end diff --git a/euler048.rb b/euler048.rb new file mode 100644 index 0000000..f7b13fa --- /dev/null +++ b/euler048.rb @@ -0,0 +1,3 @@ +def series(n) + (1..n).map { |x| x ** x }.inject(:+) +end diff --git a/euler049.rb b/euler049.rb new file mode 100644 index 0000000..640dcda --- /dev/null +++ b/euler049.rb @@ -0,0 +1,23 @@ +require 'prime' + +def four_digit_increasing_by(n) + return [] if (9999 - (2*n)) < 0 + solutions = [] + (1..(9999 - (2*n))).each do |x| + solutions << [x, x + n, x + (2 * n)] + end + solutions +end + +def to_digit_list(num) + num.to_s.split('').map(&:to_i) +end + +def permutations?(number_list) + comparison_digit_list = to_digit_list(number_list.first).sort + number_list.all? { |x| to_digit_list(x).sort == comparison_digit_list } +end + +def find_prime_permutations(n) + four_digit_increasing_by(n).select { |x| permutations?(x) }.select { |x| x.all? { |y| Prime.prime?(y) } } +end diff --git a/euler050.rb b/euler050.rb new file mode 100644 index 0000000..8dd0062 --- /dev/null +++ b/euler050.rb @@ -0,0 +1,22 @@ +require 'prime' + +def longest_sum_of_consecutive_primes_beneath(n) + sum = 0 + primes = Prime.take_while { |x| (sum += x) < n } + longest_prime_count = 0 + longest_prime_sum = 0 + prime_list = [] + (1..primes.count).each do |x| + primes.each_cons(x) do |y| + subcount = y.inject(:+) + if Prime.prime?(subcount) + if y.count > longest_prime_count + longest_prime_sum = subcount + longest_prime_count = y.count + prime_list = y + end + end + end + end + [prime_list, longest_prime_sum] +end diff --git a/euler051.rb b/euler051.rb new file mode 100644 index 0000000..3e34852 --- /dev/null +++ b/euler051.rb @@ -0,0 +1,28 @@ +def permutations?(number_list) + comparison_digit_list = to_digit_list(number_list.first).sort + number_list.all? { |x| to_digit_list(x).sort == comparison_digit_list } +end + +def six_multiples(x) + [x, 2 * x, 3 * x, 4 * x, 5 * x, 6 * x] +end + +def infinite_series + Enumerator.new do |y| + n = 1 + loop do + y << n + n += 1 + end + end +end + +def same_digit_six_multiples + series = infinite_series + n = series.next + loop do + result = permutations?(six_multiples(n)) + return six_multiples(n) if result + n = series.next + end +end