Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. require 'set'
  2.  
  3. class Triplet < Set
  4. attr_reader :n1, :n2, :n3
  5. def initialize(n1, n2, n3)
  6. @n1 = n1
  7. @n2 = n2
  8. @n3 = n3
  9. end
  10.  
  11. def sum
  12. n1 + n2 + n3
  13. end
  14.  
  15. def product
  16. n1 * n2 * n3
  17. end
  18.  
  19. def pythagorean?
  20. n1 ** 2 + n2 ** 2 == n3 ** 2
  21. end
  22.  
  23. def self.where(options={})
  24. max = options[:max_factor]
  25. min = options[:min_factor] || 1
  26. sum = options[:sum]
  27. triplets = []
  28. (min..max).each do |n1|
  29. (n1+1..max).each do |n2|
  30. (n2+1..max).each do |n3|
  31. set = new(n1, n2, n3)
  32. if set.pythagorean?
  33. if sum
  34. triplets << set if set.sum == sum
  35. else
  36. triplets << set
  37. end
  38. end
  39. end
  40. end
  41. end
  42. triplets
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement