파이썬_실전 프로젝트

프로젝트 오일러 8번문제 - 13자리연속 곱

1000자리 숫자에서, 연속하는 13자리숫자들의 곱중에서 가장 큰값을 찾는 문제입니다.

 

Largest product in a series

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?


1. 데이터불러오고,
2. 루프문을 만들고 13자리 곱을 계산하고,
3. 조건문으로 큰값을 판별해줍니다.

 

데이터 불러오기 -1

 여러줄의 문자열은 작은따옴표 3개로 묶어주면 됩니다.

string='''73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450'''
print(string)
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
.....

 

그러나 실제 변수안에 저장되는 문자열은 줄바꿈 문자(\n)가 포함된 상태입니다.

string
'73167176531330624919225119674426574742355349194934\n96983520312774506326239578318016984801869478851843\n85861560789112949495459501737958331952853208805511\n12540698747158523863050715693290963295227443043557\n66896648950445244523161731856403098711121722383113\n

 

그래서 replace 명령으로 '\n'을 '' 공백으로 치환해주면 됩니다.

string.replace('\n','')
73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438

 

데이터 불러오기 -2
with open("q008_data.txt") as file_object:
    lines = file_object.readlines()

string=''
for line in lines:
    string=string+line.rstrip()
print(string)

731671765313306249192251...............................0823257530420752963450

또는 텍스트 파일에다 저장한후에 불러올수도 있습니다. (파일불러오기 토픽 참조)

 

연속 13자리 숫자 출력
for i in range(0,1000):
    print(string[i:i+13])
7316717653133
3167176531330
1671765313306
6717653133062
7176531330624
1765313306249
7653133062491
...
..
.
 

문자열 slice 기능을 이용해서 13자리씩 끊어서 출력합니다.

 

연속 13자리 숫자 곱
for i in range(0,1000):
    product =1
    for j in string[i:i+13]:
        product = product * int(j)
    print(string[i:i+13],product)
7316717653133 5000940
3167176531330 0
1671765313306 0
6717653133062 0
7176531330624 0
1765313306249 0
7653133062491 0
6531330624919 0
...
...

반복문을 사용해서 13자리를 곱합니다. 0 이 있는 경우는 결과가 0으로 계산됩니다.

 

13자리 숫자 곱(2)
from functools import reduce
for i in range(0,1000):
    product = reduce(lambda x,y:int(x)*int(y), string[i:i+13])
    print(string[i:i+13],product)
7316717653133 5000940
3167176531330 0
1671765313306 0
6717653133062 0
7176531330624 0
1765313306249 0
7653133062491 0
6531330624919 0
5313306249192 0
3133062491922 0
1330624919225 0
3306249192251 0
3062491922511 0
0624919225119 0
6249192251196 4199040
2491922511967 4898880
4919225119674 9797760
9192251196744 9797760
...
..

아니면 5번 문제에서 사용한 reduce 함수를 사용해도 됩니다.

 

최대값
maxValue =0
for i in range(0,1000):
    product =1
    for j in string[i:i+13]: 
        product = product * int(j)
    if product > maxValue:
        maxValue = product
    print(string[i:i+13],product, maxValue)
print(maxValue)
7316717653133 5000940 5000940
3167176531330 0 5000940
1671765313306 0 5000940
6717653133062 0 5000940
7176531330624 0 5000940
1765313306249 0 5000940
7653133062491 0 5000940
6531330624919 0 5000940
5313306249192 0 5000940
3133062491922 0 5000940
1330624919225 0 5000940
3306249192251 0 5000940
3062491922511 0 5000940
0624919225119 0 5000940
6249192251196 4199040 5000940
2491922511967 4898880 5000940
...
...
...
23514624000

 

댓글

댓글 본문
버전 관리
nomadlife
현재 버전
선택 버전
graphittie 자세히 보기