added solutions for 7 more problems
parent
22af3e98b1
commit
91c02fef43
|
@ -0,0 +1,16 @@
|
|||
def champernowe_constant_up_to(n)
|
||||
(1..n).to_a.join('')
|
||||
end
|
||||
|
||||
def solution
|
||||
champernowe = champernowe_constant_up_to(1000000)
|
||||
digits = []
|
||||
digits << champernowe[0]
|
||||
digits << champernowe[9]
|
||||
digits << champernowe[99]
|
||||
digits << champernowe[999]
|
||||
digits << champernowe[9999]
|
||||
digits << champernowe[99999]
|
||||
digits << champernowe[999999]
|
||||
digits.map(&:to_i).inject(:*)
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
require 'prime'
|
||||
|
||||
def pandigital?(n)
|
||||
n.to_s.split('').map(&:to_i).sort == (1..n.to_s.length).to_a
|
||||
end
|
||||
|
||||
def largest_pandigital_prime_up_to(n)
|
||||
Prime.take_while { |i| i < n }.select { |i| pandigital?(i) }.max
|
||||
end
|
|
@ -0,0 +1,51 @@
|
|||
def parse_words
|
||||
file = File.open('p042_words.txt')
|
||||
words = file.read.delete('"').split(',')
|
||||
file.close
|
||||
words
|
||||
end
|
||||
|
||||
def perfect_square?(n)
|
||||
Math.sqrt(n) % 1 == 0
|
||||
end
|
||||
|
||||
def triangle_number?(n)
|
||||
perfect_square?((8 * n) + 1)
|
||||
end
|
||||
|
||||
ALPHABET = {
|
||||
'A' => 1,
|
||||
'B' => 2,
|
||||
'C' => 3,
|
||||
'D' => 4,
|
||||
'E' => 5,
|
||||
'F' => 6,
|
||||
'G' => 7,
|
||||
'H' => 8,
|
||||
'I' => 9,
|
||||
'J' => 10,
|
||||
'K' => 11,
|
||||
'L' => 12,
|
||||
'M' => 13,
|
||||
'N' => 14,
|
||||
'O' => 15,
|
||||
'P' => 16,
|
||||
'Q' => 17,
|
||||
'R' => 18,
|
||||
'S' => 19,
|
||||
'T' => 20,
|
||||
'U' => 21,
|
||||
'V' => 22,
|
||||
'W' => 23,
|
||||
'X' => 24,
|
||||
'Y' => 25,
|
||||
'Z' => 26
|
||||
}
|
||||
|
||||
def triangle_word?(word)
|
||||
triangle_number?(word.split('').map { |c| ALPHABET[c] }.inject(:+))
|
||||
end
|
||||
|
||||
def count_triangle_words
|
||||
parse_words.select { |w| triangle_word?(w) }.count
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
def pandigital?(n)
|
||||
n.to_s.split('').map(&:to_i).sort == (1..n.to_s.length).to_a
|
||||
end
|
||||
|
||||
def pandigitals
|
||||
[0,1,2,3,4,5,6,7,8,9].permutation.to_a
|
||||
end
|
||||
|
||||
def digit_list_to_int(digits)
|
||||
digits.join('').to_i
|
||||
end
|
||||
|
||||
def tridigit_additive_property?(digits)
|
||||
return false unless digit_list_to_int([digits[1],digits[2],digits[3]]) % 2 == 0
|
||||
return false unless digit_list_to_int([digits[2],digits[3],digits[4]]) % 3 == 0
|
||||
return false unless digit_list_to_int([digits[3],digits[4],digits[5]]) % 5 == 0
|
||||
return false unless digit_list_to_int([digits[4],digits[5],digits[6]]) % 7 == 0
|
||||
return false unless digit_list_to_int([digits[5],digits[6],digits[7]]) % 11 == 0
|
||||
return false unless digit_list_to_int([digits[6],digits[7],digits[8]]) % 13 == 0
|
||||
return false unless digit_list_to_int([digits[7],digits[8],digits[9]]) % 17 == 0
|
||||
true
|
||||
end
|
||||
|
||||
def pandigitals_with_additive_property
|
||||
pandigitals.select { |n| tridigit_additive_property?(n) }.map { |n| n.join('').to_i }
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
def pentagonal?(n)
|
||||
((Math.sqrt((24 * n) + 1) + 1) / 6) % 1 == 0
|
||||
end
|
||||
|
||||
def nth_pentagonal(n)
|
||||
n * (3 * n - 1) / 2
|
||||
end
|
||||
|
||||
def pentagonal
|
||||
pentagonal_enum = Enumerator.new do |y|
|
||||
n = 1
|
||||
loop do
|
||||
x = n * (3 * n - 1) / 2
|
||||
n += 1
|
||||
y << x
|
||||
end
|
||||
end
|
||||
pentagonal_enum
|
||||
end
|
||||
|
||||
def sum_and_difference_pentagonal_up_to(n)
|
||||
x_list = pentagonal.take_while { |x| x < n }
|
||||
y_list = pentagonal.take_while { |y| y < n }
|
||||
|
||||
solutions = []
|
||||
|
||||
x_list.each do |x|
|
||||
y_list.each do |y|
|
||||
solutions << [x,y] if pentagonal?(x + y) && pentagonal?((x - y).abs)
|
||||
end
|
||||
end
|
||||
solutions
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
def triangular
|
||||
triangular_enum = Enumerator.new do |y|
|
||||
n = 1
|
||||
loop do
|
||||
x = n * (n+1) / 2
|
||||
n += 1
|
||||
y << x
|
||||
end
|
||||
end
|
||||
triangular_enum
|
||||
end
|
||||
|
||||
def pentagonal?(n)
|
||||
((Math.sqrt((24 * n) + 1) + 1) / 6) % 1 == 0
|
||||
end
|
||||
|
||||
def hexagonal?(n)
|
||||
((Math.sqrt((8 * n) + 1) + 1) / 4) % 1 == 0
|
||||
end
|
||||
|
||||
def triangular_pentagonal_and_hexagonal_up_to(n)
|
||||
triangular.take_while { |x| x < n }.select { |x| pentagonal?(x) && hexagonal?(x) }
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
require 'prime'
|
||||
|
||||
def odd_composites
|
||||
enum = Enumerator.new do |y|
|
||||
a = 3
|
||||
loop do
|
||||
y << a unless Prime.prime?(a)
|
||||
a += 2
|
||||
end
|
||||
end
|
||||
enum
|
||||
end
|
||||
|
||||
def squares
|
||||
enum = Enumerator.new do |y|
|
||||
n = 1
|
||||
loop do
|
||||
y << n ** 2
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
enum
|
||||
end
|
||||
|
||||
|
||||
def find_sum(num)
|
||||
found = false
|
||||
primes = Prime::EratosthenesGenerator.new
|
||||
prime = 0
|
||||
squares_enum = squares
|
||||
while prime < num
|
||||
prime = primes.next
|
||||
squares_enum.rewind
|
||||
twice_square = squares_enum.next * 2
|
||||
while twice_square < num
|
||||
return [prime, twice_square / 2] if twice_square + prime == num
|
||||
twice_square = squares_enum.next * 2
|
||||
end
|
||||
end
|
||||
found
|
||||
end
|
||||
|
||||
def property?(num)
|
||||
find_sum(num)
|
||||
end
|
||||
|
||||
def find_incorrect_up_to(n)
|
||||
odd_composites.take_while { |x| x < n }.reject{ |x| property?(x) }
|
||||
end
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue