refactored some old solutions

master
Evan Hemsley 2014-12-04 16:50:52 -08:00
parent b8ab94b533
commit a9d1671150
10 changed files with 46 additions and 95 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.DS_Store

View File

@ -6,14 +6,18 @@ class Integer
end end
def divisors def divisors
divisors = [] [].tap do |divisors|
(1..Math.sqrt(self).to_i).each do |num| (1..Math.sqrt(self).to_i).each do |num|
if self % num == 0 if self % num == 0
divisors << num divisors << num
divisors << self / num divisors << self / num
end end
end end
divisors end
end
def prime_factors
self.prime_division.map(&:first)
end end
def factorial def factorial

View File

@ -1,9 +1,7 @@
def multiples_of_three_and_five_below(n) def multiples_of_three_and_five_below(n)
multiples = [] (3..n-1).select { |i| (i % 3 == 0) || (i % 5 == 0) }
(3..n-1).each do |i|
multiples << i if ((i % 3 == 0) || (i % 5 == 0))
end
multiples
end end
puts multiples_of_three_and_five_below(1000).inject(:+) def solution
multiples_of_three_and_five_below(1000).inject(:+)
end

View File

@ -1,30 +1,16 @@
def fibonacci(n) def even_fibonacci_up_to(n)
fibTable = [] Enumerator.new do |y|
if (n == 0) or (n == 1) a = 0
return 1 b = 1
else loop do
if fibTable.at(n-1) == nil temp = b
fibTable.insert(n-1, fibonacci(n-1)) b = a + b
fib1 = fibTable.at(n-1) a = temp
else y << b
fib1 = fibTable.at(n-1)
end
if fibTable.at(n-2) == nil
fibTable.insert(n-2, fibonacci(n-2))
fib2 = fibTable.at(n-2)
else
fib2 = fibTable.at(n-2)
end
return fib1 + fib2
end end
end.take_while { |i| i < n }.select { |i| i.even? }
end end
sum = 0 def solution
for i in 1..32 even_fibonacci_up_to(4000000).inject(:+)
fib = fibonacci(i)
if (fib%2) == 0
sum += fib
end end
end
puts sum

View File

@ -1,38 +1,5 @@
def sieve(n) require_relative 'euler'
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
eSieve = sieve(600851475143) def solution
600851475143.prime_factors.max
def isPrime(n)
eSieve.contains(n)
end end
puts isPrime(7)
factors = []
for i in 1..600851475143
if (600851475143 % i) == 0
if isPrime(600851475143 % i)
factors << i
end
end
end

View File

@ -1,13 +1,13 @@
require_relative 'euler' require_relative 'euler'
def products_of_three_digits def products_of_three_digits
products = [] [].tap do |products|
(100..999).each do |i| (100..999).each do |i|
(100..999).each do |j| (100..999).each do |j|
products << i*j products << i*j
end end
end end
products end
end end
def solution def solution

View File

@ -1,22 +1,17 @@
require_relative 'euler' require_relative 'euler'
require 'prime'
def factorization_of_integer_divisible_by_all_integers_up_to(n) def factorization_of_integer_divisible_by_all_integers_up_to(n)
{}.tap do |factorization| {}.tap do |factorization|
(2..n).map do |i| (2..n).map do |i|
Prime.prime_division(i).map do |factor| Prime.prime_division(i).map do |prime, exponent|
factorization[factor.first] = factor.last if (!factorization.include?(factor.first) || (factor.last > factorization[factor.first])) factorization[prime] = exponent if (!factorization.include?(prime) || (exponent > factorization[prime]))
end end
end end
end end
end end
def factors_to_int(factorization) def factors_to_int(factorization)
[].tap do |result| factorization.map { |prime, exponent| prime ** exponent }.inject(:*)
factorization.each do |prime, exponent|
result << prime ** exponent
end
end.inject(:*)
end end
def solution def solution

View File

@ -6,10 +6,6 @@ def square_sums(n)
(1..n).map { |i| i ** 2 }.inject(:+) (1..n).map { |i| i ** 2 }.inject(:+)
end end
def difference(n)
sum_squared(n) - square_sums(n)
end
def solution def solution
difference(100) sum_squared(100) - square_sums(100)
end end

View File

@ -1,7 +1,7 @@
require_relative 'euler' require_relative 'euler'
def largest_consecutive_product(n, adjacent) def largest_consecutive_product(n, adjacent)
n.to_digit_list.each_cons(adjacent).map { |x| x.inject(&:*) }.max n.to_digit_list.each_cons(adjacent).map { |x| x.inject(:*) }.max
end end
def solution def solution

View File

@ -1 +1,5 @@
puts (2**1000).to_s.split("").map(&:to_i).inject(:+) require_relative 'euler'
def solution
(2**1000).to_digit_list.inject(:+)
end