Skip to content

Instantly share code, notes, and snippets.

@teamdandelion
Created April 30, 2013 22:19
Show Gist options
  • Save teamdandelion/5492378 to your computer and use it in GitHub Desktop.
Save teamdandelion/5492378 to your computer and use it in GitHub Desktop.
Prints lists of numbers which are the sum of two powers of a given base
isPowerSum :: Int -> Int -> Bool
isPowerSum p x= any (==x) [a + b | a <- shortPows, b <- shortPows] where
shortPows = takeWhile (<x) . map (p^) $ [0..]
powerSequence :: Int -> [Int]
powerSequence p = filter (isPowerSum p) [1..]
sq1 = take 20 . powerSequence $ 2
sq2 = take 20 . powerSequence $ 3
sq3 = take 20 . powerSequence $ 5
sq4 = take 20 . powerSequence $ 7
main :: IO()
main = do
print sq1
print sq2
print sq3
print sq4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment