简体   繁体   English

程序不会以非常大的数字运行

[英]Program won't run with really big numbers

I'm pretty new to programming and so I"m making this program in C++ that will take a number and find it's prime factors, which works great! Unless it's too big for an int variable. Now then I tried to change all of the int variables to long long variables so it wouldn't matter, but this doesn't seem to fix the problem. The program is as follows: 我对编程很陌生,所以我用C ++编写这个程序,需要一个数字并找到它的主要因素,这很有效!除非它对于一个int变量太大了。现在我试着改变所有的int变量到long long变量所以没关系,但这似乎没有解决问题。程序如下:

#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;
}

Now then to be sure, the problem does the same thing whether or not it's a int or not. 现在可以肯定的是,问题是否与int无关。 Also like I said I"m new to programming, and C for that matter so I look forward to your easily understandable replies. :) 也像我说的那样我是编程新手,而C就是这个问题,所以我期待你的回答容易理解。:)

I'm willing to be that your program IS running. 我愿意你的程序正在运行。 I'm sure that someone is going to pop on and give you the answer in a heartbeat, but I'm hoping that it doesn't happen so that you get to experience the same thing that I did when I ran into the problem YEARS ago. 我确信有人会在心跳中弹出并给你答案,但我希望它不会发生,这样你就可以体验到与我遇到问题时所做的相同的事情。前。

Do this: start with 1, and work up from there using powers of 2 (1, 2, 4, 8, 16, etc.) and just keep going, doubling the input number each time. 这样做:从1开始,然后使用2(1,2,4,8,16等)的功率从那里开始工作并继续前进,每次加倍输入数字。 When does it "stop running?" 什么时候“停止运转?” Does it get progressively slower? 它会逐渐变慢吗?

Please comment back on my post or on your own, or edit your own, or post an answer, whatever it is you're allowed to do with only 56 rep. 请回复我的帖子或自己评论,或编辑自己的帖子,或发布答案,无论你只允许56个代表做什么。 If the community will allow it (and of course I would like the community to further the lesson), I'd like to gently push you to the answer through a series of back-and-forth steps feedback rather than the typical fashion, since this is an obvious unique learning opportunity. 如果社区允许它(当然我希望社区能够继续上课),我想通过一系列来回反馈而不是典型的方式,轻轻地推动您找到答案,因为这是一个明显独特的学习机会。

If you are trying to find if a number is a prime or not, here is a fast solution, 如果你试图找出一个数字是否是素数,这是一个快速的解决方案,

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM