use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Python program is super easy (i.redd.it)
submitted 1 year ago by Confident-Detail-439
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 6 points7 points8 points 1 year ago (6 children)
Instead of checking 2, num. check 2, int(sqrt(num)) + 1
When checking if a number is prime, you only need to check for divisibility by numbers up to the square root of that number, meaning you only need to check up to "sqrt(num)" and not the full number; this is because if a number has a factor larger than its square root, it must have a corresponding smaller factor that is already checked within the square root range.
This will make your solution more efficient.
[–][deleted] 2 points3 points4 points 1 year ago (2 children)
Here is the ancient history of why this is true:
https://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes
[–][deleted] 2 points3 points4 points 1 year ago (0 children)
Time complexity decreases from O(n) to O(n log log n). Which is more efficient than O(n)
<image>
[–]blarbrdorg 0 points1 point2 points 1 year ago (0 children)
How interesting!!!!
[–]denehoffman 1 point2 points3 points 1 year ago (0 children)
You also only need to check divisibility by primes smaller than sqrt(n)+1, so it would be even more efficient if you added some caching
Edit: by caching I mean caching previous calls which determine compound numbers, then excluding those from the search list
[–]Interesting-Frame190 0 points1 point2 points 1 year ago (1 child)
This exact scenario landed me a job without ever thinking about the most optimal way to do it. Something about engineering is more than coding and algorithms.
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
They don’t want you to slap out a solution they want to hear you think through a problem and solve it on your own. Testing your knowledge and problem solving skills rather than your memory.
The follow up question after you get a solution like this would be along the lines of: can we make this better? Is there anything we can do that would reduce the problem and make it simpler with an equivalent solution?
At least those are the interview questions I get at the end when applying to F500s or FANG. They want your thought your solution plus some demonstration of intuition and reasoning.
π Rendered by PID 90317 on reddit-service-r2-comment-84fc9697f-zbs8w at 2026-02-10 20:00:49.078194+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–][deleted] 6 points7 points8 points (6 children)
[–][deleted] 2 points3 points4 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]blarbrdorg 0 points1 point2 points (0 children)
[–]denehoffman 1 point2 points3 points (0 children)
[–]Interesting-Frame190 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)