Skip to content

Instantly share code, notes, and snippets.

@manveru
Forked from jcoene/gist:5034155
Last active December 18, 2015 03:19

Revisions

  1. manveru renamed this gist Jun 5, 2013. 1 changed file with 3 additions and 12 deletions.
    15 changes: 3 additions & 12 deletions gistfile1 → gistfile1.rb
    Original 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.

    require "thread"
    n = 10000

    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}"
    p (1..n).reduce(:+) # by iteration
    p (n / 2) * (n + 1) # by math
  2. @jcoene jcoene created this gist Feb 25, 2013.
    20 changes: 20 additions & 0 deletions gistfile1
    Original 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}"