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. Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat. Learn more Explore Teams

  3. print("factorial = 1") for i in range(1,num + 1): factorial = factorial*i. print(num, factorial) But I want to do this with a while loop (no function). A for loop can be rewritten as a while loop. Set up your initial value for i before the loop; use the while condition to detect when i reaches its limit; increment i inside the loop.

  4. Oct 15, 2020 · I'm new to Python. I only get by by self-studying please be kind. So I would like to ask, without using math factorial(), is how to answer this example: def factorial(n): result = 1 for i i...

  5. 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.

  6. 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.

  7. Jul 2, 2021 · 2. factorial and n values are not reset to their default values in your loop. They store the values of previous iteration and use them in the current iteration. You must include the factorial and n inside the while loop. while True: n = 1. factorial = 1. num=int(input("Enter number: ")) if num<=0:

  8. Sep 4, 2020 · def factorial (n): if n < 0: return "" #This is as per your specification fact = 1 for number in range(1,n+1): fact = fact * number return fact This function will give you the factorial of any number 'n'. But you need the factorial of all elements in the list. So you can simply map the function to your list of numbers.

  9. 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.

  10. Oct 24, 2013 · 0. I don't know why you don't want to use a loop, but here's a couple ideas: Unroll the loop yourself. System.out.println(factorial(1)); System.out.println(factorial(2)); // ... System.out.println(factorial(11)); Make another recursive method and call it from main. public static void factorials(int n)

  1. Searches related to factorial program in python using for loop

    factorial program in python
    python online compiler
  1. People also search for