From 84b7ee2f317c0b924d86b3eba5aeb449fffe9db5 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Tue, 6 Sep 2016 00:08:56 -0700 Subject: [PATCH] problem 51 solution --- ruby/euler.rb | 8 ++++++ ruby/euler051.rb | 73 +++++++++++++++++++++++++++++++++++------------- ruby/euler052.rb | 28 +++++++++++++++++++ 3 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 ruby/euler052.rb diff --git a/ruby/euler.rb b/ruby/euler.rb index 5ecb019..2f72dd6 100644 --- a/ruby/euler.rb +++ b/ruby/euler.rb @@ -64,5 +64,13 @@ module Euler true end + def non_empty_subsets(list) + (1..list.count).map { |x| list.combination(x).to_a }.flatten(1) + end + + def powerset(list) + (0..list.count).map { |x| list.combination(x).to_a }.flatten(1) + end + end end diff --git a/ruby/euler051.rb b/ruby/euler051.rb index 3e34852..807abed 100644 --- a/ruby/euler051.rb +++ b/ruby/euler051.rb @@ -1,28 +1,61 @@ -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 } +require_relative 'euler' + +# i = 0 +# Prime.take_while do |p| +# i += 1 +# result = false +# digits = p.to_digit_list +# Euler.non_empty_subsets(digits).each do |index_list| +# related_primes = [] +# ('1'..'9').each do |j| +# copied_digit_list = Array.new(digits) +# index_list.each do |index| +# copied_digit_list[index] = j +# end +# new_num = Euler.from_digit_list(copied_digit_list) +# related_primes.push(new_num) if new_num.prime? and !related_primes.include?(new_num) +# end +# if related_primes.count == 7 +# puts p +# puts '---' +# puts related_primes.inspect +# result = related_primes +# end +# end +# puts result if i % 1000 +# result == false +# end + +def bitmasks(length) + (1..2**length-1).map { |n| n.to_s(2) }.map { |s| s.rjust(4, '0')}.map { |s| s.split('').map(&:to_i)} 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 +def prime_digit_replacement(number, mask) + digits = number.to_digit_list + related_primes = [] + ('0'..'9').each do |j| + copied_digit_list = Array.new(digits) + mask.each_with_index do |bit, index| + copied_digit_list[index] = j if bit == 1 end + new_num = Euler.from_digit_list(copied_digit_list) + related_primes.push(new_num) if new_num.prime? and !related_primes.include?(new_num) and new_num.to_digit_list.count == digits.count end + related_primes 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 +def solution(family_count) + result = false + Prime.take_while do |p| + digits = p.to_digit_list + bitmasks(digits.count).each do |mask| + related_primes = prime_digit_replacement(p, mask) + if related_primes.count == family_count and related_primes.include?(p) + result = p + break + end + end + result == false end + result end diff --git a/ruby/euler052.rb b/ruby/euler052.rb new file mode 100644 index 0000000..3e34852 --- /dev/null +++ b/ruby/euler052.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