Python Forum

Full Version: Escape indentation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I just posted an infinite sieve of Erathostenes https://python-forum.io/thread-36218.html
Hello Dean,

In the generator version, you modified, return prime should return a list? But there is no list of prime anymore. Even without the prime it will return the numbers in the output. I woud of expected an error saying name 'prime' is not defined but its not the case. "A generator is a function that returns an object (iterator) which we can iterate over (one value at a time)." Like you said it return number now and not a list anymore.

"You don't have to wait for it to compute every result" But you dont see it, its done behind the scene. You realize it only with large sample. From what i understand.

Speaking of generating primes. I saw this code using modulo that compile faster than Sieve of Eras. I cant really debate if modulo or Eras are better that one or the other but i suppose it depend how you write your code.

import math

def is_prime_v3(n):
    
    if n == 1:
     return False
     
    if n == 2:
     return True
     
    if n > 2 and n % 2 == 0:
     return False
     
    max_divisor = math.floor(math.sqrt(n))
    for d in range(3, 1 + max_divisor, 2):
        if n % d == 0:
            return False
    return True
    
for n in range(1,21):
    print(n, is_prime_v3(n))
As for Gribouillis code there are a few things in there i didnt have the time to learn yet. Like if __name__ == '__main__': that part is still a mystery to me. As for therse imports from heapq import heappush, heappop, heappushpop i will have to check on that. But thank you for the info.
Pages: 1 2