I am trying to create an algorithm in Python that takes an input and prints the multiple of all the numbers less than it. example: input ; 3 output ; 1*2*3 = 6.
example2: input ; 15 output ; 1*2*3*4*5*6*7*8*9*10*11*12*13*14*15 = 1,307,674,368,000.
I am trying to create an algorithm in Python that takes an input and prints the multiple of all the numbers less than it. example: input ; 3 output ; 1*2*3 = 6.
example2: input ; 15 output ; 1*2*3*4*5*6*7*8*9*10*11*12*13*14*15 = 1,307,674,368,000.
Welcome to Stack Overflow. I think this problem is not so difficult, and you should try it yourself first.
Here is a simple example:
def main(arg):
ans = 1
for x in range(1, arg+1):
ans *= x
if x == arg:
print(x, end=' ') # skip the last one
else:
print(x, end='*')
print(f'= {ans}', end='')
if __name__ == '__main__':
try:
arg = int(input())
main(arg)
except:
pass
input: 3 output: 1*2*3 = 6
This is equal to calculating factorial(n). I will provide a simple recursive implementation.
def recursive_factorial( n ):
if n <= 1:
return 1
return n * recursive_factorial(n - 1)
if __name__ == '__main__':
x = int(input())
print( recursive_factorial(x) )
It is a really common programming exercise and you can easily find more information. A good place to start is ( https://en.wikipedia.org/wiki/Factorial )