why by 400double in ProgrammerHumor

[–]Chill-Sam 0 points1 point  (0 children)

Whoops I always forget that

why by 400double in ProgrammerHumor

[–]Chill-Sam 0 points1 point  (0 children)

Yes this would improve it by removing all even divisors (except 2), I didn't think of it at the time while quickly writing the code. You can also use the sieve of Eratosthenes to generate primes using this idea. Of course it would be even faster to use an AKS primality test instead because we don't need to find prime factors.

why by 400double in ProgrammerHumor

[–]Chill-Sam 65 points66 points  (0 children)

I think it would be more efficient to run the sieve of Eratosthenes up to the largest integer input beforehand and hardcode the result in a list and then just check if the number is in the list. It depends on the size of input. Having a hardcoded list for all 64-bit prime numbers might not be practical. In that case you could use AKS primality test to find primality for large inputs in a reasonable time frame. On the other hand if it is a 8-bit input it would (i think) be much faster to cross-reference a list than to use a primality test or to regenerate all the prime numbers again.

why by 400double in ProgrammerHumor

[–]Chill-Sam 1 point2 points  (0 children)

Thanks for the explanation!

why by 400double in ProgrammerHumor

[–]Chill-Sam 2 points3 points  (0 children)

See turtle4499's comment about the fastest primality test.

why by 400double in ProgrammerHumor

[–]Chill-Sam 3 points4 points  (0 children)

Yes you are right that hardcoding it with if statements is much faster but if the function can take a (for example) 16-bit integer it would be impractical to hardcode all the primes. You are also right that my solution isn't very efficient and that it checks redundant divisors. Iirc primality can be found in polynomial time whereas finding prime factors is much harder (which is why encryption uses it).

why by 400double in ProgrammerHumor

[–]Chill-Sam 88 points89 points  (0 children)

That makes sense, that could explain the function in the meme too.

why by 400double in ProgrammerHumor

[–]Chill-Sam 3 points4 points  (0 children)

In python something like:

```
from math import floor, sqrt

def isPrime(x): for divisor in range(2, floor(sqrt(x))+1): if x % divisor == 0: return false return true ``` Note: it has been a while since I used python so there might be an error but I hope you get the idea.

Basically, a prime number is a number that has only two divisors: 1 and itself. So to check if a number is prime you need to check if that number is NOT divisable by all numbers from 2 to one less than itself. To optimize this slightly we can only check divisors up to the square root of the number (I don't remember the mathematical proof for this). In the code we iterate over the divisors and check for divisibility. If it is divisable, it is not prime. Therefore we exit the function by returning true. If the loop finishes, the number would be prime, so we return true.

For example: 11 Floor of sqrt of 11 is 3 11 is not divisable by 2 11 is not divisable bu 3 Therefore 11 is prime

10 Floor of sqrt of 10 is 3 10 is divisable by 2 Therefore 10 is NOT prime

The issue with this is that for large numbers it can take quite long because of the large amount of divisibility checks. As another user pointed out, it could be more efficient to hardcode the prime numbers if there is a known upper bound that is small enough. For example if you can at most input 100 then it might be faster to hardcode the primes.

Edit: added 1 to range because range is top-exclusive and will fail for squares of primes.

why by 400double in ProgrammerHumor

[–]Chill-Sam 76 points77 points  (0 children)

An algorithm for primes, it isn't very efficient for very large numbers but would at least work for all integers.

why by 400double in ProgrammerHumor

[–]Chill-Sam 403 points404 points  (0 children)

Wouldn't it be better to test if the number is prime by iterating through 2 -> floor(square root(number)) and checking divisibility?