Jump to content

Pythagorean triples: Difference between revisions

add Ruby
(add Ruby)
Line 1,004:
1000000 (70229, 808950)
10000000 (702309, 9706567)</lang>
 
=={{header|Ruby}}==
{{trans|Java}}
<lang ruby>class PythagoranTriplesCounter
def initialize(limit)
@limit = limit
@total = 0
@primatives = 0
generate_triples(3, 4, 5)
end
attr_reader :total, :primatives
private
def generate_triples(a, b, c)
perim = a + b + c
return if perim > @limit
 
@primatives += 1
@total += @limit / perim
 
generate_triples( a-2*b+2*c, 2*a-b+2*c, 2*a-2*b+3*c)
generate_triples( a+2*b+2*c, 2*a+b+2*c, 2*a+2*b+3*c)
generate_triples(-a+2*b+2*c,-2*a+b+2*c,-2*a+2*b+3*c)
end
end
 
perim = 10
while perim <= 100_000_000
c = PythagoranTriplesCounter.new perim
p [perim, c.total, c.primatives]
perim *= 10
end</lang>
 
output
<pre>[10, 0, 0]
[100, 17, 7]
[1000, 325, 70]
[10000, 4858, 703]
[100000, 64741, 7026]
[1000000, 808950, 70229]
[10000000, 9706567, 702309]
[100000000, 113236940, 7023027]</pre>
 
=={{header|Seed7}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.