Note that I didn't do any benchmark, neither did I run a profiler on the code. This is just from reading your code. You should do profiling yourself if this is really time critical. (But then you should probably be using another method instead of the Sieve of Erasthothenes).
Reserve space for your primes
You know that you'll be looking for primes below primeCap = 1e10;
. The number of primes below x
is pi(x)
and can be approximated well with x/(log x - 1)
. So use that to reserve space beforehand in order to avoid (potentially costly) allocations.
What's the sign useful for?
Primes are positive. Why do you use (signed
) long int
when you could use unsigned
(long int
) numbers? Though keep in mind that some object against using unsigned integer datatypes as there are some pitfalls regarding "the usual arithmetic conversions".
Two times 2
The prime 2
will be twice in your vector. Change the initialization of the loop variable i
to i = 3
to solve that.
{
and }
for everyone
This is a very opinionated argument here, but consider using {}
even if there's only a single statement after an if
(or for
). We had some pretty popular bugs (partly) due to this "syntax shortcut" recently.
Notes:
- Don't try to optimize the
sqrt(i)
. Don't try to optimize thesqrt(i)
. - Some may object against the
break
. I don't think that it's an issue here, as the flow of control is pretty obvious.
There may also be an issue where the very last item in the vector is set to
2
. [..]
I have no idea what you want to suggest here. With the change you mention I definitively do not get 2
as last output (unless primeCap
is set accordingly).
Note that I didn't do any benchmark, neither did I run a profiler on the code. This is just from reading your code. You should do profiling yourself if this is really time critical. (But then you should probably be using another method instead of the Sieve of Erasthothenes).
Reserve space for your primes
You know that you'll be looking for primes below primeCap = 1e10;
. The number of primes below x
is pi(x)
and can be approximated well with x/(log x - 1)
. So use that to reserve space beforehand in order to avoid (potentially costly) allocations.
What's the sign useful for?
Primes are positive. Why do you use (signed
) long int
when you could use unsigned
(long int
) numbers? Though keep in mind that some object against using unsigned integer datatypes as there are some pitfalls regarding "the usual arithmetic conversions".
Two times 2
The prime 2
will be twice in your vector. Change the initialization of the loop variable i
to i = 3
to solve that.
{
and }
for everyone
This is a very opinionated argument here, but consider using {}
even if there's only a single statement after an if
(or for
). We had some pretty popular bugs (partly) due to this "syntax shortcut" recently.
Notes:
- Don't try to optimize the
sqrt(i)
. - Some may object against the
break
. I don't think that it's an issue here, as the flow of control is pretty obvious.
There may also be an issue where the very last item in the vector is set to
2
. [..]
I have no idea what you want to suggest here. With the change you mention I definitively do not get 2
as last output (unless primeCap
is set accordingly).
Note that I didn't do any benchmark, neither did I run a profiler on the code. This is just from reading your code. You should do profiling yourself if this is really time critical. (But then you should probably be using another method instead of the Sieve of Erasthothenes).
Reserve space for your primes
You know that you'll be looking for primes below primeCap = 1e10;
. The number of primes below x
is pi(x)
and can be approximated well with x/(log x - 1)
. So use that to reserve space beforehand in order to avoid (potentially costly) allocations.
What's the sign useful for?
Primes are positive. Why do you use (signed
) long int
when you could use unsigned
(long int
) numbers? Though keep in mind that some object against using unsigned integer datatypes as there are some pitfalls regarding "the usual arithmetic conversions".
Two times 2
The prime 2
will be twice in your vector. Change the initialization of the loop variable i
to i = 3
to solve that.
{
and }
for everyone
This is a very opinionated argument here, but consider using {}
even if there's only a single statement after an if
(or for
). We had some pretty popular bugs (partly) due to this "syntax shortcut" recently.
Notes:
- Don't try to optimize the
sqrt(i)
. - Some may object against the
break
. I don't think that it's an issue here, as the flow of control is pretty obvious.
There may also be an issue where the very last item in the vector is set to
2
. [..]
I have no idea what you want to suggest here. With the change you mention I definitively do not get 2
as last output (unless primeCap
is set accordingly).
Note that I didn't do any benchmark, neither did I run a profiler on the code. This is just from reading your code. You should do profiling yourself if this is really time critical. (But then you should probably be using another method instead of the Sieve of Erasthothenes).
Reserve space for your primes
You know that you'll be looking for primes below primeCap = 1e10;
. The number of primes below x
is pi(x)
and can be approximated well with x/(log x - 1)
. So use that to reserve space beforehand in order to avoid (potentially costly) allocations.
What's the sign useful for?
Primes are positive. Why do you use (signed
) long int
when you could use unsigned
(long int
) numbers? Though keep in mind that some object against using unsigned integer datatypes as there are some pitfalls regarding "the usual arithmetic conversions".
Two times 2
The prime 2
will be twice in your vector. Change the initialization of the loop variable i
to i = 3
to solve that.
{
and }
for everyone
This is a very opinionated argument here, but consider using {}
even if there's only a single statement after an if
(or for
). We had some pretty popular bugs (partly) due to this "syntax shortcut" recently.
Notes:
- Don't try to optimize the
sqrt(i)
. - Some may object against the
break
. I don't think that it's an issue here, as the flow of control is pretty obvious.
There may also be an issue where the very last item in the vector is set to
2
. [..]
I have no idea what you want to suggest here. With the change you mention I definitively do not get 2
as last output (unless primeCap
is set accordingly).