python2 2! + 3! + 4! + 5! ... + 20! 구하는 알고리즘

#-*- coding:utf-8 -*-
sum = 0
i = 1
F =1

while(i<20):
    i +=1
    F = F*i
    sum +=F;


print sum



2! = 2 x 1  ===> i가 2일 때 F * i = 2 * 1
3! = 3 x 2 x 1 ===> i가 3으로 증가 F * i = (2*1) * 3 = 3 * 2 * 1
4! = 4 x 3 x 2 x 1 ===> i가 4로 증가 F * i = (3*2*1) * 4 = 4 * 3 * 2 * 1
5! = 5 x 4 x 3 x 2 x 1 ===> i가 5로 증가 F * i = (4*3*2*1) * 5 = 5 * 4 * 3 * 2 * 1

.
..
...
....

20! = 20 x 19 x 18 ... 3 x 2 x 1 ===> i가 20으로 증가 F * i = (19*18*..*2*1) * 20
                                                 = 20 * 19 * 18 *... * 3 * 2 * 1

댓글

이 블로그의 인기 게시물

Spring Boot Actuator readiness, liveness probes on k8s

About idempotent

About JVM Warm up

About G1 GC

About Kafka Basic

sneak peek jitpack

About ZGC

HackerRank Java Between Two Sets

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

I need to know a little JVM