Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of Python factorial summation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to write the code about the summation of Python factories. the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The method of summation of Python factorial

Topic description:

Get the integer n entered by the user, and output 1 "2" +. + n! The value of.

If the input value is 0, negative, non-numeric, or non-integer, the output prompt: input is incorrect, please enter a positive integer.

Method 1:

# factTest1def main (): a = input () sum = 0 if a.isdigit (): n = eval (a) if n > 0: fact = 1 for i in range (1, n = 1): fact * = I sum + = fact print (sum) else: print ("input is incorrect, please enter a positive integer") else: print ("enter a positive integer") main ()

Method 2: recursive thought

# factTest2import syssys.setrecursionlimit (5000) def getSum (I): sum = 0 if iComplement0: return 0 else: for x in range (1jigme 1): sum + = fact (x) return sumdef fact (m): if macaque 0: return 1 else: return m*fact (Mmur1) def main (): n = input () if n.isdigit (): a = eval (n) if a > 0: result = getSum (a) print (result) else: print ("input error, please enter a positive integer") else: print ("input error") Please enter a positive integer ") main ()

Summary of questions:

When you use the recursive method to get a factorial of 1024, an exception occurs: RecursionError: maximum recursion depth exceeded in comparison, which exceeds the maximum depth of recursion. Some netizens mentioned that the default maximum recursion depth in Python is 1000, but in the actual test, my computer has an exception at 997. I don't know what determines this. Therefore, in order to be able to calculate a factorial of 1024, you need to assign a larger value to the maximum recursive depth. The following methods can be used here:

Import syssys.setrecursionlimit (5000) # modified to 5000

Alternatively, you can view the maximum recursion depth:

Import syssys.getrecursionlimit () # output:1000

On the Python factorial summation of how to write the code to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report