Latest web development tutorials

Python incluye ejercicios 25

Python 100 Li Python 100 Li

Título: La demanda 1 + 2 + 3 + ... + 20 y !!!.

Análisis del programa: Este programa simplemente se multiplican acumulan cansarse.

Código fuente:

Método uno:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

n = 0
s = 0
t = 1
for n in range(1,21):
    t *= n
    s += t
print '1! + 2! + 3! + ... + 20! = %d' % s

Método dos:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

s = 0
l = range(1,21)
def op(x):
    r = 1
    for i in range(1,x + 1):
        r *= i
    return r
s = sum(map(op,l))
print '1! + 2! + 3! + ... + 20! = %d' % s

La salida del ejemplo anterior es:

1! + 2! + 3! + ... + 20! = 2561327494111820313

Python 100 Li Python 100 Li