Created
September 30, 2016 13:04
-
-
Save itsmuriuki/1c391d130888fc1dccd5a35e56c25fd8 to your computer and use it in GitHub Desktop.
finding prime number
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 square_root (n) | |
guess = n / 2.0 | |
while (((guess *guess) - n).abs > 0.000000001) | |
guess = refine(guess, n) | |
end | |
return guess | |
end | |
def refine (guess, n) | |
guess = (guess + (n / guess)) / 2.0 | |
end | |
#checking for prime numbers | |
def prime (n) | |
y = square_root (n) | |
(2..y). each do |i| | |
p = n % i | |
if p == 0 | |
return | |
false | |
end | |
end | |
true | |
end | |
square_root (7) | |
puts prime (7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment