fixed some old code + added solutions for new problems

master
Evan Hemsley 2014-11-14 00:57:29 -08:00
parent 6947402ff4
commit 3c91e1fb8f
10 changed files with 220 additions and 23 deletions

9
euler001.rb Normal file
View File

@ -0,0 +1,9 @@
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
end
puts multiples_of_three_and_five_below(1000).inject(:+)

16
euler015.rb Normal file
View File

@ -0,0 +1,16 @@
def factorial(n)
i = 1
while (n > 0)
i *= n
n -= 1
end
i
end
def combination(n, k)
factorial(n) / (factorial(k) * factorial(n-k))
end
def lattice_paths(size)
combination(2*size, size)
end

29
euler026.rb Normal file
View File

@ -0,0 +1,29 @@
def gcd(a, b)
b == 0 ? a : gcd(b, a % b)
end
def is_relatively_prime?(a, b)
gcd(a, b) == 1
end
def multiplicative_order_of_ten(n)
return nil unless is_relatively_prime?(10, n)
(2..n-1).each do |i|
return i if 10**i % n == 1
end
return nil
end
def longest_repeating_decimal
highest_order = 0
highest_order_n = 0
(1..1000).each do |n|
order = multiplicative_order_of_ten(n)
next if order.nil?
if highest_order < order
highest_order = order
highest_order_n = n
end
end
highest_order_n
end

17
euler028.rb Normal file
View File

@ -0,0 +1,17 @@
def spiral(size)
max_value = size**2
diagonals = [1]
diagonal_increase_value = 2
current_num = 1
until current_num == max_value
4.times do
current_num += diagonal_increase_value
diagonals << current_num
end
diagonal_increase_value += 2
end
diagonals.inject(:+)
end

View File

@ -1,18 +1,20 @@
def change(n, list_of_coins)
changeHelper(n, list_of_coins, [])
change_helper(n, list_of_coins, [])
end
def changeHelper(n, list_of_coins, result_list)
def change_helper(n, list_of_coins, result_list)
if list_of_coins.empty?
[result_list]
return []
elsif n == 0
[result_list]
return [result_list]
else
if n >= list_of_coins.first
changeHelper(n - list_of_coins.first, list_of_coins, result_list << list_of_coins.first) +
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
else
changeHelper(n, list_of_coins[1..list_of_coins.count-1], result_list)
list_of_coins.each do |coin|
if n < coin
return []
else
return change_helper(n - coin, list_of_coins, result_list + [coin]) + change_helper(n, list_of_coins - [coin], result_list)
end
end
end
end

View File

@ -1,21 +1,56 @@
class Integer
def mod_exp(exp, mod)
exp < 0 and raise ArgumentError, "negative exponent"
prod = 1
base = self % mod
until exp.zero?
exp.odd? and prod = (prod * base) % mod
exp >>= 1
base = (base * base) % mod
def gcd(a, b)
b == 0 ? a : gcd(b, a % b)
end
def generate_fractional_pairs
pairs = []
(10..99).each do |i|
(10..99).each do |j|
pairs << [i,j] if i < j
end
prod
end
pairs
end
PHI = (1 + Math.sqrt(5)) / 2
def check_weird_cancellations
solutions = []
fractions = generate_fractional_pairs
def fib(n)
((PHI ** n - (-1 / PHI) ** n) / Math.sqrt(5)).to_i
fractions.each do |fraction|
next if fraction.first % 10 == 0
num = fraction.first.to_s
den = fraction.last.to_s
if (num.include?(den[0]))
digit = num[num.index(den[0])]
new_num = num.delete(digit)
new_den = den.delete(digit)
elsif (num.include?(den[1]))
digit = num[num.index(den[1])]
new_num = num.delete(digit)
new_den = den.delete(digit)
end
solutions << [fraction.first, fraction.last] if (num.to_f / den.to_f) == (new_num.to_f / new_den.to_f)
end
solutions
end
puts fib(8)
def reduce_fraction(numerator, denominator)
until (gcd = gcd(numerator, denominator)) == 1
numerator /= gcd
denominator /= gcd
end
[numerator, denominator]
end
def multiply_fractions(fractions)
numerators = 1
denominators = 1
fractions.each do |fraction|
numerators *= fraction.first
denominators *= fraction.last
end
reduce_fraction(numerators, denominators)
end

17
euler034.rb Normal file
View File

@ -0,0 +1,17 @@
def factorial(n)
i = 1
while (n > 0)
i *= n
n -= 1
end
i
end
def curious_numbers(range)
solutions = []
(3..range).each do |i|
digits = i.to_s.split('').map(&:to_i)
solutions << i if digits.map { |x| factorial(x) }.inject(:+) == i
end
solutions
end

29
euler035.rb Normal file
View File

@ -0,0 +1,29 @@
require 'prime'
def primes_up_to(n)
Prime.take_while { |x| x < n }
end
def integer_rotations(n)
rotations = []
digits = n.to_s.split('')
digits.count.times do
rotations << digits.join('').to_i
digits.rotate!
end
rotations
end
def is_circular?(n)
circular = true
integer_rotations(n).each do |perm|
return false unless Prime.prime?(perm)
end
circular
end
def circular_primes_up_to(n)
primes_up_to(n).select { |x| puts x; is_circular?(x) }
end

11
euler036.rb Normal file
View File

@ -0,0 +1,11 @@
def is_palindromic_base_ten?(n)
n.to_s == n.to_s.reverse
end
def is_palindromic_base_two?(n)
n.to_s(2) == n.to_s(2).reverse
end
def palindromic_up_to(n)
(1..n).select { |i| is_palindromic_base_two?(i) && is_palindromic_base_ten?(i) }
end

32
euler037.rb Normal file
View File

@ -0,0 +1,32 @@
require 'prime'
def truncate_right(n)
n.to_s[0..-2].to_i
end
def truncate_left(n)
n.to_s[1..-1].to_i
end
def prime_truncatable_right?(n)
return false unless Prime.prime?(n)
truncated_prime = n
until (truncated_prime = truncate_right(truncated_prime)) == 0
return false unless Prime.prime?(truncated_prime)
end
return true
end
def prime_truncatable_left?(n)
return false unless Prime.prime?(n)
truncated_prime = n
until (truncated_prime = truncate_left(truncated_prime)) == 0
return false unless Prime.prime?(truncated_prime)
end
return true
end
def primes_truncatable_up_to(n)
(1..n).select { |i| prime_truncatable_right?(i) && prime_truncatable_left?(i) }
end