Could i <= n / i be wrong in some cases because of division?
source link: http://codeforces.com/blog/entry/101729
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Let's say I want to check whether an integer is prime or not.
Implementation 11:
bool is_prime1(long long n) {
if (n < 2)
return false;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
Implementation 22:
bool is_prime2(long long n) {
if (n < 2)
return false;
for (long long i = 2; i <= n / i; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
Which implementation is better?
30 hours ago, # |
I mean, they look pretty similar to me, and they are both correct. (the second implementation won't be wrong because the only case it could have problems with is when n is a square, and the <= will take care of that)
30 hours ago, # |
I checked the following implementations on multiple test cases but I couldn't find any sort of TLEs in my code. Maybe it doesn't matter too much whether you use the first implementation or the second implementation. I think both of them will work fine. Obviously these are brute-force approaches, you can't use them in problems with very big constraints like 2e18 or so. You'll have to use techniques like Sieve of Eratosthenes to counter such problems.
-
It doesn't matter at all, they are basically the same code. And as for big constraints, how exactly would the Sieve of Eratosthenes help? remember, it's O(nlogn) for time and O(n) for space. (You can speed up the primality test by a tiny bit, by looping over primes smaller than the square root, but that only makes it O(sqrt(n)/log(n)) which is really redundant)
-
Which Technique is good to counter problems with the constraint marooonik mentioned?
-
Pretty sure there isn't an algorithm for primality testing faster than O(sqrt(n)). The Sieve of Eratosthenes can help with stuff like fast primality testing or prime factorization of many numbers in a reasonable range (like 1e6) (O(nlogn) for creation and O(1) for checking primality and O(sum of powers in prime factorization which is worst case logn) for prime factorization)
Sorry for the layered brackets ;)
-
-
29 hours ago, # |
FWIW *
operator is faster than /
operator, so first implementation has lower constant
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK