File tree Expand file tree Collapse file tree
project_euler/problem_014 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #Problem 14: https://projecteuler.net/problem=14
2+
3+ #Problem Statement:
4+ #The following iterative sequence is defined for the set of positive integers:
5+ #
6+ # n → n/2 (n is even)
7+ # n → 3n + 1 (n is odd)
8+ #
9+ #Using the rule above and starting with 13, we generate the following sequence:
10+ #
11+ # 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
12+ #
13+ #It can be seen that this sequence (starting at 13 and finishing at 1) contains
14+ #10 terms. Although it has not been proved yet (Collatz Problem), it is thought
15+ #that all starting numbers finish at 1.
16+
17+ #Which starting number, under one million, produces the longest chain?
18+
19+ def solution ( )
20+ index_best_result = 0
21+ for num in 2 ..1000000
22+ index_candidate = 0
23+ n = num
24+ while n > 1
25+ if n %2 == 0
26+ n = n / 2
27+ else
28+ n = ( 3 *n ) + 1
29+ end
30+ index_candidate +=1
31+ end
32+ if index_best_result < index_candidate
33+ index_best_result = index_candidate
34+ value = num
35+ end
36+ end
37+ result = value
38+ end
39+
40+ answer = solution ( )
41+ p answer
You can’t perform that action at this time.
0 commit comments