Project Euler Problem 3: Finding the Largest Prime Factor

In summary, the largest prime factor of the number 600851475143 is 6857. The provided code has some errors, such as using a non-standard header and attempting to store a number larger than the maximum value of a 32-bit signed integer. A more efficient approach is to start at the square root of the number and work backwards.
  • #1
SELFMADE
80
0
What is the largest prime factor of the number 600851475143 ?

My attempt:
Code:
#include <stdafx.h>

int main() 
{
	int num1, num2, num3, num4, lrgprime, sum;
	lrgprime=num2=num4=sum=0;
	for (num1=2; num1<300425737572; num1++) {
		num2=600851475143%num1; 

		if (num2==0) {
			for (num3=2; num3<=num1; num3++) {
				num4=num1%num3;
				if (num4==0 && num1==num3 && num1>=lrgprime)
					lrgprime=num1; 
			}		
		}	
	}
	printf("This is the largest prime factor: %d", lrgprime);
	return 0;
}

The machine is just not running this code

What did I do wrong?
 
Physics news on Phys.org
  • #2
check with some smaller numbers first. see whether you get anything
 
  • #3
Two problems. First, 600851475143 is too large to store in an int (which is usually 32 bits and only goes up to about 2 billion). Try using long longs instead of ints. Second,
Code:
		if (num2==0) {
			for (num3=2; num3<=num1; num3++) {
				num4=num1%num3;
				if (num4==0 && num1==num3 && num1>=lrgprime)
					lrgprime=num1; 
			}		
		}
doesn't do what you want. It eventually always sets lrgprime to num1, even when num1 has divisors. You need to use a break statement to break out of the loop if num4 == 0 and num1 != num3.
 
  • #4
SELFMADE said:
What is the largest prime factor of the number 600851475143 ?

My attempt:
Code:
#include <stdafx.h>

int main() 
{
	int num1, num2, num3, num4, lrgprime, sum;
	lrgprime=num2=num4=sum=0;
	for (num1=2; num1<300425737572; num1++) {
		num2=600851475143%num1; 

		if (num2==0) {
			for (num3=2; num3<=num1; num3++) {
				num4=num1%num3;
				if (num4==0 && num1==num3 && num1>=lrgprime)
					lrgprime=num1; 
			}		
		}	
	}
	printf("This is the largest prime factor: %d", lrgprime);
	return 0;
}

The machine is just not running this code

What did I do wrong?

1) stdafx.h is not a standard header
2) The maximum value of a 32-bit signed int is 2147483647. Of course, there is no reason to use integers for this task. The largest basic type you could use would be "unsigned long long", which has a maximum value of 18446744073709551615
3) Your code is a mess...you have tons of useless auxiliary variables and have split things into multiple lines just making it more confusing than it should be.
4) This is not an efficient approach, and will take too long on a big number.

Code:
#include <iostream>
#include <limits>

typedef unsigned long long BigInt;
using namespace std;

BigInt largest_prime_factor( const BigInt &x )
{
	//find factors
	for(BigInt y = x/2; y>2; --y)
	{
		//check factors
		if( x%y == 0 )
		{
			//check if factor is prime
			for(BigInt z=2; z<y; ++z)
				if( y%z == 0 )
					goto not_prime;
			return y;
		}
not_prime: ;
	}
	return x;
}

int main() 
{

	cout << "Max value = " << numeric_limits<BigInt>::max() << "\n";
	cout << largest_prime_factor( 600851475143 );
}
 
Last edited:
  • #5
Code:
for(BigInt y = x/2; y>2; --y)
Just a suggestion: since the largest prime factor of x cannot not exceed x0=(int)sqrt(x), it pays to start at x0, especially for large numbers.
 
  • #6
Thank you so much, I am going to save this page up, go home and try it. I have been struggling on this problem for 3 days.
 

What is Project Euler problem 3?

Project Euler problem 3 is a mathematical problem that asks for the largest prime factor of a given number.

How do I solve Project Euler problem 3?

There are multiple approaches to solving this problem, but one common solution is to use the "trial division" method, where you divide the given number by smaller prime numbers until you reach the largest prime factor.

What is the input for Project Euler problem 3?

The input for this problem is a single positive integer.

Is there a limit to the size of the input for Project Euler problem 3?

While there is no specific limit stated, it is important to consider the computational resources available when attempting to solve this problem. The larger the input number, the longer it will take to find the solution.

Can I use programming to solve Project Euler problem 3?

Yes, programming is often used to solve Project Euler problems. However, it is recommended to first attempt to solve the problem mathematically before using programming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
5K
  • Engineering and Comp Sci Homework Help
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
7K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
Back
Top