Python code's factorial summation method

Source: Internet
Author: User
Requirement: factorial: It is also a term in mathematics. factorial refers to multiplying 1 by 2 by 3 by 4 to the required number. when expressing a factorial, use "!". . For example, h factorial indicates h !; Factorial is generally difficult to calculate because the product is large. Question: 1 + 2! + 3! +... + 20! Environment: python3 editor: pycharm requirements:

Factorial: It is also a term in mathematics. a factorial refers to multiplying 1 by 2 by 3 by 4 to the required number. when expressing a factorial, use "!". . For example, h factorial indicates h !; Factorial is generally difficult to calculate because the product is large.

Question: 1 + 2! + 3! +... + 20! And

Implementation environment: python3

Editor: pycharm

Analysis: 1. factorial calculation is a troublesome part. it is a good solution to implement recursive functions. First, define a recursive function to implement the factorial function.

Def recursion (n): 'defines recursive functions to implement the factorial function. 'if n = 1: return 1 else: return n * recursion (n-1)

2. the summation method can be used to directly sum up, or define a list to append the obtained factorial result of for traversal to the list, and then use the sum () function to sum the result.

Sum_0 = 0 print ("for loop directly calling recursive function summation ". center (80, "*") # display effect obvious for I in range (): sum_0 + = recursion (I) print (sum_0) list summation scheme: list = [] # define an empty list and append the factorial value generated by calling the recursive function to the list print ("write 1-20 factorial to the list and use the sum function to sum ". center (80, "*") # display effect obvious for I in range (): list. append (recursion (I) # append the factorial value generated by calling recursive functions to the list print (sum (list) # list summation

You can implement the same number of lines of code.

Usage knowledge: Recursive functions for loop range () functions.

Complete source code and results:

#/Usr/bin/env python # _ * _ coding: UTF-8 _ * _ def recursion (n): 'define a recursive function to implement the factorial function 'if n = 1: return 1 else: return n * recursion (n-1) list = [] # define an empty list, append the factorial value generated by calling the recursive function to the list print ("write 1-20 factorial to the list and use the sum function to sum ". center (80, "*") # display effect obvious for I in range (): list. append (recursion (I) # append the factorial value generated by calling the recursive function to the list print (sum (list )) # List summation sum_0 = 0 print ("for loop directly calling recursive function summation ". center (80, "*") # display results: * **************************** write 1-20 factorial to the list, use the sum function to sum **************************** 2561327494111820313 ****** * ************************ for a loop to directly call the sum of recursive functions ******* *************************** 2561327494111820313

Both of them can achieve basic functions, but the calculation of larger data volume is not tested.

The above is a detailed description of the factorial summation method of python code. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.