Sha256: 33c4da5c41ab9eadc3425e579042bf327d0ceb3967e4cc727b201effd147722f

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

# A silly little test program that finds prime numbers.  It
# is intentionally badly designed to show off the use
# or ruby-prof.
# 
# Source from http://people.cs.uchicago.edu/~bomb154/154/maclabs/profilers-lab/

def make_random_array(length, maxnum)
  result = Array.new(length)
  result.each_index do |i|
    result[i] = rand(maxnum)
  end
    
  result
end
 
def is_prime(x)
  y = 2
  y.upto(x-1) do |i|
    return false if (x % i) == 0
  end
  true
end

def find_primes(arr)
  result = arr.select do |value|
    is_prime(value)
  end
  result
end

def find_largest(primes)
  largest = primes.first

  # Intentionally use upto for example purposes
  # (upto is also called from is_prime)
  0.upto(primes.length-1) do |i|
    sleep(0.02)
    prime = primes[i]
    if prime > largest
      largest = prime
    end
  end
  largest
end

def run_primes
  length = 500  
  maxnum = 10000
  
  # Create random numbers
  random_array = make_random_array(length, maxnum)
  
  # Find the primes
  primes = find_primes(random_array)
  
  # Find the largest primes
  largest = find_largest(primes)
end

run_primes

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
jeremy-ruby-prof-0.6.1 test/prime.rb
ruby-prof-0.5.2-mswin32 test/prime.rb
ruby-prof-0.5.1-mswin32 test/prime.rb
ruby-prof-0.6.0 test/prime.rb
ruby-prof-0.5.2 test/prime.rb
ruby-prof-0.5.1 test/prime.rb
ruby-prof-0.6.0-x86-mswin32-60 test/prime.rb