还是哥德巴赫猜想,和前面那题代码几乎差不多。。直接copy过来改两下完事了。。
#include<stdio.h>
#include<string.h>
#include<math.h>
int is_prime(int* primes, int num)
{
int i, n = (int) sqrt(num);
for (i = 0; primes[i] != -1 && primes[i] <= n; i++)
if (num % primes[i] == 0)
return 0;
return 1;
}
int is_in_prime_array(int* primes, int len, int num)
{
int low = 0, high = len - 1, center = (low + high) / 2;
while (primes[center] != num)
{
if (low >= high)
return 0;
if (primes[center] < num)
low = center + 1;
else
high = center - 1;
center = (low + high) / 2;
}
return 1;
}
int main()
{
int n, i, primes[7000], index = 0;
memset(primes, -1, 7000 * sizeof(int));
primes[index++] = 2;
for (i = 3; i < 2 << 15; i += 2)
if (is_prime(primes, i))
primes[index++] = i;
while (scanf("%d", &n), n)
{
int count = 0;
for (i = 0; primes[i] <= n / 2; i++)
if (is_in_prime_array(primes, index, n - primes[i]))
count++;
printf("%d\n", count);
}
return 0;
}