Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape indentation
#11
I just posted an infinite sieve of Erathostenes https://python-forum.io/thread-36218.html
Reply
#12
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 935 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  add Escape charcters in string GrahamL 3 1,202 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,859 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,337 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,048 Dec-23-2020, 06:47 PM
Last Post: Mark17
  help for escape sequences NewPi 1 2,058 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  escape single quote deep_logic 1 1,825 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  The use of escape char \ hishamzero1 2 2,396 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  Escape sequences display in python Uchikago 1 2,455 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Python 3 escape codes oldDog 1 2,614 Sep-15-2018, 10:12 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020