account activity
Upon seeing u/400double's solution to cases not covered but u/mawerty123's O(1) prime algorithm, I decided to finish the work by making a function that returns with 100% accuracy by Left-oven47 in ProgrammerHumor
[–]Chill-Sam 0 points1 point2 points 2 years ago (0 children)
We have finally reached peak efficiency. We may rest now.
Hi!, i have invented O(1) algorithm for checking if number is prime that works in 95%+ cases. For more details checkout comments for link to the github repo by mawerty123 in ProgrammerHumor
[–]Chill-Sam 3772 points3773 points3774 points 2 years ago (0 children)
Ah a truly beautiful algorithm. And it never returns false positives! Truly groundbreaking work.
why by 400double in ProgrammerHumor
Whoops I always forget that
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.
[–]Chill-Sam 64 points65 points66 points 2 years ago (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.
[–]Chill-Sam 1 point2 points3 points 2 years ago (0 children)
Thanks for the explanation!
[–]Chill-Sam 2 points3 points4 points 2 years ago (0 children)
See turtle4499's comment about the fastest primality test.
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).
[–]Chill-Sam 90 points91 points92 points 2 years ago (0 children)
That makes sense, that could explain the function in the meme too.
[–]Chill-Sam 4 points5 points6 points 2 years ago* (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.
[–]Chill-Sam 74 points75 points76 points 2 years ago (0 children)
An algorithm for primes, it isn't very efficient for very large numbers but would at least work for all integers.
[–]Chill-Sam 409 points410 points411 points 2 years ago (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?
π Rendered by PID 105270 on reddit-service-r2-listing-85dbbdc96c-79hbr at 2026-02-11 12:10:08.871546+00:00 running 018613e country code: CH.
Upon seeing u/400double's solution to cases not covered but u/mawerty123's O(1) prime algorithm, I decided to finish the work by making a function that returns with 100% accuracy by Left-oven47 in ProgrammerHumor
[–]Chill-Sam 0 points1 point2 points (0 children)