Skip to content

Instantly share code, notes, and snippets.

@vituscze
Created April 8, 2026 13:47
Show Gist options
  • Select an option

  • Save vituscze/6cd2b515bedadabf54f4980eba16ec56 to your computer and use it in GitHub Desktop.

Select an option

Save vituscze/6cd2b515bedadabf54f4980eba16ec56 to your computer and use it in GitHub Desktop.
factors1 :: Integer -> Integer -> [Integer]
factors1 k n
| k * k > n = [n]
| n `mod` k == 0 = k : factors1 k (n `div` k)
| otherwise = factors1 (k + 1) n
factors :: Integer -> [Integer]
factors 1 = []
factors n = factors1 2 n
hasRepeat :: [Integer] -> Bool
hasRepeat [] = False
hasRepeat [_] = False
hasRepeat (x : y : xs) = x == y || hasRepeat (y : xs)
squareFreeFrom :: Integer -> [Integer]
squareFreeFrom n =
let rest = squareFreeFrom (n + 1)
in if hasRepeat (factors n) then rest else n : rest
squareFree :: [Integer]
squareFree = squareFreeFrom 1
root :: Integer -> Integer -> Integer -> Integer
root lo hi n =
if hi < lo then hi
else let mid = (lo + hi) `div` 2
in if mid * mid <= n then root (mid + 1) hi n
else root lo (mid - 1) n
squareRoot :: Integer -> Integer
squareRoot n = root 1 n n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment