프로젝트 오일러 도전기 #6 (with 파이썬 프로그래밍공부)

 문제


Sum square difference

 
The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is
3025 - 385 = 2640
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

1부터 100까지 "제곱의 합"과 "합의 제곱"의 차는?

1부터 10까지 자연수를 각각 제곱해 더하면 다음과 같습니다 (제곱의 합).

12 + 22 + ... + 102 = 385

1부터 10을 먼저 더한 다음에 그 결과를 제곱하면 다음과 같습니다 (합의 제곱).

(1 + 2 + ... + 10)2 = 552 = 3025

따라서 1부터 10까지 자연수에 대해 "합의 제곱"과 "제곱의 합" 의 차이는 

3025 - 385 = 2640 이 됩니다.

그러면 1부터 100까지 자연수에 대해 "합의 제곱"과 "제곱의 합"의 차이는 얼마입니까?

 

 

엑셀로도 구할수있는 쉬운문제

for 문을 이용해서 접근하겠다


추가로 오늘은 입력값을 받는 걸 이용하여 값을 받아서 해보겠다

파이썬에서 입력값을 받는 법은 input을 활용하면된다


1
2
3
4
>>> a=input()
1000
>>> type(a)
<class 'str'>
cs


여기서 주의할점은 input은 문자열로 받기때문에 int 로 변환해주는 과정이 필요하다


1
2
3
4
>>> a=int(input())
1000
>>> type(a)
<class 'int'>
cs


for문을 활용하여 계산한다


1
2
3
4
5
6
first = 0
second = 0
for i in range(a+1):
    first += i**2  ;값들의합
    second += i ;최종적으로 제곱
answer = second**2 - first
cs


**는 자승을 의미한다

댓글

이 블로그의 인기 게시물

프로젝트 오일러 도전기 #5 (with 파이썬 프로그래밍공부)

프로젝트 오일러 도전기 #7