Skip to content

Instantly share code, notes, and snippets.

@mstahl
Created February 16, 2009 08:00
Show Gist options
  • Save mstahl/65069 to your computer and use it in GitHub Desktop.
Save mstahl/65069 to your computer and use it in GitHub Desktop.
-- The smallest number expressible as the sum of a prime square,
-- prime cube, and prime fourth power is 28. In fact, there are
-- exactly four numbers below fifty that can be expressed in
-- such a way:
--
-- 28 = 2^(2) + 2^(3) + 2^(4) 33 = 3^(2) + 2^(3) + 2^(4) 49 =
-- 5^(2) + 2^(3) + 2^(4) 47 = 2^(2) + 3^(3) + 2^(4)
--
-- How many numbers below fifty million can be expressed as the
-- sum of a prime square, prime cube, and prime fourth power?
--
module Main where
import Data.List
import Primes
import System
primes = filter (prime) [2..]
squares n = takeWhile (< n) $ map (^2) primes
cubes n = takeWhile (< n) $ map (^3) primes
fourths n = takeWhile (< n) $ map (^4) primes
sums n = nub [a + b + c | a <- (squares n)
, b <- (cubes n)
, c <- (fourths n)
, a + b + c < n]
main = do args <- getArgs
let n = (read (args !! 0))::Int
print $ sums n
print $ length $ sums n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment