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

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

About ZGC

Spring Boot Actuator readiness, liveness probes on k8s

About G1 GC

sneak peek jitpack

About idempotent

C 언어 구조체의 포인터 멤버 변수

Synology NAS에 MariaDB 10에 Mysql workbench로 원격접속하기

About Websocket minimize data size and data transfer cost on cloud