main = interact $ unlines . map (show . goldbach . read) . takeWhile (/= "0") . lines

goldbach :: Int -> Int
goldbach x = if odd x then 0 else goldbach' (takeWhile (<= div x 2) p) (takeWhile (>= div x 2) (reverse p))
 where
  p = takeWhile (<= x) primes
  goldbach' (p1:ps1) (p2:ps2)
   | p1 == p2   = if p1+p2==x then 1 else 0
   | p1+p2 < x  = goldbach' ps1 (p2:ps2)
   | p1+p2 > x  = goldbach' (p1:ps1) ps2
   | p1+p2 == x = 1 + goldbach' ps1 ps2
  goldbach' _ _ = 0
 
primes = 2:3:[x | i<-[1..], j<-[-1,1], let x = 6*i+j, isPrime x]
 where isPrime n = null [i | i<-takeWhile (\x -> x*x <= n) primes, rem n i == 0]