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!