reset password
Author Message
rabbott
Posts: 1649
Posted 17:02 Aug 27, 2016 |

Instead of searching for primes p you could search for constants k.

oddsFrom3 = [3, 5 .. ]

primeDivisors n = [d | d <- takeWhile ((<= n) . (^2)) primes, n `mod` d == 0]

primes = 2 : [p | p <- oddsFrom3, null (primeDivisors p)]

-- Michael Fong suggested using dropWhile for isPrime.
isPrime g = g == head (dropWhile (< g) primes)

-- In the following we are looking for a k such that g - 2 * k * k is prime.  We limit the search
-- to k's such that g > 2 * k ^ 2.
goldbachFails = [g | g <- takeWhile (< 6000) oddsFrom3, not (isPrime g), 
                                null [k | k <- takeWhile ((<g) . (*2) . (^2)) [1 .. ], isPrime (g - 2 * k * k)]   ]

-- Or search for primes as before.
isSquare n = (floor (sqrt (fromIntegral n))) ^ 2 == n 
-- Why use fromIntegral rather than fromInteger? See Converting numbers on the Haskell website.

goldbachFails' = [g | g <- takeWhile (< 6000) oddsFrom3, not (isPrime g), 
                                null [p | p <- takeWhile (<g) primes, isSquare ((g - p) `div` 2)]   ]

Last edited by rabbott at 12:48 Aug 28, 2016.