Advertisement
Shuva_Dev

Factorial Factors (UVa) - Less Complexity

Jan 18th, 2023
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int M = 1 << 16;
  4.  
  5. vector<int> primes;
  6.  
  7. void sieve()
  8. {
  9.     vector<bool> comp(M + 5, 0);
  10.     for (int i = 2; i * i <= M; i++)
  11.     {
  12.         if (!comp[i])
  13.         {
  14.             for (int j = i * i; j <= M; j += i)
  15.             {
  16.                 comp[j] = 1;
  17.             }
  18.         }
  19.     }
  20.     primes.clear();
  21.     primes = {2};
  22.     for (int i = 3; i <= M; i += 2)
  23.     {
  24.         if (!comp[i])
  25.         {
  26.             primes.push_back(i);
  27.         }
  28.     }
  29. }
  30.  
  31. bool is_prime(long long n)
  32. {
  33.     for (int i = 0; i < primes.size() && primes[i] * primes[i] <= n; i++)
  34.     {
  35.         if (n % primes[i] == 0)
  36.         {
  37.             return false;
  38.         }
  39.     }
  40.     return true;
  41. }
  42.  
  43. int main()
  44. {
  45.     sieve();
  46.     int T;
  47.     scanf("%d", &T);
  48.     while (T--)
  49.     {
  50.         long long n;
  51.         scanf("%lld", &n);
  52.         for (auto i = n - 1; i > 1; i--)
  53.         {
  54.             if (is_prime(i))
  55.             {
  56.                 printf("%lld\n", i);
  57.                 break;
  58.             }
  59.         }
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement