欧拉计划 第四十六题

prime and twice a square.

9 = 7 + 2×1215 = 7 + 2×2221 = 3 + 2×3225 = 7 + 2×3227 = 19 + 2×2233 = 31 + 2×12

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

由Christian Goldbach提出,每个奇数的复合数可以写成素数和两个平方的总和。

9 = 7 + 2×1 215 = 7 + 2×2 221 = 3 + 2×3 225 = 7 + 2×3 227 = 19 + 2×2 233 = 31 + 2×1 2

事实证明,猜想是错误的。

什么是最小的奇数复合,不能写为素数和两倍平方的总和?

思路

1.与素数相关,需要用到素数筛

2.所有素数满足猜想

3.对奇数进行筛选

#include <stdio.h>

#define max 10000

int prime[max + 5] = {0};
int sum[max + 5] = {0};

int main(){
	for(int i = 2; i <= max; i++){
		if(!prime[i]){prime[++prime[0]] = i; sum[i] = 1;}
		for(int j = 1; j <= prime[0]; j++){
			if(prime[j] * i > max) break;
			prime[prime[j] * i] = 1;
			if(i % prime[j] == 0) break;
		}
	}
	for(int i = 1; i <= prime[0]; i++){
		for(int n = 1; ; n++){
			if(prime[i] + 2 * n * n > max) break;
			sum[prime[i] + 2 * n * n] = 1;
		}
	}
	for(int i = 11; i < max; i += 2){
		if(!sum[i]){
			printf("%d\n",i);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/81005165