Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uva 100
- #include<stdio.h>
- int main()
- {
- /* Variables used in the program */
- int counter, i, j, m, maxCounter;
- /* Read until the end of the file */
- while(scanf("%d%d",&i,&j) == 2)
- {
- /* Reset the max counter */
- maxCounter = 0;
- /* Continue processing until i reaches j */
- while(i<=j) {
- /* Reset the counter to 1*/
- counter = 1;
- /* Assign the value of i to the temporary variable m */
- m = i;
- /* Perform the algorithm as described in the problem statement */
- while( m ! = 1) {
- /* If m is odd, then m = 3 x m + 1 */
- if(m%2 == 1) {
- m = 3 * m + 1;
- /* Else if m is even, then m = m / 2 */
- } else {
- m = m/2;
- }
- /* Increment the counter */
- counter++;
- }
- /* If the counter is greater than the max so far, set a new max */
- if(counter > maxCounter) {
- maxCounter = counter;
- }
- maxcounter++;
- }
- printf("%d",maxcounter);
- }
- return 0;
- }
- //10 is even....then the next one would be 5 but its odd then 5*3+1=16
- //10,5,16,8,4,2,1-7 cycle
- //9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1- 20 cycles
- // 20>7
- //this will continue to 1
- //maximum would be printed
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement