Jumat, 23 Desember 2016

UVA 100 - The 3n +1 problem solution

Problem Statement: The 3n +1 problem

Explanation

Some critical point from the problem statement:
  • Determine the maximum cycle length over all numbers between and including both i and j. 
  • No operation overflows a 32-bit integer.
  • The integers i and j must appear in the output in the same order in which they appeared in the input
There's a possibility that j > i. So you must check it before processing further.

Optimization


Everytime you do the 3n + 1 operation, the result always an even integer. So everytime you do 3n + 1 operation you have to do n/2 operation as well. This two operation can be combined in one if statement then increment the cycle length by 2.

Source Code


Tidak ada komentar:

Posting Komentar