Python Forum
Comparing recursion and loops using two scripts (basic factorial)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing recursion and loops using two scripts (basic factorial)
#4
There is a special place reserved in hell for people who write this kind of code:
Quote:def factorial(n):
    return [(f := f * i) if i > 0 else (f := 1) for i in range(n+1)]

## This looks so much nicer
def factorial(n):
    f = 1
    for i in range(1, n+1):
        f = f * i
        yield f
Reply


Messages In This Thread
RE: Comparing recursion and loops using two scripts (basic factorial) - by deanhystad - Oct-11-2020, 06:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using recursion instead of for loops / list comprehension Drone4four 4 3,192 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Python factorial code timebrahimy 4 72,310 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial, repeating Aldiyar 4 2,845 Sep-01-2020, 05:22 PM
Last Post: DPaul
  factorial using recursive function spalisetty06 12 4,139 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 1,913 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,334 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Want to print each iteration of factorial program sbabu 10 4,629 Jan-09-2020, 07:25 AM
Last Post: perfringo
  Factorial sketch(python 3) KingArthur526 1 1,997 Sep-25-2019, 01:51 PM
Last Post: ichabod801
  Factorial leodavinci1990 8 4,498 Jul-19-2019, 10:59 PM
Last Post: leodavinci1990
  nested for loops to recursion ashkea26 1 3,092 Nov-02-2018, 09:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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