代码之家  ›  专栏  ›  技术社区  ›  samuraiseoul

这个程序不会用大数字运行

  •  4
  • samuraiseoul  · 技术社区  · 14 年前

    #include <iostream>
    
    using namespace std;
    
    bool prime (long long recievedvalue) { //starts a function that returns a boolean with parameters being a factor from a number
        long long j =1;
        long long remainderprime = 0;
        bool ended = false;
        while (ended == false){ //runs loop while primality is undetermined
            if (recievedvalue == 1){ //if the recieved value is a 1 it isn't prime
                //not prime
                return false;
                break; // breaks loop
                }
            remainderprime=recievedvalue%j; //gives a remainder for testing
            if ((remainderprime==0 && j>2) && (j!=recievedvalue || j == 4)){ //shows under which conditions it isn't prime
            ended=true;
            //not prime
            return false;
            }
            else if (j==1){
                j++;
                }
            else if ( recievedvalue==2 || j==recievedvalue ){ // shows what conditions it is prime
              ended = true;
              //prime
              return true;
                }
                else {
                j++;
                }
            }
        }
    
    
    long long multiple(long long tbfactor){ //factors and then checks to see if factors are prime, then adds all prime factors together
        //parameter is number to be factored
        long long sum = 0;
        bool primetest = false;
        long long remainderfact;
        long long i=1;
        while (i<=tbfactor){ //checks if a i is a factor of tbfactor
            remainderfact=tbfactor%i;
            if (remainderfact==0){ //if it is a factor it checks if it is a prime
                primetest = prime(i);
            }
            if (primetest ==true){ //if it is prime it add that to the sum
                sum += i;
                primetest=false;
            }
            i++;
        }
        return sum;
    }
    
    int main()
    {
        long long input;
        long long output;
        cout << "Enter a number > 0 to find the sum of all it's prime factors: ";
        cin >> input;
        if (input == 0 || input <= 0){
            cout << "The number you entered was too small."<< endl << "Enter number a number to find the sum of all it's prime factors: ";
        cin >> input;
            }
        output = multiple(input);
        cout << output << endl << "finished";
        return 0;
    }
    

    现在可以肯定的是,无论它是否是int,问题都会做同样的事情。就像我说的,我是编程新手,所以我很期待你的回复。:)

    2 回复  |  直到 14 年前
        1
  •  4
  •   San Jacinto    14 年前

    我很乐意你的程序正在运行。我肯定会有人突然出现,马上给你答案,但我希望这不会发生,这样你就可以体验到我几年前遇到这个问题时做的同样的事情。

    这样做:从1开始,然后用2的幂(1,2,4,8,16等)继续,每次输入两倍。它什么时候“停止运行?”是不是越来越慢了?

        2
  •  2
  •   josh    14 年前

    如果你想知道一个数是否是素数,这里有一个快速的解决方案,

    #include <iostream>
    
    using namespace std;
    
    #define ullong unsigned long long
    
    bool prime (ullong x)
    {
        if(x <= 1)
            return false;
    
        ullong s = (ullong)sqrt(x);
    
        for(ullong i=2;i<=s;i++)
            if(x%i == 0)
                return false;
    
        return true;
    }