Skip to content

Instantly share code, notes, and snippets.

@kgrz
Created August 15, 2012 16:45
Show Gist options
  • Select an option

  • Save kgrz/3361492 to your computer and use it in GitHub Desktop.

Select an option

Save kgrz/3361492 to your computer and use it in GitHub Desktop.
Project Euler problem 9. Using formula.
require 'benchmark'
result = []
Benchmark.bm(7) do |reports|
reports.report do
(2..500).each do |x|
(2..500).each do |y|
next unless x > y
a = [x*x-y*y, 2*x*y, x*x+y*y]
next unless (a[0] + a[1] + a[2] === 1000)
next unless (a[0]*a[0]+a[1]*a[1] === a[2]*a[2])
result << a
puts a
end
end
end
end
__END__
user system total real
0.110000 0.000000 0.110000 (0.10.7985)
The formula used is Euler's formula for finding Pythagorean triplets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment