Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active February 10, 2026 05:11
Show Gist options
  • Select an option

  • Save dgodfrey206/927a269a57ba0b57ea31c271eb2ef69a to your computer and use it in GitHub Desktop.

Select an option

Save dgodfrey206/927a269a57ba0b57ea31c271eb2ef69a to your computer and use it in GitHub Desktop.
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of the number 600851475143?
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
int sieve() {
ll n = 600851475143;
ll sz = sqrt(n);
vector<bool> prime(sz, true);
prime[1] = prime[2] = true;
for (int i = 2; i < sz; i++)
for (int k = i + i; k < sz; k += i)
prime[k] = false;
for (int i = sz - 1; i > 1; i--)
if (prime[i] && n % i == 0)
return i;
return 1;
}
int main() {
cout << sieve();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment