# [Project Euler] add Euler problem 14

Source: Internet
Author: User

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 ;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.