cleans up some older solutions + new Euler module

master
Evan Hemsley 2014-11-20 11:38:31 -08:00
parent 786860f59f
commit 4ba5651e9d
8 changed files with 87 additions and 139 deletions

39
euler.rb Normal file
View File

@ -0,0 +1,39 @@
require 'prime'
class Integer
def to_digit_list
self.to_s.split('').map(&:to_i)
end
end
module Euler
class << self
def from_digit_list(list)
list.join('').to_i
end
def generate_pythagorean_triples(upper_bound)
[].tap do |triples|
(2..upper_bound).each do |a|
(a..upper_bound).each do |b|
c = Math.sqrt(a**2 + b**2)
triples << [a, b, c.to_i] if c % 1 == 0
end
end
end
end
def palindrome?(n)
num = n.to_s
(0..(num.length-1)/2).each do |i|
if !(num[i] == num[num.length-1-i])
return false
end
end
true
end
end
end

View File

@ -1,28 +1,15 @@
def isPalindrome(n) require_relative 'euler'
numString = n.to_s
for i in 0..(numString.length-1)/2
if !(numString[i] == numString[numString.length-1-i])
return false
end
end
return true
end
def productsOfThreeDigits def products_of_three_digits
products = [] products = []
for i in 100..999 (100..999).each do |i|
for j in 100..999 (100..999).each do |j|
products << i*j products << i*j
end end
end end
products products
end end
palindromes = [] def solution
for i in productsOfThreeDigits products_of_three_digits.select { |x| Euler.palindrome?(x) }.max
if isPalindrome(i)
palindromes << i
end end
end
puts palindromes.sort

View File

@ -1,22 +1,24 @@
def multipleUpTo20 require_relative 'euler'
divisible = nil require 'prime'
i = 2
while !divisible def factorization_of_integer_divisible_by_all_integers_up_to(n)
numFound = true {}.tap do |factorization|
puts "checking #{i}" (2..n).map do |i|
for j in (2..20) Prime.prime_division(i).map do |factor|
if !(i % j == 0) factorization[factor.first] = factor.last if (!factorization.include?(factor.first) || (factor.last > factorization[factor.first]))
numFound = false
end end
end end
if numFound
return i
end
i += 1
end end
end end
puts multipleUpTo20 def factors_to_int(factorization)
[].tap do |result|
factorization.each do |prime, exponent|
result << prime ** exponent
end
end.inject(:*)
end
def solution
2*3*2*5*7*2*3*11*13*2*17*19 factors_to_int(factorization_of_integer_divisible_by_all_integers_up_to(20))
end

View File

@ -1,20 +1,15 @@
def sumSquared(n) def sum_squared(n)
(1..n).inject(:+) ** 2 (1..n).inject(:+) ** 2
end end
def squareSums(n) def square_sums(n)
sum = 0 (1..n).map { |i| i ** 2 }.inject(:+)
for i in 1..n
sum += i ** 2
end
sum
end end
def difference(n) def difference(n)
sumSquared(n) - squareSums(n) sum_squared(n) - square_sums(n)
end end
puts squareSums(10) def solution
puts sumSquared(10) difference(100)
end
puts difference(100)

View File

@ -1,33 +1,5 @@
require 'progressbar' require 'prime'
def increasingSieve(size) def solution
startingSize = 10 Prime.take(10001).last
eSieve = []
while eSieve.length < size
eSieve = sieve(startingSize)
startingSize *= 2
end end
eSieve
end
def sieve(n)
eSieve = (2..n).to_a
i = 0
pbar = ProgressBar.new("sieving", n)
while i < Math.sqrt(n)
j = i + 1
while (j < eSieve.length)
if (eSieve[j] > (i ** 2)) and ((eSieve[j] % eSieve[i]) == 0)
eSieve.delete_at j
pbar.inc
end
j += 1
end
i += 1
pbar.set(i + (n-eSieve.length))
end
pbar.finish
eSieve
end
puts increasingSieve(10001)[10000]

View File

@ -1,26 +1,10 @@
def consecutive_lists(num) require_relative 'euler'
list_of_sub_lists = []
digit_list = num.to_s.split("") def largest_consecutive_product(n, adjacent)
start_index = 0 n.to_digit_list.each_cons(adjacent).map { |x| x.inject(&:*) }.max
end_index = 4
while end_index < digit_list.size
list_of_sub_lists << digit_list[start_index..end_index].map(&:to_i)
start_index += 1
end_index += 1
end
list_of_sub_lists
end end
def consecutive_products(num) def solution
product_list = [] largest_consecutive_product(7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450,
consecutive_lists(num).each do |list| 13)
product_list << list.inject(:*)
end end
product_list
end
def largest_consecutive_product(num)
consecutive_products(num).max
end
puts largest_consecutive_product(7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450)

View File

@ -1,30 +1,5 @@
def generate_pythagorean_triples(upper_bound) require_relative 'euler'
list_of_triples = []
a = 2
b = 3
while a < upper_bound
b = a + 1
while b < upper_bound
c = Math.sqrt(a**2 + b**2)
list_of_triples << [a, b, c.to_i] if c % 1 == 0 #this is a check that the sqrt is an integer
b += 1
end
a += 1
end
list_of_triples
end
def sums_of_triples(list_of_triples) def solution
hash_of_sums = {} Euler.generate_pythagorean_triples(500).find { |x| x.inject(:+) == 1000 }.inject(:*)
list_of_triples.each do |triple|
hash_of_sums[triple] = triple.inject(:+)
end end
hash_of_sums
end
def find_pythagorean_sum(upper_bound, sum_to_match)
hash_of_sums = sums_of_triples(generate_pythagorean_triples(upper_bound))
hash_of_sums.detect { |key, value| value == sum_to_match }
end
puts find_pythagorean_sum(500, 1000).first.inject(:*)

View File

@ -1,11 +1,5 @@
#an implementation of the sieve of Eratosthenes require_relative 'euler'
def improved_sieve(n)
eSieve = (2..n).to_a
(2..Math.sqrt(n)).each do |i|
next unless eSieve[i]
(i*i).step(n, i) { |j| eSieve[j] = nil } #steps by increments of i up to n starting at i*i and sets those elements to nil, marking them for removal
end
eSieve.compact
end
puts improved_sieve(2000000).inject(:+) def solution
Prime.take_while { |x| x < 2000000 }.inject(:+)
end