Created
March 10, 2016 11:24
-
-
Save 78526Nasir/b2ae9120803eaa958149 to your computer and use it in GitHub Desktop.
Solution of the 3n+1 problem !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The 3n+1 problem // | |
#include<stdio.h> | |
long long count(long long n){ | |
long long c; | |
c=1; | |
while(n>1){ | |
if(n%2!=0){ | |
n=(3*n)+1; | |
++c; | |
} | |
else{ | |
n=n/2; | |
++c; | |
} | |
} | |
return c; | |
} | |
long long cycle(long long a,long long b){ | |
long long i,max=0,take,temp; | |
if(a>b){ | |
temp=a; | |
a=b; | |
b=temp; | |
} | |
i=a; | |
while(i<=b){ | |
take=count(i); | |
if(max<take) | |
max=take; | |
i++; | |
} | |
return max; | |
} | |
int main() | |
{ | |
long long n1,n2,takeMax; | |
while(scanf("%lld%lld",&n1,&n2)!=EOF){ | |
takeMax=cycle(n1,n2); | |
printf("%lld %lld %lld\n",n1,n2,takeMax); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment