Skip to content

Instantly share code, notes, and snippets.

@pjc
pjc / euler5.rb
Created July 1, 2012 22:41
Euler Project in Ruby - Problem 5
upto = 20
def prime? x
(2..x-1).each { |y| return false if x % y == 0 }
true
end
# For speed reasons we want to find max possible increment
def increment upto
a = 1
@pjc
pjc / euler4.rb
Created July 1, 2012 22:38
Euler Project in Ruby - Problem 4
def palindrome? x
y = x.to_s
checks = y.length / 2
y[0..(checks-1)] == y[-checks..-1].reverse
end
def divideable? x
999.downto(100).each { |y| return true if x % y == 0 && (x / y).to_s.length == 3 }
false
end
@pjc
pjc / euler3.rb
Created July 1, 2012 22:35
Euler Project in Ruby - Problem 3
# We make array of prime numbers with modulus 0 as long as product_sum < n
def prime? n
(2..(n-1)).each { |x| return false if n % x == 0 }
true
end
n = 600_851_475_143
a = []
product_sum = 1
@pjc
pjc / euler2.rb
Created July 1, 2012 22:32
Euler Project in Ruby - Problem 2
# Create Fibonnaci Array
a = [1,2]
upto = 4_000_000
while a[-2] + a[-1] < upto
a << a[-2] + a[-1]
end
# Create sum of even Fibonnaci numbers
sum = 0
a.each { |x| sum+= x if x.even? }
@pjc
pjc / euler1.rb
Created July 1, 2012 22:26
Euler Project in Ruby - Problem 1
y = 0
(1..999).each { |x| y = y + x if (x % 3 == 0) || (x % 5 == 0) }
puts y