fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4.  
  5. const int MAXN = 1e6;
  6. vector<int> spf(MAXN + 1, 0);
  7.  
  8. void computeSpf(){
  9. for (int i = 2; i <= MAXN; i++){
  10. spf[i] = i;
  11. }
  12.  
  13. // starting the sieve process
  14.  
  15. for (int i = 2; i * i <= MAXN; i++){
  16. if(spf[i] == i){
  17. for (int j = i*i; j <= MAXN; j += i){
  18. if(spf[j] == j){
  19. spf[j] = i;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. }
  26.  
  27.  
  28. signed main() {
  29. ios_base::sync_with_stdio(0);
  30. cin.tie(0); cout.tie(0);
  31.  
  32. // int t; cin >> t;
  33. // while (t--) {
  34. // solve();
  35. // }
  36. computeSpf();
  37. vector<int> input = {2, 14, 11, 36, 49, 7, 21, 44, 69, 501, 67, 1000};
  38.  
  39. for (int i : input){
  40. cout << "The smallest prime Factor of " << i << " is: " << spf[i] << endl;
  41. }
  42. return 0;
  43. }
Success #stdin #stdout 0.02s 10860KB
stdin
Standard input is empty
stdout
The smallest prime Factor of 2 is: 2
The smallest prime Factor of 14 is: 2
The smallest prime Factor of 11 is: 11
The smallest prime Factor of 36 is: 2
The smallest prime Factor of 49 is: 7
The smallest prime Factor of 7 is: 7
The smallest prime Factor of 21 is: 3
The smallest prime Factor of 44 is: 2
The smallest prime Factor of 69 is: 3
The smallest prime Factor of 501 is: 3
The smallest prime Factor of 67 is: 67
The smallest prime Factor of 1000 is: 2