'콜라츠 추측'에 해당되는 글 1건

  1. 2019.01.05 프로젝트 오일러 문제 14
2019. 1. 5. 06:08
728x90

Longest Collatz sequence

Problem 14

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.

어떠한 수 n에 대해 콜라츠 추측을 거치는 횟수는 동일하다. 그리고 수의 범위는 100만. 
문제 풀이의 목적은 어디까지나 횟수를 구하는 것이므로 각 수에 대한 콜타츠 추측 횟수를 기록하면 된다. 
예를들어 arrNum 배열이 있다고 할 때 

arrNum[2]는 2 -> 1로 1회.
arrNum[3]는 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2로 6회를 거치고 2는 체인이 1회므로 3의 체인은 7회.
arrNum[6]은 6 -> 3으로 1회를 거치고, 3은 체인이 7회이므로 6의 체인은 8회

이와 같은 방식으로 진행하면 100만까지의 모든 수에 대한 콜라츠 추측을 빠르게 구할 수 있다.
재귀함수를 이용한다면 arrNum[3]의 과정에서 3, 10, 5, 16, 8, 4의 체인 값을 바로 파악할 수 있어 연산 시간을 조금 더 줄일 수 있게 된다.



728x90

'공부 > 문제풀기' 카테고리의 다른 글

프로젝트 오일러 문제 16  (0) 2019.06.02
프로젝트 오일러 문제 15  (0) 2019.06.01
프로젝트 오일러 문제 13  (0) 2018.12.23
프로젝트 오일러 문제 12  (0) 2018.11.24
프로젝트 오일러 문제 11  (0) 2018.11.18
Posted by 아야카