OK, I am doing problem number 3 from ProjectEuler. (Just to let you know, this is an amazing site if you really want some hard problems to help better your C++ knowledge and mathematical knowledge.)I think Ive got something here... (Maybe I dont) but not a single error or warning comes up and when the console window opens.NOTHING IS THERE!!! PLEASE HELP ME FIX THIS!!! ALSO! DO NOT TELL ME THE ANSWER! THAT IS THE ABSOLUTE WORST THING YOU COULD DO!!! anyway... Ya, I need some help.
#include < iostream>
usingnamespace std;
//Foward declaration of function used to find the remainder
int findRemainder(int x, int y)
{
return x % y;
}
int main()
{
//all variables
int num = 2;
longlongint primeFactor = 600851475143;
int result = primeFactor % num;
do
{
//Function that will find remainder
int findRemainder(primeFactor % num);
//If statements will make sure that only factor of primeFactor are displayed
if (result = 0)
{
cout << num;
}
else
{
;
}
//For loop will test every number until it gets result = 0
for(int num = 3; result = 0; num++)
{
int result = primeFactor % num;
longlongint primeFactor = 600851475143;
int findRemainder(primeFactor % num);
if (result = 0)
{
cout << num;
}
else
{
;
}
}
}while(result = 0);
cin.get();
return 0 ;
}
Dont tell me the answer. >:-(
Thanks!
EDIT:
I know that the number called 'primeFactor isnt really a prime number... or a factor. I will fix that later.
EDIT: Sorry, forgot to say what the program is supposed to do. It is supposed to find a factor of 'primeFactor'. So i made the program go into a loop so that it keeps finding whether the remainder is 0 and when it is, the loop will be terminated.
Remember, a single =, is to assign an value to a variable, whereas a double ==, is to check validity of a value. Your program has many assigns, where they should be checking. Also, for(int num = 3; result = 0; num++), is not a correct for loop. Maybe you should use a do/while loop.
1 2 3 4
do
{
... Your program ...
} while(result!=0);
There are other problems. After creating an int, etc., you do not put the int word in front of the variable when assigning it another value later in the program.
1 2
int my_number = 10;
my_number = 24; // not int my_number = 24;
#include < iostream>
usingnamespace std;
//Foward declaration of function used to find the remainder
int findRemainder(int x, int y)
{
return x % y;
}
int main()
{
//all variables
int num = 2;
longlongint primeFactor = 600851475143;
int result = primeFactor % num;
do
{
//Function that will find remainder
++num;
int findRemainder(primeFactor % num);
if (int result = 0)
{
cout << num;
}
else
{
;
}
}while(result == 0);
cin.get();
return 0 ;
}
Is there anything else wrong? I took out the for loop and just kept the do-while one. I also made sure every int value had integer before it. (except if the type name was not allowed). Is it because my computer isnt able to handle a long long int?
@Hazique35
You're only going to be able to check one or two numbers, before the program ends. So, check with 100 nums instead. Even if you use 1000, it still only prints 3 numbers. But it's a start..
#include < iostream>
usingnamespace std;
//Foward declaration of function used to find the remainder
int findRemainder(int x, int y)
{
return x % y;
}
int main()
{
//all variables
int num = 1;
longlongint primeFactor = 600851475143;
int result;
do
{
//Function that will find remainder
result = findRemainder(primeFactor, num);
if (!result)
{
cout << num << endl;
}
num++; // Increase num after result check
}while(num < 100);
cout << "Program is done!!" << endl;
cin.get();
return 0 ;
}
!result means 'not result' or 'if result == 0', or any negative number. Try removing the !, to leave just the result, and you'll show a lot more numbers .
Not sure why your version didn't show anything, but I'm running this and get numbers up to 99, then it prints "Program is done!!". I changed your large number to 65535. I get the same results, but not the conversion error of possible data loss converting from _int64 to int
#include < iostream>
usingnamespace std;
//Foward declaration of function used to find the remainder
int findRemainder(int x, int y)
{
return x % y;
}
int main()
{
//all variables
int num = 1;
int primeFactor = 65535; // lowered value here from original
int result;
do
{
//Function that will find remainder
result = findRemainder(primeFactor, num);
if (result) // Removed the ! here
{
cout << num << endl;
}
num++; // Increase num after result check
}while(num < 100);
cout << "Program is done!!" << endl;
cin.get();
return 0 ;
}