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
def divisors
divisors = []
[].tap do |divisors|
(1..Math.sqrt(self).to_i).each do |num|
if self % num == 0
divisors << num
divisors << self / num
end
end
divisors
end
end
def prime_factors
self.prime_division.map(&:first)
end
def factorial

View File

@ -1,9 +1,7 @@
def multiples_of_three_and_five_below(n)
multiples = []
(3..n-1).each do |i|
multiples << i if ((i % 3 == 0) || (i % 5 == 0))
end
multiples
(3..n-1).select { |i| (i % 3 == 0) || (i % 5 == 0) }
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)
fibTable = []
if (n == 0) or (n == 1)
return 1
else
if fibTable.at(n-1) == nil
fibTable.insert(n-1, fibonacci(n-1))
fib1 = fibTable.at(n-1)
else
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
def even_fibonacci_up_to(n)
Enumerator.new do |y|
a = 0
b = 1
loop do
temp = b
b = a + b
a = temp
y << b
end
end.take_while { |i| i < n }.select { |i| i.even? }
end
sum = 0
for i in 1..32
fib = fibonacci(i)
if (fib%2) == 0
sum += fib
def solution
even_fibonacci_up_to(4000000).inject(:+)
end
end
puts sum

View File

@ -1,38 +1,5 @@
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
require_relative 'euler'
eSieve = sieve(600851475143)
def isPrime(n)
eSieve.contains(n)
def solution
600851475143.prime_factors.max
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'
def products_of_three_digits
products = []
[].tap do |products|
(100..999).each do |i|
(100..999).each do |j|
products << i*j
end
end
products
end
end
def solution

View File

@ -1,22 +1,17 @@
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]))
Prime.prime_division(i).map do |prime, exponent|
factorization[prime] = exponent if (!factorization.include?(prime) || (exponent > factorization[prime]))
end
end
end
end
def factors_to_int(factorization)
[].tap do |result|
factorization.each do |prime, exponent|
result << prime ** exponent
end
end.inject(:*)
factorization.map { |prime, exponent| prime ** exponent }.inject(:*)
end
def solution

View File

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

View File

@ -1,7 +1,7 @@
require_relative 'euler'
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
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