1. C++, Tobias Fuchs

/* Problem: 686
* Author: Tobias Fuchs
* Verdict: Accepted
* Runtime: 0.010
*/

#include<iostream>
#include<string>

#define log(m) ::std::cout << "log: '" << m << "'" << ::std::endl

using ::std::cin;
using ::std::string;

/* max input value is 215 = 32.768
* -> sqrt(32.768) = 181.02 -> 182
*/
#define MAX_PRIME_SQRT 182
#define MAX_PRIME (MAX_PRIME_SQRT*MAX_PRIME_SQRT)

/* We want linearized access to this map, so
* do not use ::std::vector, but a simple
* 2-dim array of integers.
*/
bool primes[MAX_PRIME_SQRT][MAX_PRIME_SQRT];

/* Provides serialized accesso to map of primes.
* Map is generated using Sieve of Eratosthenes.
*/
inline bool
is_prime(unsigned int n)
{
return (*primes)[n];
}

/* Building map of primes.
* Implements Sieve of Erastothenes.
*/
void
build_primes()
{
unsigned int step = 2;
unsigned int mark = 1;
memset(primes, true, MAX_PRIME);
while(step < MAX_PRIME_SQRT) {
mark = step;
while(mark+step < MAX_PRIME) {
mark += step;
(*primes)[mark] = false;
}
do { ++step; } while(!is_prime(step));
}
(*primes)[0] = false;
(*primes)[1] = false;
}
void
set_to_next_prime(unsigned int * value_ref) {
do { ++(*value_ref); } while(!is_prime((*value_ref)));
}
void
set_to_prev_prime(unsigned int * value_ref) {
do { --(*value_ref); } while(!is_prime((*value_ref)));
}

int
main()
{
build_primes();

unsigned int sum, a, b, solutions;
string sum_str;
bool done;

while(cin >> sum_str && (sum = atoi(sum_str.c_str())) != 0)
{
a = 2;
b = sum;
done = false;
solutions = 0;
while(!done && a <= b) {
if(a+b < sum) { set_to_next_prime(&a); }
else if(a+b > sum) { set_to_prev_prime(&b); }
if(a+b == sum) {
solutions++;
a++;
}
if(a > b) { done = true; }
}
::std::cout << solutions << ::std::endl;
}
return 0;
}