Skip to content

Instantly share code, notes, and snippets.

@NolanDeveloper
Last active July 24, 2019 20:48
Show Gist options
  • Save NolanDeveloper/94db092e07d1a8a1f7dea2e0c1afd3f0 to your computer and use it in GitHub Desktop.
Save NolanDeveloper/94db092e07d1a8a1f7dea2e0c1afd3f0 to your computer and use it in GitHub Desktop.
Project euler utility functinos
-- Convert number to list of digits
tods n = if n < 10 then [n] else reverse $ unfoldr (\i -> if i == 0 then Nothing else Just (i `mod` 10, i `div` 10)) n
-- Convert list of digits to number
ton = foldl' (\a b -> a * 10 + b) 0
-- Make list of numbers from digits
numbers ns = concat $ iterate (\xs -> concatMap (\n -> map (\xs' -> n : xs') xs) ns) (map (\c -> [c]) ns)
-- Primality test
isPrime n = 2 <= n && all (\k -> n `mod` k /= 0) [2..ceiling (sqrt (fromIntegral n))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment