[문제]
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
[풀이]
intNumber = 1000000
intCount = 0
for i in range(1,intNumber):
intTemp = 1
intDiv = i
while intDiv!=1:
if intDiv%2==0:
intDiv = int(intDiv/2)
intTemp+=1
else:
intDiv = int(3*intDiv+1)
intTemp+=1
if intTemp>intCount:
intCount = intTemp
print(i)
'''
기존의 체인보다 더 긴 체인이 발생하면 그 값을 print하도록 했다.
'''
'파이썬' 카테고리의 다른 글
[Project Euler]16. Power digit sum (0) | 2021.06.10 |
---|---|
[Project Euler]15. Lattice paths (0) | 2021.06.09 |
[Project Euler]13. Large sum (0) | 2021.06.07 |
[Project Euler]12. Highly divisible triangular number (0) | 2021.06.06 |
[Project Euler]11. Largest product in a grid (0) | 2021.06.02 |