1

Hello I am trying to make a function goldbach :: Integer -> Bool for interval [4..n] that should return True only if all elements in that interval is even and can be summed from two prim numbers. So far I have done this.

goldbach::Integer->Bool
goldbach n 
    |goldbach2 n == ??? = goldbach (n-2)
    |n==4 = True
    |otherwise = False

goldbach2 :: Integer -> (Integer, Integer)
goldbach2 a = head $
                 filter (\(x,y) -> isPrime x && isPrime y) $
                 map (\e -> (e, a - e)) [3,5..a `div` 2]
where
factors a = filter (isFactor a) [2..a-1]
isFactor a b = a `mod` b == 0
isPrime a = null $ factors a

Function golbach2 result looks like this goldbac2 28 = (5, 23). How should I chek in my 3rd line goldbach2 n == ??? = goldbach (n-2) if result of goldbach2 n is correct and gives me two prim numbers?

Lukas Karmanovas
  • 409
  • 1
  • 4
  • 14
  • Your description must be wrong, for, the only qualifying intervals are [] and `[4..4]` since in all the others there is at least one odd number. Hence, a short version of what you describe would be `g = (<=4)` – Ingo Dec 04 '16 at 12:15
  • I don't get it how can that be. If I would enter `n=8` I should have a list `[4,6,8]` and it should return True. – Lukas Karmanovas Dec 04 '16 at 12:27
  • 1
    Yes, you actually don't construct the interval `[4..n]` in your code. In the description, what you really mean is `[4,6 .. n]` – Ingo Dec 04 '16 at 12:55
  • So how should i change it so that it would be correct? – Lukas Karmanovas Dec 04 '16 at 14:21

2 Answers2

1

If I understand it correctly, you want something like:

 | let (n1, n2) = goldbach2 n in n == n1 + n2   =  goldbach (n-2)
Paul Johnson
  • 17,438
  • 3
  • 42
  • 59
0

I would write this like

goldbachInterval n = all goldbach [4,6 .. n]
   where
       goldbach k = .... -- property of being a golbach number
Ingo
  • 36,037
  • 5
  • 53
  • 100