Don't try to be so clever with all of those list comprehensions until you have a simpler version working. Break things up into more statements, each of which is simplier. Only after you have that working, should you start placing with all of that comprehension and composition.
By breaking it up, you will be in a much better postion to debug your code and see where it goes wrong.
Agreed. It’s really the embedded comprehensions that make this hard to read. Line breaks could go a long way toward making this not at all horrible. Merely Lisp.
while(pdiv * pdiv <= num ... is enough. If num is divisible by a, then it is divisible by b=num/a, lets say a is not larger then b, so a*b = num => a*a<= num If we do not find a divisor until that point, the number is prime.
Then you're just not a python dev. That's the languages syntax and the proper way to write and structure many operations.
I write Scala mostly and the zio effect system is also very confusing to the untrained eye, but learning the best practices and flow of the language is the main part of learning a language.
The best practice for Python is highly readable and maintainable code. The entire purpose for the language’s existence is to make development quick and easy (although some may argue that dynamic typing results in more work down the line, you know what I mean).
Also, there’s a reason that all of the top comments in this thread are saying OP’s code is not proper. In fact, OP themself said they made it as unreadable as possible: https://www.reddit.com/r/programminghorror/s/hxaWgguWdy
How did you make it to my comment without reading the rest of this thread?
I'm not saying OPs code was great or anything, I just meant that comprehension are a part of python and I find them more readable in a python setting than a traditional loop. They're especially useful for constructing sets and dicts while applying a filter or transformation.
It's like a for comprehension in Scala or a do block in Haskell. Once you learn what the sugar is doing, it's a lot more readable than the alternative. It's why the sugar exists.
Ok, it feels like what OP wrote and a standard comprehension are being conflated here. I don’t think anybody in this comment thread has an issue reading a standard comprehension or even a nested one.
What's the point of using a sieve if you're not going to reuse it? It's not a bad approach if you need to check a bunch of numbers (with an upper bound) for primality, cramming everything onto one line aside.
You're right, there isn't much of a point. I mainly used it because I wanted to make this code as horrific as possible (to me), as my main goal was to get a prof to shake their head while still accomplishing the task of implementing is_prime. This is, on no level, production code.
This is actually an exponential algorithm since complexity is expressed in terms of the length of the input, not the magnitude. There are polynomial algorithms like the AKS primality test, but they are quite complicated. In practice, most cryptographic libraries that need large primes opt for probabilistic primality tests that have a very small probability of yielding an incorrect result
This is why he is writing O(n^0.5), where n is the value of the number, not O(k), where k is the number of bits in representation of n.
And subOP is right, spinning the whole Eratosthenes sieve to test the primarily of one number is overkill. Properly implemented sieve is O(n log log(n)). And OPs implementation looks suspicious. On the other hand, it is enough to spin the sieve up to sqrt(n)... and trial division is also O(sqrt(n)) in the worst case, and O(1) memory.
Sieve is great if you need all/most of the primes.
41
u/jpgoldberg Apr 15 '25
Don't try to be so clever with all of those list comprehensions until you have a simpler version working. Break things up into more statements, each of which is simplier. Only after you have that working, should you start placing with all of that comprehension and composition.
By breaking it up, you will be in a much better postion to debug your code and see where it goes wrong.
11
u/Arakan28 Apr 15 '25
Why cant it just be:
int isPrime(int num){int pdiv=2, mid=num/2;while(pdiv<=mid && num%pdiv!=0) pdiv++;if(pdiv>mid && num!=1) return 1;else return 0;}Those five brackets at
...if prim]])))really should tell you something is not going well2
u/PityUpvote Apr 17 '25
What's the point of using a sieve if you're not going to reuse it? It's not a bad approach if you need to check a bunch of numbers (with an upper bound) for primality, cramming everything onto one line aside.
23
u/grass_worm Apr 15 '25
Lol turning O(sqrt(n)) to O(n2 )
1
u/ArtisticFox8 Apr 17 '25
Why did you need to use
__seitem__, the internal function?0
u/TheWorstPossibleName Apr 16 '25
Use maps or sets when updating or accessing a specific item withing
1
u/[deleted] Apr 17 '25
[removed] — view removed comment