Guest User

http://stackoverflow.com/q/31194315 version 2

a guest
Jul 2nd, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.59 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. def get_array number, recurring
  4.     (1..number-recurring).map {|i| [i, i]} + (number-recurring..number).map {|i| [i, i-recurring]}
  5. end
  6.  
  7. def simons array
  8.     hash = array.reduce({}) {|hash, pair| (hash[pair.last] ||= []).push pair.first; hash}
  9.     hash.keys.combination(16).each do |array|
  10.       arrays = array.map {|parent| hash[parent]}
  11.       arrays.first.product(*arrays.drop(1)).each {|a| yield a}
  12.     end
  13. end
  14.  
  15. size = 0
  16. puts Benchmark.measure do
  17.     simons(get_array(33, 1)) do |x|
  18.         size += 1 # or do something more interesting
  19.     end
  20. end
  21. p size
Add Comment
Please, Sign In to add comment