-1

Some integer n is given. If n is odd, carry out a task of 3 n+1 and n / 2 if n is even. Finish work when n reaches 1. The number created before n reaches 1 is called the cycle length. When two numbers, I and j, try to get the maximum cycle length for all numbers between I and j. Is there any way to compare the length of the cycle without writing global variables?

#include<stdio.h>
void cycle(int num) {
     int count = 1;
     while (1)
     {
            if (num == 1)
            break;

        if (num % 2 == 1) {
            num = 3 * num + 1;
            count++;
            printf("%d ", num);
        }
        else {
            num = num / 2;
            count++;
            printf("%d ", num);
        }
    }
    printf("\ncycle-length : %d\n", count);

}


void cycle_count(int num1,int num2) {
    int num;

    for (num = num1; num <= num2; num++) 
    {
        cycle(num);
    }

}
void main() 
{
    int num1, num2;
    scanf("%d %d", &num1,&num2);
    cycle_count(num1, num2);

    return;

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
baek
  • 1
  • 1

2 Answers2

1

Just declare the functions such a way that they would return the found values.

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )

Here is a demonstrative program

#include <stdio.h>

size_t cycle( unsigned int num ) 
{
    size_t count = 0;

    enum { EVEN = 0, ODD };     

    while ( num > 1 )
    {
        switch ( num % 2 )
        {
            case EVEN:
                num /= 2;
                break;

            case ODD:
                num = 3 * num + 1;
                break;
        }

        ++count;
    }

    return count;
}

size_t cycle_count( unsigned int num1, unsigned int num2 ) 
{
    size_t max_count = 0;

    if ( num2 < num1 )
    {
        unsigned int tmp = num1;
        num1 = num2;
        num2 = tmp;
    }

    for ( unsigned int num = num1; num <= num2; num++ ) 
    {
        size_t n = cycle( num );
        if ( max_count < n ) max_count = n;
    }

    return max_count;
}

int main(void) 
{
    unsigned int num1, num2;

    printf( "Enter two non-negative numbers: " );
    scanf( "%u %u", &num1, &num2 );

    printf( "The maximum cycle length is equal to %zu\n", cycle_count( num1, num2 ) );

    return 0;
}

Its output might look like

Enter two non-negative numbers: 1 10
The maximum cycle length is equal to 19

Pay attention to that in my functions I start to calculate the cycle length starting with 0. You can change the initial value if you want.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Curious use of `size_t` here as `size_t` is useful for array indexing and sizing. I'd expect `unsigned` or `unsigned long` or `unsigned long long`. – chux - Reinstate Monica Apr 06 '18 at 19:13
  • @chux Is not unsigned long usually size_t is it?:) – Vlad from Moscow Apr 07 '18 at 12:27
  • Perhaps `size_t` has the same range as `unsigned long` - or maybe not? I have worked on systems where `size_t` range is less than `unsigned long` as well as systems where `size_t, unsigned, unsigned long` all with the same range. Setting this aside, since [Collatz sequence](https://en.wikipedia.org/wiki/Collatz_conjecture) is not known to have a known upper bound, code could use `unsigned` (for speed) or `uintmax_t` for pedantic range. – chux - Reinstate Monica Apr 07 '18 at 12:50
0

You don't need global variable to do that. You can just use max_cycle variable to compare each iteration inside your user defined function. Here I have shown a simple python code.

def cycle(a):
    count=0
    while 1:
        count+=1
        if a==1:break
        elif a%2==0:a//=2
        else:a=3*a+1
    return count
def max_count(x,y):
    max_count=0
    for i in range(x,y+1):
        if max_count<cycle(i):max_count=cycle(i)
    return max_count
n,m=map(int,input().split())
print(max_count(n,m))
white-timer
  • 1
  • 1
  • 1