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 find the factorial of N by python

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to find the factorial of N by python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Find the factorial of N

This question requires that a program be written to calculate the factorial of N.

Input format:

Enter to give a positive integer N on one line.

Output format:

Output the factorial value F in the format "product = F" on one line, notice that there is a space on the left and right of the equal sign. The topic ensures that the calculation results do not exceed the double precision range.

Enter a sample:

five

Sample output:

Product = 120

X = int (input ()) a = 1for i in range (1, x = 1): a = a*iprint ("product =% d"% float (a)) three solutions to factorial

Problem description:

Enter a positive integer n and output n! The value of.

Among them, nasty 123 *... * n.

Algorithm description:

N! It may be very large, but the range of integers that the computer can represent is limited, so it is necessary to use high-precision calculation methods. An array An is used to represent a large integer a [0] represents the bits of a, A [1] represents ten digits of a, and so on.

Multiply a by an integer k to multiply every element of array A by k, and pay attention to handling the corresponding carry.

First, set a to 1, then multiply by 2, multiply by 3, and when multiplied by n, you get n! The value of.

Input format:

The input contains a positive integer n Carry = 0 for j in range (length): temp = ns [j] * I + carry carry = int (temp/10) ns [j] = temp% 10 while carry > 0: ns [length] + = carry length+=1 carry = int (carry/10) while length > 0: length-= 1 print End='')

Next, I would like to talk about the following ideas:

First define a ns array to store n! Add 10000 zeros to the ns using the for loop to facilitate subsequent operations on the array directly according to the index.

Then define length as the "length of the array" (0 with real values rather than automatically added), that is, n! The number of digits of the result of the

After that, we must also use the for loop for cumulative multiplication, but unlike the direct multiplication of solution 1, here the multiplier (that is, I) is multiplied by the number on each bit respectively. If the result is greater than or equal to 10, the carry > 0 means that the value of one digit forward is carry. If carry > 0 after the end of the j cycle, it means that the "length" of the current ns needs to be carried forward by one digit, so length+1 is the number of digits + 1, where carry plays the role of judging whether to carry or not. Length represents the number of digits of the result. It may be a bit abstract to say so, but let's explain the above idea more intuitively by breaking down the running process.

For example, we now need to require 5 cycles, which is divided into five steps, that is, I cycle 5 times:

① iTunes 1 ns [0] = length = 1, carry = 0 ∴ j in range (1)

⑴ jung0

Temp = ns [j] * I + carry= ns [0] * I + carry= 1 is the number of digits multiplied by I and the result of the carry value of multiplying the number of 1 digits by I is carry= int (temp/10) = 1 ns 10 = carry carry=0 so there is no carry ns [j] = temp% 10, that is, ns [0] = 1% 10 = 1 # take only individual digits as the value of bit j, ns item2 ns [0] = 1, length = 1 Carry = 0 ∴ j in range (1)

⑴ jung0

Temp = ns [j] * I + carry= ns [0] * I + carry= 1 "2: 0,0,0,2 # temp is the result of multiplying the j digit by I and adding the carry value of Jmuri 1 digit multiplied by I, carry= int (temp/10) = 2 / 10 = 0 # carry=0, so there is no carry ns [j] = temp% 10, that is, ns [0] = 2% 10 = 2 # only take the individual digit as the value of the j bit # so it is already 2! That is, 2 ③ ionization 3 ns [0] = 2, length = 1, carry = 0 ∴ j in range (1)

⑴ jung0

Temp = ns [j] * I + carry= ns [0] 6 # temp is the result of multiplying the j digit by I and adding the carry value of Jmuri 1 digit multiplied by I carry= int (temp/10) = 6 / 10 = 0 # carry=0 so there is no carry ns [j] = temp% 10, that is, ns [0] = 6% 10 = 6 # only take the individual digit value as the j bit value # so it is already 3! That is, 6 ④ iTunes 4 ns [0] = 6, length = 1, carry = 0 ∴ j in range (1)

⑴ jung0

Temp = ns [j] * I + carry= ns [0] * I + carry= 6 "4" 0 "24 # temp is the result of multiplying the j digit by I and adding the carry value of the 1 digit by I carry= int (temp/10) = 24 / 10 = 2 # carry=2 > 0, so you need to move forward 2 ns [j] = temp% 10, that is, ns [0] = 24% 10 = 4 # take only the single digit value as the j bit value

J loop ends, carry > 0 executes while loop

While carry > 0: ns [length] + = carry, that is, ns [1] + = 2% 10 = 2 # carry = 2, so forward 2 length+=1 is length = 1 "1" 2 # digits plus one carry = int (carry/10) = 2 / 10 = 0 # carry = 20, so you need to move forward 2 ns [j] = temp% 10, that is, ns [0] = 20% 10 = 0 # only take the single digit value as the j bit value.

⑵ jung1

Temp = ns [j] * I + carry= ns [1] * I + carry= 2 "5" 2 "12 # temp is the result of multiplying the number j by I and adding the value of carry by the multiplication of 1 digit and I, carry= int (temp/10) = 12 / 10 = 1 # carry=1 > 0, so you need to move forward 1 ns [j] = temp% 10, that is, ns [1] = 12% 10 = 2 # take only single digit as the value of j

J loop ends, carry > 0 executes while loop

While carry > 0: ns [length] + = carry is ns [2] + = 1% 10 = 1 # carry = 1, so forward 2 length+=1 is length = 2 + 1 = 3 # digits plus one carry = int (carry/10) = 1 / 10 = 0 # carry = 1

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