Return to Snippet

Revision: 66123
at March 18, 2014 08:38 by omarelsaid


Initial Code
#include <iostream>

unsigned long long int largestPrimeFactor(long long int N)
{
	if (N < 0)
		N = -N;
	
	if (N == 0 || N == 1)
		return 0;

	long long int i;
	for (i = 2; i < N; ++i)
		if (N % i == 0)
			return largestPrimeFactor(N / i);
	
	return i;
}

void main()
{
	long long int n;
	std::cout << "Please enter any integer (negative numbers will be treated as positive):" << std::endl;
	std::cin >> n;
	std::cout << "The largest prime factor of " << n << " is " << largestPrimeFactor(n) << std::endl;
	system("pause");
}

Initial URL


Initial Description
This code finds the largest prime factor in any given integer (within the bounds of a long long int)

Initial Title
Largest prime factor

Initial Tags


Initial Language
C++