reset password
Author Message
rabbott
Posts: 1649
Posted 11:40 Sep 14, 2017 |

Most people wrote code for Project 1 that looks something like this.

goldbachFails = take 2 [g | g <- oddsFrom3, not (isPrime g), null (goldbachPrimes g) ]

goldbachPrimes g = [(p, k) | p <- primes, isAPerfectSquare ((g-p) `div` 2] 

Although it's not difficult, many people had trouble explaining how that code finds numbers that fail Goldbach's other conjecture.

You might try the following instead.

goldbachFails = take 2 [g | g <- oddsFrom3, not (isPrime g), null (goldbachPairs g) ]

goldbachPairs g = [(p, k) | k <- takeWhile (\n -> 2*n^2 < g) [1 .. ], let p = g - 2*k^2, isPrime p] 

Don't forget that Goldbach's conjecture says that for every odd composite number g there is a prime p and an integer k such g = p + 2*k^2. You should be able to explain how this code finds numbers that fail Goldbach's conjecture. The key term is "there is." Use that in your explanation.

Last edited by rabbott at 20:28 Sep 15, 2017.
rabbott
Posts: 1649
Posted 20:21 Sep 15, 2017 |

In the project description I mentioned the trace function. So far I haven't seen anyone use it. I urge you to look into it.  Here's a way to use it to follow the progress of goldbachPairs as it searches for p's and k's. 

goldbachPairs g = trace ("\n       g: " ++ show g ++ "\n       p? k") 
                  [(p, k) | k <- takeWhile (\n -> 2*n^2 < g) [1 .. ], 
                            let p = g - 2 * k^2, 
                            let pIsPrime = isPrime p,
                            trace ((if pIsPrime then "
==> " else "     ") ++ show (p, k)) pIsPrime 
                  ]

> goldbachPairs 175

         g: 175
         p?   k
==> (173,1)
==> (167,2)
==> (157,3)
        (143,4)
        (125,5)
==> (103,6)
        (77,7)
==> (47,8)
==> (13,9)
[(173,1),(167,2),(157,3),(103,6),(47,8),(13,9)]

 

In the above output, each line explores a possible k value. (The k values are the second elements of the pair.) Given a possible k value the code computes a possible p value, which it tests for primality. An arrow at the start of a row indicates that the possible p value is in fact a prime. You can see the list of valid (p, k) values at the end of the output.

Last edited by rabbott at 12:30 Sep 16, 2017.