Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'set'
- class Triplet < Set
- attr_reader :n1, :n2, :n3
- def initialize(n1, n2, n3)
- @n1 = n1
- @n2 = n2
- @n3 = n3
- end
- def sum
- n1 + n2 + n3
- end
- def product
- n1 * n2 * n3
- end
- def pythagorean?
- n1 ** 2 + n2 ** 2 == n3 ** 2
- end
- def self.where(options={})
- max = options[:max_factor]
- min = options[:min_factor] || 1
- sum = options[:sum]
- triplets = []
- (min..max).each do |n1|
- (n1+1..max).each do |n2|
- (n2+1..max).each do |n3|
- set = new(n1, n2, n3)
- if set.pythagorean?
- if sum
- triplets << set if set.sum == sum
- else
- triplets << set
- end
- end
- end
- end
- end
- triplets
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement