5 more solutions

master
Evan Hemsley 2014-11-19 14:50:42 -08:00
parent 91c02fef43
commit 786860f59f
5 changed files with 81 additions and 0 deletions

5
euler047.rb Normal file
View File

@ -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

3
euler048.rb Normal file
View File

@ -0,0 +1,3 @@
def series(n)
(1..n).map { |x| x ** x }.inject(:+)
end

23
euler049.rb Normal file
View File

@ -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

22
euler050.rb Normal file
View File

@ -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

28
euler051.rb Normal file
View File

@ -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