Return to Snippet

Revision: 5255
at February 24, 2008 09:41 by lukaszkorecki


Initial Code
#!/bin/ruby
# author: Lukasz Korecki, student no: 0617836
# purpose: simple program generating Pythagorian triples

# Vars:
given_number = 60

m = 1
# Number definitions using the article from wikipedia:
# http://en.wikipedia.org/wiki/Pythagorean_triple#Other_formulas_for_generating_triples
#
a = m*2 + 1
b = (m*2) * (m + 1)
c = ((m*2) * (m + 1)) + 1
sum = a + b + c


# Output and formatting
sep = "\t|\t"
puts "Your max value is " + given_number.to_s
puts sep+"small"+sep+"medium"+sep+"large"+ sep + "sum" + sep
puts "-" * 80

# Main loop calculating the numbers and outputs
while c < given_number
		m +=1
		puts sep + a.to_s +  sep+ b.to_s + sep +c.to_s + sep + sum.to_s + sep

		a = m*2 + 1
		b = (m*2) * (m + 1)
		c = ((m*2) * (m + 1)) + 1
		sum = a + b +c

end

Initial URL

                                

Initial Description
Very quick and dirty Pythagorean triples generator

Initial Title
Pythagorean triples generator

Initial Tags
ruby

Initial Language
Ruby