variables - C++ Prime factor program 2 problems -


OK, so I am writing a program (in C ++) that is to take the number, go through it , Find out if this factor is the key, if so, add it to an amount, and then generate the sum of the key factors of all the numbers attached.

My program seems to be doing this successfully, but it has 2 problems, P>

1) I am the number to examine the number of key factors of this number (600851475143) But it is too big for int. I'm not sure how to use other variables or to change the type of variables. I would really like a clear explanation if all possible.

2) For some reason, when the program checks to see that there is a factor of 1 number, and then check to see that 1 is the head, it says that 1 is the key, even if Only to see if it is prime that if it is 1, then it is not primary. Subtracting 1 from the very last value for 1 is the main factor, however, it is a fix, in fact the problem is not finding, if at least it can tell what the problem is, then I appreciate it!

If you have questions here, please ask!

  #include & lt; Iostream & gt; using namespace std; Bool prime (int recievedvalue) {// starts a function that gives a boolean with the parameter, which is a factor from the number int j = 1; Int balance = 0; Bull end = wrong; Whereas (end == incorrect) {// run loop while primitization is uncertain if (recipient == 1) {/ if the value received is 1 then it is not prime minister / not prime minister; // break loop return false; } Balance = recipient =% j; // gives a balance for the test if ((light illumination == 0 & amp; amp; & gt; 2) & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; J == 4)) Head ends = true; // No major return false; } And if (J == 1) {j ++; } And if (recipient == 2 || Jammu == has been received) {// it shows what is this prime state = true; // main return true; } Other {j ++; }}} Int multi (int tbfactor) {// factor and then to check whether the factors are major, then all the major factors add together // parameter number is intact = 0; Bool primetest = false; Int remaining facts; Int i = 1; While (i & lt; = tbfactor) {// check is an i tbfactor balance factor = tbfactor% i; If (the remaining facts == 0) {// If it is a factor, then it checks that it is a prime primitiv = prime (i); } If (primitest == true) {// If it is the principal then it adds to the sum amount + i =; Primetest = false; } I ++; } Sum -; // For some reasons it always gives ad 1 as a key number, so it's okay for me; } Int main () {int input; Integer output; Cout & lt; & Lt; "Enter a number to find the sum of all, its main factors are:"; Cin & gt; & Gt; Input; Output = multiple (input); Cout & lt; & Lt; Output; Return 0; }  

I'm really new to this, like a few days or so, so I'm just unfamiliar with the goods so please explain to me easily! I look forward to helping you! Thanks!

for 1), you have to use a large datatype. A 64-bit integer should be sufficient here, so whatever is called 64-bit integer type (maybe long , or maybe on your platform from your int Code> long time ).

for 2), the problem appears that your return is before your return is locked to prevent the loop of the code immediately And the execution continues immediately after the loop. It does not appear that the return value is given at any given position (which will give your compiler need to warn you), so the actual value is effectively arbitrarily wrong.


Comments