C++ 跳入c++;(素因子树的实践)
我做了以下练习: 设计一个程序,查找从1到1000的所有数字,当这些数字的素数加在一起时,它们的素数加起来就是一个素数(例如,12的素数为2、2和3,它们加起来就是7,这就是素数)。实现该算法的代码 我的代码:C++ 跳入c++;(素因子树的实践),c++,function,primes,C++,Function,Primes,我做了以下练习: 设计一个程序,查找从1到1000的所有数字,当这些数字的素数加在一起时,它们的素数加起来就是一个素数(例如,12的素数为2、2和3,它们加起来就是7,这就是素数)。实现该算法的代码 我的代码: #include <iostream> #include <string> //function prototype int PrimeFactor(int number); bool isPrime(int PrimeOrNot); int main()
#include <iostream>
#include <string>
//function prototype
int PrimeFactor(int number);
bool isPrime(int PrimeOrNot);
int main() {
int i;
for (i = 0; i < 1000; i++)
PrimeFactor(i);
}
int PrimeFactor(int number) {
int i;
int firstsplit = 0;
int secondsplit = 0;
int nextSplit = 0;
int afternextsplit = 0;
int sum = 0;
for (i = 2; i < number; i++) //loop that find the number that split it to two number , the purpose is for buiding the factor tree.
{
if (number % i == 0) {
firstsplit = number / i;
secondsplit = number / firstsplit;
if (isPrime(firstsplit))
sum = sum + firstsplit;
if (isPrime(secondsplit))
sum = sum + secondsplit;
break;
}
}
if (isPrime(firstsplit) == false) //check if the splitted number is not already a factor from previous loop.
{
while (isPrime(firstsplit) == false) {
for (i = 2; i < firstsplit; i++) //loop that continue to split the splitted number for the purpose of prime factors.
{
if (firstsplit % i == 0) {
nextSplit = firstsplit / i;
afternextsplit = firstsplit / nextSplit;
if (isPrime(nextSplit))
sum = sum + nextSplit;
if (isPrime(afternextsplit))
sum = sum + afternextsplit;
}
}
}
}
}
bool isPrime(int PrimeOrNot) {
int i;
for (i = 0; i < PrimeOrNot; i++) {
if (PrimeOrNot % i == 0)
return false;
}
return true;
}
16
/ \
4 4
/\ /\
2 2 2 2
这是解决任务的一种有效方法。我用的是筛子,你可以参考一下
#包括
#包括
使用名称空间std;
#定义N 1000
int标志[N+1];//标志[i]=0表示我是素数,标志[i]=1表示我不是素数
整数素数[N+1];//素数[i]包含第(i+1)个素数,如素数[0]=2,素数[1]=3,素数[2]=5。。。
int len=0;//len确定填充的素数数组的长度
void sieve(){//此函数计算素数数组中小于N的所有素数,素数的个数以len为单位。
对于(inti=2;iSo)来说,程序本身运行良好,您基本上只需要一个?@JoachimPileborg,不,代码不工作(程序未完成),我想问的是,是否有人可以告诉我这是一种方式(我确实知道有一种不止一种的方法来适应一个程序,这很神奇,但在我看来,这种程序有很多变量,而且更多,所以如果有人看到代码并告诉我这不是一种方法,试着思考一下(abc)或者是一个更好的方法。如果你只是在学习,集中精力完成工作!在我看来,你目前做得很好。我不知道代码是否运行,但你有一些函数可以执行函数,并且充分利用局部变量,而不是将所有变量都放入全局池。唯一的一件事:一个承诺返回<代码的函数>int
应该这样做-或者使其无效
。如果遇到实际问题,请返回此处,如果它运行但您认为可以更好,请在代码审阅时提交。您有两个问题:1)是否有更好的方法来解决此问题。2)有人可以复习我的代码。第一个问题是关于这个网站的话题。第二个问题更适合于CooDeVIEW网站。如果学习C++是你的目标,你可能想遵循你自己的想法直到完成。只有当你有一个工作程序时,才看别人的解决方案。<代码>我的程序似乎不是很有效率,因为有一个程序。很多变量这是一个错误的假设,至少在这个编程级别上是这样。不要担心变量的数量,特别是因为你养成了给它们取有意义的名字的好习惯。
#include<iostream>
#include<cmath>
using namespace std;
#define N 1000
int flag[N+1]; // flag[i] =0 means i is prime, flag[i] =1 means i is not prime
int primes[N+1]; // primes[i] contains the (i+1)th prime, like prime[0]=2, prime[1]=3, prime[2]=5...
int len=0; // len determines the length of primes array filled
void sieve(){ // this function calculates all the primes less than N in primes array and number of primes is in len.
for( int i=2; i<=sqrt(N); i++ ){
if( flag[i] == 0 )
for( int j=2*i; j<=N; j+=i )
flag[j] = 1;
}
// now after this first loop flag[i] is ready to determine prime
for( int i=2; i<=N; i++ )
if( flag[i] == 0 )
primes[len++] = i;
// now primes contain the primes
}
bool isFactorSumPrime( int x ){ // this functions check whether sum of prime factors is prime or not
int sum = 0; // sum of factors
for( int i=0; i<len; i++ ){
if( x==1 ) break; // x has no prime factors
while( x%primes[i] == 0 ){ // if primes[i] is factor of x
sum += primes[i];
x /= primes[i];
}
}
return !flag[sum];
}
int main(){
sieve();
for( int i=2; i<=N; i++ )
if( isFactorSumPrime(i) == true )
cout << i << endl;
return 0;
}