Answer the question
In order to leave comments, you need to log in
Euler project. Task number 3. Where is the mistake?
I am new to programming. I decided for the experience to solve problems from the Euler project.
3 problem is not solved.
Condition: "The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of 600851475143?"
Here is the code:
#include <iostream>
using namespace std;
long long number = 600851475143;
int del = 0;
int main(int argc, char const *argv[])
{
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
for (int j = 2; j < i; j++)
{
if (i % j == 0)
break;
else
del = i;
}
}
}
cout << del << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
Your number doesn't fit in int. And you drive the loop from 2 to number. Your condition i < number
in the loop will always be fulfilled - because well, int i cannot be greater than number.
edit: And in general, the logic of checking for simplicity is broken. del = i
holds for any j such that i is not divisible by it. Those. if i=6, then with j=5 you will overwrite del. You need to set the bool flag in the loop. And, after the cycle, look at it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question