Revisions
-
manveru renamed this gist
Jun 5, 2013 . 1 changed file with 3 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,16 +5,7 @@ # Challenge A: Does this solution have any problems? If so, explain. # Challenge B: Propose an alternate implementation that does not use threads. n = 10000 p (1..n).reduce(:+) # by iteration p (n / 2) * (n + 1) # by math -
jcoene created this gist
Feb 25, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ #!/usr/bin/ruby # This program adds up the numbers 1 through 10,000 using 100 threads. # Challenge A: Does this solution have any problems? If so, explain. # Challenge B: Propose an alternate implementation that does not use threads. require "thread" mt_result = 0 100.times.map do |n| Thread.new do base = 1 + (n * 100) 100.times do |i| mt_result += base + i end end end.map(&:join) puts "The multi-threaded result is: #{mt_result}"