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 JVM Warm up

About idempotent

About Kafka Basic

About ZGC

sneak peek jitpack

Spring Boot Actuator readiness, liveness probes on k8s

About Websocket minimize data size and data transfer cost on cloud

About G1 GC

대학생 코딩 과제 대행 java, python, oracle 네 번째