Skip to content

Instantly share code, notes, and snippets.

@chiragmongia
Created July 31, 2013 06:42
Show Gist options
  • Select an option

  • Save chiragmongia/6119862 to your computer and use it in GitHub Desktop.

Select an option

Save chiragmongia/6119862 to your computer and use it in GitHub Desktop.
#Question- Prime Numbers - Step
#Define a method to find all prime numbers upto n using 'step' function.
def prime(num)
prime_no = []
a=0
prime_no << 2
3.step(num,2) do |n|
3.step(n/2,1) do |k|
if n % k == 0
a = 1
break
end
end
if a == 0
prime_no << n
end
a = 0
end
prime_no
end
#Main
print "Enter the number upto which you want Prime numbers:"
num = gets.to_i
if num == 2
p "#{num}"
end
p prime(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment