Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- def get_array number, recurring
- (1..number-recurring).map {|i| [i, i]} + (number-recurring..number).map {|i| [i, i-recurring]}
- end
- def simons array
- hash = array.reduce({}) {|hash, pair| (hash[pair.last] ||= []).push pair.first; hash}
- hash.keys.combination(16).map do |array|
- array.map {|parent| hash[parent]}
- end.map {|array| array.first.product *array.drop(1)}.flatten(1).size
- end
- def original array
- array.combination(16).select {|c| c.map(&:last).uniq.size == c.size }.size
- end
- puts "Original: #{Benchmark.measure { puts original get_array 33, 1}}"
- puts "Simons: #{Benchmark.measure { puts simons get_array 33, 1}}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement