Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. # Takeuchi function - concurrent version
  2.  
  3. def tak(x, y, z)
  4. unless y < x
  5. z
  6. else
  7. tak(tak(x-1, y, z),
  8. tak(y-1, z, x),
  9. tak(z-1, x, y))
  10. end
  11. end
  12.  
  13. def tak2(x, y, z)
  14. unless y < x
  15. z
  16. else
  17. tak2(tak2(x-1, y, z),
  18. tak2(y-1, z, x),
  19. tak2(z-1, x, y))
  20. end
  21. end
  22.  
  23. require 'benchmark'
  24.  
  25. max = (ARGV.shift || 22).to_i
  26.  
  27. puts "Max: #{max}"
  28.  
  29. puts Benchmark.measure {
  30. threads = []
  31. threads << Thread.new do
  32. p tak(max, 9, 0)
  33. end
  34.  
  35. threads << Thread.new do
  36. p tak2(max, 9, 0)
  37. end
  38.  
  39. threads.each { |t| t.join }
  40. }
Add Comment
Please, Sign In to add comment