The program will not work with really large numbers

I am new to programming, so I am making this C ++ program that will take a number and find it as the main factors, which works great! If it is too big for an int variable. Now I tried to change all int variables for long variables, so that doesn't matter, but that doesn't seem to fix the problem. The program is as follows:

#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 ai 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 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 prime factors: "; cin >> input; } output = multiple(input); cout << output << endl << "finished"; return 0; } 

Now, to be sure, the problem is doing the same thing, whether it is int or not. Also, as I said, I'm new to programming, and C is for that matter, so I look forward to your easy-to-understand answers. :)

+4
source share
2 answers

I am ready to be that your program is running. I am sure that someone is going to appear and give you an answer in the blink of an eye, but I hope that this does not happen so that you experience the same thing as I did when I encountered the problem YEARS ago.

Do this: start from 1 and work from there using forces 2 (1, 2, 4, 8, 16, etc.) and continue moving by doubling the entry number each time. When does it "stop working"? Is it getting slower?

Comment on your post or at your own discretion, edit it or post an answer, regardless of what you are allowed to do with only 56 representatives. If the community allows this (and, of course, I would like the community to advance this lesson), I would like to gently nudge you to the answer through a series of reverse steps, rather than a typical fashion, as this is an obvious unique learning opportunity.

+4
source

If you are trying to find whether a number is prime or not, here is a quick 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; } 
+2
source

All Articles