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)
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
require_relative 'euler'
def productsOfThreeDigits
def products_of_three_digits
products = []
for i in 100..999
for j in 100..999
(100..999).each do |i|
(100..999).each do |j|
products << i*j
end
end
products
end
palindromes = []
for i in productsOfThreeDigits
if isPalindrome(i)
palindromes << i
end
def solution
products_of_three_digits.select { |x| Euler.palindrome?(x) }.max
end
puts palindromes.sort

View File

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

View File

@ -1,33 +1,5 @@
require 'progressbar'
require 'prime'
def increasingSieve(size)
startingSize = 10
eSieve = []
while eSieve.length < size
eSieve = sieve(startingSize)
startingSize *= 2
end
eSieve
def solution
Prime.take(10001).last
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)
list_of_sub_lists = []
digit_list = num.to_s.split("")
start_index = 0
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
require_relative 'euler'
def largest_consecutive_product(n, adjacent)
n.to_digit_list.each_cons(adjacent).map { |x| x.inject(&:*) }.max
end
def consecutive_products(num)
product_list = []
consecutive_lists(num).each do |list|
product_list << list.inject(:*)
end
product_list
def solution
largest_consecutive_product(7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450,
13)
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)
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
require_relative 'euler'
def sums_of_triples(list_of_triples)
hash_of_sums = {}
list_of_triples.each do |triple|
hash_of_sums[triple] = triple.inject(:+)
end
hash_of_sums
def solution
Euler.generate_pythagorean_triples(500).find { |x| x.inject(:+) == 1000 }.inject(:*)
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
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
require_relative 'euler'
puts improved_sieve(2000000).inject(:+)
def solution
Prime.take_while { |x| x < 2000000 }.inject(:+)
end