Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Triplet
- def initialize(*input)
- @numbers = *input
- end
- def sum
- @numbers.reduce(:+)
- end
- def product
- @numbers.reduce(:*)
- end
- def pythagorean?
- squares=@numbers.map {|m| square(m)}.sort
- squares[2]==squares[0]+squares[1]
- end
- def self.where(conditions)
- max = conditions[:max_factor]
- min = conditions[:min_factor] || 1
- sum = conditions[:sum]
- available_integers = Array(min..max)
- triplet_min_to_max = available_integers.combination(3).to_a.map{|m| Triplet.new(m[0],m[1],m[2])}
- pythagorean_triplets = triplet_min_to_max.select {|s| s.pythagorean?}
- sum ? pythagorean_triplets.select! {|s| s.sum==sum} : pythagorean_triplets
- end
- private
- def square(x)
- x**2
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement