Yahoo India Web Search

Search results

  1. Jan 6, 2022 · The easiest way is to use math.factorial (available in Python 2.6 and above): import math. math.factorial(1000) If you want/have to write it yourself, you can use an iterative approach: def factorial(n): fact = 1. for num in range(2, n + 1): fact *= num. return fact.

  2. Mar 14, 2013 · I have just started learning python. I came across lambda functions. On one of the problems, the author asked to write a one liner lambda function for factorial of a number. This is the solution that was given: num = 5. print (lambda b: (lambda a, b: a(a, b))(lambda a, b: b*a(a, b-1) if b > 0 else 1,b))(num) I cannot understand the weird syntax.

  3. Feb 23, 2018 · User warnabas suggests a way to do this using the itertools library, but it is possible (and simpler) to avoid this. def fac(n, t): '''This creates a generator for the sequence of factorial. numbers.'''. yield t. yield from fac(n+1, n*t) f = fac(1,1) We can then use this generator however we want. For example, the code.

  4. This will define a factorial function and a main function. The if block at the bottom will execute the main function, but only if the script is interpreted directly: ~> python3 test.py Please enter a number greater than or equal to 0: 4 4 factorial is 24

  5. May 2, 2013 · Here's my approach to factorials: def factorial(n): '''Returns factorial of n'''. r = 1. for i in range(1, n + 1): r *= i. return r. I think it's pretty straightforward, though I guess you could make something more efficient, because it takes ages for large numbers like 100000.

  6. Aug 10, 2018 · Similar to YulkyTulky's answer, it is also possible to write the one liner just using Boolean logic and without an if. factorial = lambda x : x and x * factorial(x - 1) or 1. The x and x * factorial(x - 1) part returns a value as long as x is not zero, but when x is zero, the function returns 1, ending the recursion. factorial(4) # 24.

  7. I've been stucked on this question for a really long time. I've managed to do a single recursive factorial. def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) Double factorial For an even integer n, the double factorial is the product of all even positive integers less than or equal to n.

  8. 1. write a function that calculates the factorial (def fact(n):...return res) for a number n and then use a list comprehension like: [fact(x) for x in range(10)] – Ma0. Dec 9, 2016 at 14:28. @Ev.Kounis Maybe the OP is asking us to write him a list comprehension that does not use any function ;) – user6999902.

  9. Oct 2, 2014 · 9. you can do this pretty easily: >>> import functools, operator. >>> functools.reduce(operator.mul, xrange(1, 6)) 120. Note that the first argument is a function (you're passing the result of a function call). the second argument is an iterable. Also note that written this way, no recursion is needed...

  10. Oct 22, 2022 · 95 10. There is no way to reduce the time complexity of the factorial function to below O (n), since n! has approximately n log n digits. That's assuming you're interested in wall time, and not the number of (big-int) arithmetic operations. – Paul Hankin. Oct 22, 2022 at 10:28.

  1. People also search for