Home>

### c - i want to eliminate an infinite loop in the following program

I want to make the following program.
(1) 2020 or lessEvenOn the other hand, if the natural number n is even, double it, and if it is odd, multiply it by 3 and add 1.
However, at the beginning of this operation, multiply by 5 and add 1. (Example 2 → 11 part)
Example when n = 2
2 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2
(2) Count only those that have returned to the original number as in the above example, and output the count number.
Since some numbers cannot be restored, devise a process in that case.
Example When n = 6
6 → 31 → 94 → 47 → 142 → 71 → 214 → 107 → 322 → 161 → 484 →
242 → 121 → 364 → 182 → 91 → 274 → 137 → 412 → 206 → ···

The problem i am having

The result will be output as 1.

I want to omit the above operation when n is an even number, but I don't know how to program.

Since there is a possibility that there is a natural number n in which the number loops infinitely with this operation,

I'd like to get out of that loop, is that okay?

If i know, please answer.

Corresponding source code
``````#include<stdio.h>
#include<stdlib.h>
#define INTMAX 2147483647
int h (int n) {
int count = 0;
int i;
for (i = n;i>0;i--) {
if (i% 2! = 0) break;
i == 5 * i + 1;
while (i! = n) {
if (i% 2 == 0) {
i = i/2;
} else {
if (i>(INTMAX-1)/3) exit (1);
n = 3 * n + 1;
}
}
count ++;
}
return count;
}
int main (void) {
int n = 2020, k, i;
k = h (n);
printf ("% d", k);
return 0;
}``````
• Answer # 1

(1) For an even number of 2020 or less, double if the natural number n is even, multiply by 3 if it is odd, and add 1.

Looking at the examples and codes presented, "double" instead of "double"Multiply by 1/2 (divide by 2)So, I will answer that the example and code are correct.

In the for loop`i`When is odd`break`(Escape from the loop)The process to be repeated is executed only once (when the value of i is 2020)..`continue`Use (skip the rest and repeat next).

In the process of "multiply by 5 first and add 1"`= =`Not (assignment)`==`(Comparison) is being doneFor,`i`The value of is not updated.

In the first place, control the repetition of the for loop`i`It is bad to change the value of.. The value that changes in the calculation is another variable (`j`Or) should be set.

When the repetition does not endThe final result is "4 2 1 4 2 1 (repeated below)"So, the continuation condition of the while loop is "The changing value is not equal to the first value,And the changing value is not equal to 1You can avoid an infinite loop by changing

Only when "the changing value is equal to the first value" after the while loop`count`Try to increase the value of.

Instead of defining INTMAX yourself, use INT_MAX contained in limits.h.

``````#include<stdio.h>
#include<stdlib.h>
#include<p>
int h (int n)
{
int count = 0;
for (int i = n;i>0;i--) {
if (i% 2! = 0) {
continue;continue;
}
int j = 5 * i + 1;
while (j! = i&&j! = 1) {
if (j% 2 == 0) {
j/= 2;
} else {
if (j>(INT_MAX --1)/3) {
exit (1);
}
j = 3 * j + 1;
}
}
if (j == i) {
count ++;
}
}
return count;
}
int main (void)
{
printf ("% d", h (2020));
return 0;
}``````