Permalink
Browse files

change 9 around a bit, and add solution for 25.

  • Loading branch information...
1 parent 366d0b0 commit e3697ce34e8779d1277f19708107de2ad1607f31 @andrew12 committed Mar 26, 2011
Showing with 26 additions and 16 deletions.
  1. +7 −16 09.rb
  2. +19 −0 25.rb
View
23 09.rb
@@ -1,17 +1,8 @@
-# I'll admit I cheated a litle for this one.
-(1..1000).each do |a|
- b = a+1
- c = b+1
- while c <= 1000
- while c * c < b * b + a * a
- c = c + 1
- end
- if c * c == a * a + b * b
- if a + b + c == 1000
- puts a * b * c
- exit
- end
- end
- b = b + 1
- end
+(1..1000).to_a.repeated_combination(3) do |a, b, c|
+ next unless a < b
+ next unless b < c
+ next unless a + b + c == 1000
+ next unless a * a + b * b == c * c
+ puts a * b * c
+ exit
end
View
19 25.rb
@@ -0,0 +1,19 @@
+class Integer
+ def digits(base=10)
+ r = 0
+ n = self
+ until n == 0
+ r += 1
+ n /= base
+ end
+ r
+ end
+end
+
+n = 2
+a, b = 1, 1
+until b.digits == 1000
+ b, a = a + b, b
+ n += 1
+end
+puts n

0 comments on commit e3697ce

Please sign in to comment.