Last active
August 29, 2015 14:05
-
-
Save sarathsp06/afcf25569b338a2b373d 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
--fuinction to get the square of an integer | |
square::Int->Int | |
square a = a*a | |
--function to find the mid of two integers floor to ,lower if fractional | |
mid::Int->Int->Int | |
mid a b = (a+b) `div` 2 | |
--function to find the root given one arguement | |
root::Int->Int | |
root a=root a 0 a where | |
root a lowerLimit upperLimit | |
| square(mid lowerLimit upperLimit)==a = mid lowerLimit upperLimit | |
| lowerLimit == upperLimit = if square(lowerLimit)<a then a else lowerLimit-1 | |
| square(mid lowerLimit upperLimit)>a = root a lowerLimit (mid lowerLimit upperLimit - 1) | |
| square(mid lowerLimit upperLimit)<a = root a (mid lowerLimit upperLimit + 1) upperLimit | |
main = do | |
input <- getLine | |
print (root (read input)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Program to find the square root in log n complexity in #haskell
prints the nearest integer less than or equal to the square root of the number given