/ Published in: C++
This code finds the largest prime factor in any given integer (within the bounds of a long long int)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#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"); }