How to factor n! or ! n?

Asked

Viewed 2,683 times

0

How to factor a number?

n! or ! n

Example:

Let’s take x = 5

 x = input('Digite um número inteiro: ')
 i = 1
 while i <= x:
      for i in range(x):
        i = i + 1
        a = []
        a.append(i)
        print(a)
      break

Since the result should be 120, however the interpreter prints this:

[1]
[2]
[3]
[4]
[5]

With append I was able to print 'i' from 1 to 'x'. In the example above, 'i = 1' and 'x = 5'. What I can’t do is multiply 'i' from 1 to 'x'!

  • 1

    In many ways. How you tried to do and what was the difficulty found?

  • 1

    Welcome, consider visiting the links reported above. If you are trying to become a programmer, it is not a good help to respond with a ready algorithm, right here on the site there are already a lot of answers. https://answall.com/search?q=fatorial+python but in this way you would be helped not to be a programmer. Try to write a code, represented the problem, step by step, and [Edit] your question by putting the difficulty you find.

  • Have you researched in these questions?

1 answer

0


You’re making it too complicated:

x = 5
total = 1
for n in range(x):
    total = total * (n + 1)


print(total)
  • Sure I messed up too much. I’ll study more the 'range'.

  • Range goes from 0 to x-1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.