Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Escape indentation
#10
In the Sieve of Eratosthenes example the prime list and sieve set are generated fresh each time the function is called. The user wants a list of prime numbers up to and including "n". The algorithm depends on generating a set of non-prime numbers. But even if you don't want a list there are lots of things you can do with a list. Sometimes creating a list may be the most efficient way to accomplish your goal.

Looking at your code it is unclear what you want to accomplish. If you just want to print the prime number in the range 0..20 the Sieve of Eratosthenes may be the most efficient way to do that.
def primes(n): # Sieve of Eratosthenes
    prime, sieve = [], set()
    for q in range(2, n+1):
        if q not in sieve:
            prime.append(q)
            sieve.update(range(q*q, n+1, q))
    return prime
 
for prime in primes(20): 
    print(prime)  # Because you want to print them one per line.
The code is very efficient because it doesn't do any division or much math at all, far more efficient than using the modulo operator. This would be seen if n=1000000 instead of 20.

If you really hate the idea of having a list, even as a temporary holder for your values, you could rewrite this as a generator.
def primes(n): # Sieve of Eratosthenes
    sieve = set()
    for q in range(2, n+1):
        if q not in sieve:
            yield q
            sieve.update(range(q*q, n+1, q))
    return prime

for prime in primes(20):
    print(prime)
Now primes() returns a number, not a list of numbers. It doesn't make much sense writing something like Sieve of Eratosthenes as a generator. The algorithm needs an ending point and making the ending point arbitrarily large makes the algorithm run very slow. But for many kinds of calculations or operations a generator is the way to go. You don't have to wait for it to compute every result, and it doesn't use a lot of storage to save results.
Reply


Messages In This Thread
Escape indentation - by Frankduc - Jan-28-2022, 02:25 PM
RE: Escape indentation - by BashBedlam - Jan-28-2022, 02:53 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 03:05 PM
RE: Escape indentation - by snippsat - Jan-28-2022, 03:13 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 03:36 PM
RE: Escape indentation - by Gribouillis - Jan-28-2022, 03:41 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 04:07 PM
RE: Escape indentation - by deanhystad - Jan-28-2022, 04:24 PM
RE: Escape indentation - by Frankduc - Jan-28-2022, 04:50 PM
RE: Escape indentation - by deanhystad - Jan-28-2022, 07:24 PM
RE: Escape indentation - by Gribouillis - Jan-28-2022, 09:59 PM
RE: Escape indentation - by Frankduc - Jan-31-2022, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 953 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  add Escape charcters in string GrahamL 3 1,224 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,892 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,370 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,076 Dec-23-2020, 06:47 PM
Last Post: Mark17
  help for escape sequences NewPi 1 2,079 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  escape single quote deep_logic 1 1,841 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  The use of escape char \ hishamzero1 2 2,425 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  Escape sequences display in python Uchikago 1 2,483 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Python 3 escape codes oldDog 1 2,632 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