Created
September 29, 2016 20:15
-
-
Save kharioki/4e8bf51fc9629bc62538fe1fbd051b88 to your computer and use it in GitHub Desktop.
This file contains 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
def mid_point(x,y) | |
(x+y)/2.0 | |
end | |
def square_root(x,e) | |
if x == 1 | |
guess = 1 | |
else | |
lowest = 0 | |
highest = x | |
guess = mid_point(lowest,highest) | |
range = (guess*guess)- x | |
until range <= e and range > 0 do | |
if range < 0 | |
lowest = guess | |
else highest = guess | |
end | |
guess = mid_point(lowest, highest) | |
range = (guess*guess)- x | |
end | |
guess | |
end | |
end | |
def prime_number?(x) | |
sqrt = square_root(x, 0.00001) | |
startvalue = 2 | |
endvalue = sqrt | |
is_a_prime = true | |
while startvalue <= endvalue | |
if(x % startvalue == 0) | |
is_a_prime = false | |
end | |
startvalue = startvalue + 1 | |
end | |
is_a_prime | |
end | |
print "Enter No.:" | |
x = Float $stdin.gets.chomp | |
prime_number?(x) ? puts("true") : puts("false") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment