Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Last active August 29, 2015 14:05
Show Gist options
  • Save sarathsp06/afcf25569b338a2b373d to your computer and use it in GitHub Desktop.
Save sarathsp06/afcf25569b338a2b373d to your computer and use it in GitHub Desktop.
--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))
@sarathsp06
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment