Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created September 10, 2011 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradclawsie/1208013 to your computer and use it in GitHub Desktop.
Save bradclawsie/1208013 to your computer and use it in GitHub Desktop.
haskell code to find all such four-digit numbers (except 0000)
module Main where
import Char
f a1 a2 b1 b2 =
(a1 == (((a2 + b2 - b1) / 10.0) + b1 - a2) * (-1.0)) &&
(a2 == ((a2 - a1 - b1) * 10.0) - b2 + b1) &&
(b1 == (((a2 - a1 - b1) * 10.0) - a2 - b2) * (-1.0)) &&
(b2 == ((a2 - a1 - b1) * 10) - a2 + b1)
-- first line transforms 1978 -> [1.0,9.0,7.0,8.0]
g n = let ns = map toRational $ map digitToInt (show n) in
f (ns!!0) (ns!!1) (ns!!2) (ns!!3)
main = print $ filter g [1000..9999]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment