Skip to content

Instantly share code, notes, and snippets.

@yassu
Created June 19, 2015 07:52
Show Gist options
  • Save yassu/4c7b3b52d83bd1f6681a to your computer and use it in GitHub Desktop.
Save yassu/4c7b3b52d83bd1f6681a to your computer and use it in GitHub Desktop.
-- n is a square number or not
isSquare:: Int -> Bool
isSquare n = t*t == n
where t = round(sqrt (fromIntegral(n)))
-- "n = c*c + (n-c*c)" is square split or not
isSquareSplit:: Int -> Int -> Bool
isSquareSplit n c = isSquare (n - c*c)
-- return whether n is splittable as two square number or not
splittableAsSquare:: Int -> Bool
splittableAsSquare n = True `elem` map (\c -> isSquareSplit (fromIntegral n) c) [1 .. t]
where t = round(sqrt (fromIntegral(n)))
cntSplitAndNot:: Int -> Int
cntSplitAndNot n
|(splittableAsSquare(n) == False) = 0
|(otherwise) = 1 + (length (takeWhile (\c -> not(splittableAsSquare(c))) [n+1 ..]))
moreThanCntSplitAndNotFrom:: Int -> Int -> Int
moreThanCntSplitAndNotFrom s k = head(filter (\n -> cntSplitAndNot n >= k) [s ..])
moreThanCntSplitAndNot:: Int -> Int
moreThanCntSplitAndNot k = moreThanCntSplitAndNotFrom 1 k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment