Skip to content

Instantly share code, notes, and snippets.

View aptinio's full-sized avatar

Aaron Tinio aptinio

  • Angeles City, Philippines
View GitHub Profile
@aptinio
aptinio / euler009.rb
Created November 4, 2009 11:07
Project Euler Problem 9
def pythagorean_triple(m, n)
m, n = n, m if m > n
[n**2 - m**2, 2*m*n, n**2 + m**2]
end
infinity = 1.0/0.0
catch :answer_found do
(1..infinity).each do |m|
(m+1..infinity).each do |n|
@aptinio
aptinio / euler007.rb
Created November 4, 2009 10:29
Project Euler Problem 7
require 'mathn'
prime = Prime.new
10_000.times {prime.succ}
prime.succ
@aptinio
aptinio / euler004.rb
Created November 4, 2009 06:35
Project Euler Problem 4
def palindrome?(integer)
string = integer.to_s
string == string.reverse
end
products = []
999.step(100, -1).to_a.each do |a|
999.step(100, -1).to_a.each do |b|
product = a*b
@aptinio
aptinio / euler003.rb
Created November 4, 2009 05:45
Project Euler Problem 3
require 'mathn'
600851475143.prime_division.last.first
@aptinio
aptinio / euler002.rb
Created November 4, 2009 05:35
Project Euler Problem 2
fib = [1, 2]
def fib.next
self[-1] + self[-2]
end
fib << fib.next while fib.next < 4_000_000
fib.select {|f| f.remainder(2) == 0}.reduce(:+)
@aptinio
aptinio / euler001.rb
Created November 4, 2009 05:11
Project Euler Problem 1
(1...1000).to_a.select {|n| n.remainder(3) == 0 || n.remainder(5) == 0}.reduce(:+)