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. :)
source share