Submission
Status:
-PPPPPPPPP-PPPPPPPPP
Subtask/Task Score:
90/100
Score: 90
User: KanemiyaKana
Problemset: จำนวนเฉพาะก่อนหน้า
Language: cpp
Time: 0.030 second
Submitted On: 2026-09-30 18:32:44
#include <iostream>
#include <vector>
using namespace std;
vector<int> sieve(int thelast)
{
vector<bool> primenum(thelast + 1, true);
primenum[0] = false;
primenum[1] = false;
for (int num = 2; num * num <= thelast; num++) // just illiterating from 2 to the sqr of the final number
{
if (primenum[num] == true) // means if the current number IS a prime
{
for (int i = num * num; i <= thelast; i += num) // discarding multiples of the number that has been checked as prime in the above if, example if the number that has been checked true is 2 then this is simply just telling you to start fro 2*2 which is 4 and add 2 each time to discard the multiples of it until the point of the last number
{
primenum[i] = false; // discarded non-primes
}
}
}
vector<int> returnnum;
for (int p = thelast; p > 0; p--)
{
if (primenum[p])
{
returnnum.push_back(p);
}
if (returnnum.size() == 5) // size instead of length for vector
{
break;
}
}
return returnnum;
}
int main()
{
int n;
cin >> n;
if (n < 0)
{
return 0;
}
else
{
vector<int> output = sieve(n);
for (int i = int(output.size()) - 1; i >= 0; i--)
{
cout << output[i] << " ";
}
}
}