The following iterative sequence is defined for the set of positive integers:
N N/2 (NIs even)
N3N+ 1 (NIs 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.
Collatz Problem. I have solved this problem by using a brute force method and have not found any good rules yet.
WriteProgramSummary:
※Initialization is recommended for variable definition, otherwise unexpected results may occur. It is like changing the N and Max in the program. The C language does not initialize, and the system does not automatically assign 0 to the variable. Pay extra attention to this.
Question 1 of the Euler Project # Include <stdio. h> # include <stdlib. h> Int Lencollatz ( Unsigned Long N ); // Enter a number for Collatz iteration and return the chain length. Int Main ( Int Argc, Char * Argv []) { Unsigned Long N = 13, max = 1; Int Lenmax = 1; While (N <1, 1000000 ){ If (Lenmax <lencollatz (N) {max = N; lenmax = lencollatz (n) ;}n ++ ;} Printf (" Max numer is % lu, max length is % d ", Max, lenmax ); System (" Pause "); Return 0 ;} Int Lencollatz ( Unsigned Long N ){Int Len = 1; While (N! = 1 ){ If (N % 2 = 0) N/= 2; Else N = 3 * n + 1; Len ++ ;} Return Len ;}