Skip to content

Instantly share code, notes, and snippets.

@gaxiiiiiiiiiiii
Created January 23, 2019 09:53
Show Gist options
  • Select an option

  • Save gaxiiiiiiiiiiii/00c6a9f7096e033a2ebbf007955ec96b to your computer and use it in GitHub Desktop.

Select an option

Save gaxiiiiiiiiiiii/00c6a9f7096e033a2ebbf007955ec96b to your computer and use it in GitHub Desktop.
findRoot :: Float -> Float -> Float
findRoot digit n = find n candidate
where scale = 10 ** digit
candidate = map (/ scale) [1..(n * scale)]
find :: Float -> [Float] -> Float
find _ [x] = x
find n xs | (target ** 2) >= n = find n smaller
| otherwise = find n bigger
where mid = (length xs) `div` 2
(smaller,bigger) = splitAt mid xs
target = last smaller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment