Repeating Goldbachs

该文描述了一个基于Goldbach猜想的编程问题,即任何大于等于4的偶数可以表示为两个素数之和。给出了一段C++代码,用于找到将给定偶数减少至小于4所需的最少步骤,通过只枚举到x/2来优化效率。在每个步骤中,寻找差值最大的素数对并更新数值。
摘要由CSDN通过智能技术生成

 The Goldbach Conjecture states that any even number x ≥ 4 can be expressed as the sum of two primes. It can be verified that the conjecture is true for all x ≤ 106 .

Define a Goldbach step as taking x (4 ≤ x ≤ 106 ), finding primes p and q (with p ≤ q) that sum to x, and replacing x with q − p. If there are multiple pairs of primes which sum to x, we take the pair with the largest difference. That difference must be even and less than x. Therefore, we can repeat more Goldbach steps, until we can reach a number less than 4.

Given x, find how many Goldbach steps it takes until reaching a number less than 4.

Input

The input will consist of a single integer x (4 ≤ x ≤ 106 ).

Output Print,

on a single line, the number of Goldbach steps it takes to reach a number less than 4.

就是一个关于哥德巴赫的小扩展吧,之前在学习做过相识的,关键技巧就是只需要枚举到x/2,

for循环时用i,和x-i,这样就不用开双重循环了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e6+10; 
int ans;
bool fun(int x)
{
  if(x<2)
  return 0;
  for(int i=2;i<=x/i;i++)
  if(x%i==0)
  return 0;
  return 1;
}
int main()
{
  int x;
  cin>>x;
  while(1)
  {
    if(x<4)
    break;
    for(int i=2;i<=x/2;i++)
    {
      if(fun(i)&&fun(x-i))
      {
        x=abs(x-i-i);
        if(abs(x-i-i)%2==0)
        break;
      }
    }
     ans++;
  }
  cout<<ans;
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值