Created
July 31, 2013 06:42
-
-
Save chiragmongia/6119862 to your computer and use it in GitHub Desktop.
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 characters
| #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