How to multiply all numbers in a sequence (python)

As if I told the program n=10 , how would I make it return 10*9*8*7*6*5....1 ?

I thought it was a while loop, but I feel like I messed up somewhere because it doesn't sum all the numbers in the sequence.

My current code is as follows

 def product(n): i=n a=n-1 while a>0: return i * a b=i * a a=a-1 i=i-1 

Are there any better ways to do this without using recursion? Sorry for the incredibly beginner question, but I'm trying to teach myself how to code. You have to start somewhere!

Thanks!

+4
source share
8 answers

Since you are trying to learn the code, I will not give you a complete solution, but I will give you some tips:

  • You have a for loop that runs from 1 to n (using range(1, n+1) ) instead of your while -loop. This will generate the values ​​you want to propagate and repeat the correct number of times (which can sometimes be a bit complicated with while loops).

  • Have a variable called product to store the result of the multiplications every time through the loop.

  • Initialize product before entering for -loop. Inside, you will simply update the value of product .

  • After you finish the loop, you can use the return to return the value of product .

  • Finally, for testing, you can start with a small n , like 4, and print the values ​​that you calculate inside the loop to check how your code works.

There are more subtle and pythonic ways to do this, but it uses the code structure that you have already configured. And, of course, recursively, and also you mention.

Once you have mastered the basics, you will appreciate more idiomatic ways of writing this document, or you will invoke the appropriate functions that will do this for you.

+7
source

Well, here is another approach to Python.

 >>> import operator >>> numbers = range(1, 11) >>> numbers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> reduce(operator.mul, numbers) 3628800 
+3
source

Assuming you mean a factorial function, you can simply use math.factorial() :

 >>> import math >>> math.factorial(10) 3628800 
+2
source

You are trying to find the factorial of n, essentially. There are 2 methods for finding the factorial of a number.

  • Using the Loop Structure
  • Using Recursion (as you mentioned)

As a new programmer, you will be better off with a simple loop structure that runs from 1 to n and puts the multiplied value at each iteration into a variable. This variable is your answer. But also be aware that recursion will also work and make the code more elegant. Happy programming!

+2
source

This is called factorial. ten! equivalent to 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.

 def factorial(n): product = 1 while n > 0: product *= n n -= 1 return product 

By the way, in practice, just use math.factorial .

+1
source
  def factorial(n): if n <= 1: return 1 return n * factorial(n-1) 

I always think of factorial as a quintessential example in teaching recursion ...

+1
source

Another way to do this is to use scipy.product .

 >>> import scipy >>> scipy.product(xrange(1,11)) 3628800 
0
source

As a student, you should do this without using the built-in functions, this will help you learn programming, but just a tool, since learning a tool is much easier if you become a good programmer. There are two ways to do this, I implemented simpler versions.

Using recursion:

 def product(n): if n== 1: return 1 return n * product(n-1) 

Using a simple loop:

 def product(n): res = 1 while n>1: res = res * n n = n - 1 return res 
0
source

All Articles