Created
February 1, 2010 03:46
-
-
Save jonsterling/291440 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 natural multiples of 3 or 5 below 1000. | |
puts (1...1000).select { |i| i % 3 == 0 or i % 5 == 0 }.inject { |s,i| s+i } |
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 even-valued terms in the fibonacci | |
# sequence which do not exceed four million. | |
require 'rubygems' | |
require 'memoize' | |
include Memoize | |
def fibonacci n | |
return n if n < 2 | |
fibonacci(n-2) + fibonacci(n-1) | |
end | |
def last_fib n,b | |
return n if fibonacci(n+1) > b | |
last_fib(n+1,b) | |
end | |
memoize :fibonacci | |
puts 0.upto(last_fib(0,4e6)).map { |i| fibonacci i }.select { |n| n % 2 == 0 }.inject { |s,i| s+i } |
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? | |
require 'mathn' | |
class Proc | |
def << g | |
lambda {|*args| self.call(g.call(*args)) } | |
end | |
end | |
class Integer | |
def largest_prime_factor | |
first_factor = lambda do |x| | |
(2..x).find { |i| x % i == 0 } | |
end | |
complement = lambda { |a| a / first_factor.call(a) } | |
rec = :largest_prime_factor.to_proc << complement | |
return first_factor.call(self) if complement.call(self) == 1 | |
rec.call(self) | |
end | |
end | |
puts 600851475143.largest_prime_factor | |
# => 6857 |
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. | |
BOTTOM = 100**2 | |
TOP = 999**2 | |
class Integer | |
def greatest_three_digit_factor | |
(100..999).to_a.reverse.find { |x| self % x == 0 } | |
end | |
def three_by_three? | |
unless greatest_three_digit_factor.nil? | |
(self/greatest_three_digit_factor).to_s.size == 3 | |
end | |
end | |
end | |
def greatest_three_by_three_palindrome | |
(BOTTOM..TOP).select { |x| x.to_s == x.to_s.reverse and x.three_by_three? }.max | |
end | |
puts greatest_three_by_three_palindrome | |
# => 906609 |
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 number that is evenly divisible by all of the numbers from 1 to 20? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment