Last active
August 29, 2015 13:57
-
-
Save eschulz2/9534632 to your computer and use it in GitHub Desktop.
project_euler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Find the sum of all the multiples of 3 or 5 below 1000. | |
def multiples | |
total = 0 | |
(1..1000).each { |num| total = total + num if (num % 3 == 0) || (num % 5 == 0) } | |
puts total | |
end | |
multiples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. | |
def even_fib_sum (limit) | |
first = 0 | |
second = 1 | |
num = 0 | |
sum_of_evens = 0 | |
while num < limit | |
num = first + second | |
sum_of_evens += num if num % 2 == 0 | |
first = second | |
second = num | |
end | |
puts sum_of_evens | |
end | |
even_fib_sum(4000000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#What is the largest prime factor of the number 600851475143 ? | |
def prime_factors(number) | |
return [] if number == 1 | |
factor = (2..number).find {|x| number % x == 0} | |
[factor] + prime_factors(number / factor) | |
end | |
puts prime_factors(600851475143).max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Find the largest palindrome made from the product of two 3-digit numbers. | |
array = [] | |
100.upto(999) do |a| | |
100.upto(999) do |b| | |
product = a * b | |
array << product if product.to_s == product.to_s.reverse | |
end | |
end | |
puts array.max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | |
def evenly_divisible(number) | |
array = [11, 12, 13, 14, 15, 16, 17, 18, 19] | |
array.each do |n| | |
if (number%n != 0) | |
return false | |
end | |
end | |
return true | |
end | |
number = 20 | |
start = 1 | |
until evenly_divisible(number) | |
number = start * 20 | |
start += 1 | |
end | |
puts number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment