vendredi 30 septembre 2016

Stuck in Loop Collatz Conjecture Attempt in C

There is a logic flaw within my code that I can't seem to pass 2^31 − 1 as an input. Here is a fragment of my code.

#include <stdio.h>
int main() {
long input = 0;
long temp = 0;
int count = 0;
printf("Enter a positive integer ( or 0 to quit): ");
scanf("%ld", &input);
if(input == 0)
{
    printf("Quit.");
}
else
{
    temp = input;
    while (temp != 1)
    {
        if(temp %2 ==0)
        {
            temp = temp/2;
            count++;


        } else
        {
            temp = 3*temp + 1;
            count++;
        }

    }
return 0;
}

I have tried changing the size of my input to long => long long and it still get stuck within this area after Debugging it. Please provide some feedback Thanks!

Aucun commentaire:

Enregistrer un commentaire