Pythagorean triples generator


/ Published in: Ruby
Save to your folder(s)

Very quick and dirty Pythagorean triples generator


Copy this code and paste it in your HTML
  1. #!/bin/ruby
  2. # author: Lukasz Korecki, student no: 0617836
  3. # purpose: simple program generating Pythagorian triples
  4.  
  5. # Vars:
  6. given_number = 60
  7.  
  8. m = 1
  9. # Number definitions using the article from wikipedia:
  10. # http://en.wikipedia.org/wiki/Pythagorean_triple#Other_formulas_for_generating_triples
  11. #
  12. a = m*2 + 1
  13. b = (m*2) * (m + 1)
  14. c = ((m*2) * (m + 1)) + 1
  15. sum = a + b + c
  16.  
  17.  
  18. # Output and formatting
  19. sep = "\t|\t"
  20. puts "Your max value is " + given_number.to_s
  21. puts sep+"small"+sep+"medium"+sep+"large"+ sep + "sum" + sep
  22. puts "-" * 80
  23.  
  24. # Main loop calculating the numbers and outputs
  25. while c < given_number
  26. m +=1
  27. puts sep + a.to_s + sep+ b.to_s + sep +c.to_s + sep + sum.to_s + sep
  28.  
  29. a = m*2 + 1
  30. b = (m*2) * (m + 1)
  31. c = ((m*2) * (m + 1)) + 1
  32. sum = a + b +c
  33.  
  34. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.