Created
August 15, 2012 16:45
-
-
Save kgrz/3361492 to your computer and use it in GitHub Desktop.
Project Euler problem 9. Using formula.
This file contains hidden or 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
| 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