## 371 - Ackermann Functions

Moderator: Board moderators

mukit
New poster
Posts: 48
Joined: Wed Nov 21, 2007 10:09 am
Contact:

### Re: 371"Ackermann Functions "

@etameem:
this problem is straight-forward. just find and choose the best value.
The largest value in the sequence will not be larger than can be accomodated in a 32-bit Pascal LongInt or C long
use unsigned long or long long.
and ...

Code: Select all

``````max=0;
for (k=i;k<=j;k++) {
count=0;
p=k;
while (1)
{
if (p%2==0)
p=p/2;
else
p=3*p+1;
count=count+1;
if(p==1)break;
}
if(max<count) {
max=count;
gen=k;
}
count=0;
}
``````
1. Don't use count and max as variable name. sometimes it creates problem if defined as global.
2. Think simple . It will take much time to understand your code.
3. It's better if you can find your bug yourself.
4. Always search the board first. Check with board input. If further confusion exists, http://uvatoolkit.com/problemssolve.php
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

### Sopno dekha uchit....

Sometimes others problem may take u to the next level.... Jan vai Apnake add disilamm FB te But receive koren nai

Amio ekhon Sopno dekhi....
I'll keep holding on...Until the walls come tumbling down...And freedom is all around .....
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

### 371 why TE? any body help me???help

#include<stdio.h>
#include<stdlib.h>
//using namespace std;

//typedef __int64 ll;

int main()
{
long long s,e,n,c,t,p,m,temp,i;
while(scanf("%I64d %I64d",&s,&e)==2)
{
if(s==0&&e==0)
break;
t=0;
if(s>e)
{
temp=s;
s=e;
e=temp;
}
p=0;

for(i=s;i<=e;i++)
{
n=i;
c=0;
m=i;
do
{
if(n%2==0)
n/=2;
else
n=n*3+1;
c++;
}while(n!=1);

if(c>t){
t=c;
p=m;
}
}
printf("Between %I64d and %I64d, %I64d generates the longest sequence of %I64d values.\n",s,e,p,t);

}
return 0;
}
shaon_cse_cu08
New poster
Posts: 50
Joined: Tue May 25, 2010 9:10 am
Contact:

### Re: 371 why TE? any body help me???help

Just use long long with %lld in ur input and output....u do not have to use field width then...

Don't Open a new thread... just search for the problem... There are many thread like this already in the board....

Try to send ur source code in between "".... Thus it will be easier to read....
I'll keep holding on...Until the walls come tumbling down...And freedom is all around .....
New poster
Posts: 44
Joined: Thu Jul 22, 2010 9:42 am

### Re: 371 why TE? any body help me???help

finally i got AC.
dewsworld
New poster
Posts: 12
Joined: Fri Aug 13, 2010 11:52 am

### Re: 371"Ackermann Functions "

Can anyone find me out the problem, please? I'm getting WA

Code: Select all

``````#include <stdio.h>

int main()
{
long long int     a, b ;
long long int     i ;
long long int     maxSeq, maxSeqNum ;
long long int     count, t ;

while( scanf("%lld%lld", &a, &b ) )
{
if( a+b == 0 )  break ;

maxSeq = 0 ;
for( i = a ; i <= b ; i++ )
{
t = i ;
count = 0 ;
do
{
if( t%2== 0 )   t = t/2 ;
else            t = 3*t + 1 ;
++count ;
}while( t != 1 ) ;

if( count > maxSeq )
{
maxSeq = count ;
maxSeqNum = i ;
}
}

printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", a, b, maxSeqNum, maxSeq ) ;
}

return 0 ;
}
``````
reja91
New poster
Posts: 1
Joined: Sat Aug 20, 2011 12:53 am

### Re: 371"Ackermann Functions "

why i am getting WA???
#include<stdio.h>

int main()
{
unsigned long a,b,c,i,d,j,f,a1,b1,m,n;
while( scanf("%ld %ld", &a, &b)==2)
{
if(a==0 &&b==0)
{
break;
}
else
{
if(a>b)
{
a1=b;
b1=a;
}
else
{
a1=a;
b1=b;
}
f=0;
for(j=a1; j<=b1; j++)
{
i=j;
c=0;
while(1)
{
c++;
d++;
if(i==1)
{
break;
}
if(i%2!=0)
{
i= 3*i +1;
}
else
{
i= i/2;
}

}
if(c>f)
{
f=c;
m=j;
}
}
printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n", a, b,m,f-1);
}
}
return 0;
}
nebulousboy
New poster
Posts: 6
Joined: Thu Dec 15, 2011 3:06 pm

### Re: 371 - Anckerman Functions - Why WA ?

can anyone help me telling why im getting wrong answer?????

# include <stdio.h>

int main()
{
long long n,i,j,m,k,l,number;

for(scanf("%lld %lld",&i,&j);i || j;scanf("%lld %lld",&i,&j))
{

m=0;
k=i;
for(k;k<=j;k++)
{
l=k;
for(n=1;;n++)
{

if(l%2!=0)
l=3*l+1;
else
l=l/2;
if(l==1)
break;

}

if(n>m)
{
m=n;
number=k;
}
}

printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n",i,j,number,m);
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 371 - Anckerman Functions - Why WA ?

nebulousboy, swap L and H if L>H.
Check input and AC output for thousands of problems on uDebug!
jams_032
New poster
Posts: 1
Joined: Tue Jan 17, 2012 12:27 pm

### Re: 371 - Anckerman Functions - Why WA ?

why im getting WA again and again.??
#include<stdio.h>
int main()
{ unsigned long i,n1,n2,n,c=1,p=0,temp,a,b,x;
while(scanf("%lu%lu",&n1,&n2)==2)
{ a=n1;
b=n2;
if(n1==0&&n2==0)
break;
else{
if(n1>n2)
{temp=n1;
n1=n2;
n2=temp;
}
p=0;
for(i=n1;i<=n2;i++)
{ c=0;
n=i;
while(n!=1)
{ if(n%2==0)
{ n=n/2;
}
else
{n=3*n+1;
}
c=c+1;
}

if(c>p)
{x=i;
p=c;
}
}
printf("Between %lu and %lu, %lu generates the longest sequence of %lu values.\n",a,b,x,p);
p=c;
}
}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 371 - Anckerman Functions - Why WA ?

For input "1 2" the output should be:
Between 1 and 2, 1 generates the longest sequence of 3 values.

Between 1 and 2, 2 generates the longest sequence of 1 values.
Check input and AC output for thousands of problems on uDebug!
bristy1588
New poster
Posts: 2
Joined: Sun Jan 29, 2012 12:09 pm

### Re: 371 - Anckerman Functions - Why WA ?

Here is my codeL

Code: Select all

``````#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
long long dp[5000005];
int main()
{
int a,b,l,h,i,num;
long long ans,ct;
long long n;

dp[1]=0;
while(1)
{
scanf("%d %d",&a,&b);
if((a==0) &&(b==0))
break;
l=min(a,b);
h=max(a,b);
ans=0;

for(i=l;i<=h;i++)
{
n=i;
ct=0;

while(1)
{
if(n==1)
{
if(i<5000005)
dp[i]=ct;
if(ct>ans)
{
ans=ct;
num=i;
}
break;
}
if(n<5000005)
{
if(dp[n]!=0)
{
ct=ct+dp[n];
if(i<5000005)
dp[i]=ct;

if(ct>ans)
{
ans=ct;
num=i;
}
break;
}
}

if((n%2)==0)
n=n/2;
else
n=3*n+1;
ct++;
}

}

printf("Between %d and %d, %d generates the longest sequence of %lld values.\n",l,h,num,ans);

}

return 0;
}

``````
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 371 - Anckerman Functions - Why WA ?

You can test with your own input in this site http://uvatoolkit.com/problemssolve.php.
kia.masster
New poster
Posts: 6
Joined: Thu Dec 22, 2011 8:47 am

### Re: 371 - Anckerman Functions

Hello. I was wondering if you might be able to help me! Why I got RE for this problem? Here is my code:

Code: Select all

``Removed!``
Thank You
Last edited by kia.masster on Sun May 20, 2012 1:10 pm, edited 2 times in total.
jocker1
New poster
Posts: 5
Joined: Sat May 19, 2012 11:10 pm

### 371 - Ackermann Functions... Why WA????

Code: Select all

``````#include <iostream>
#include <algorithm>
using namespace std;
unsigned long long calculate(unsigned long long n);
int main(){
unsigned long long a,b,d,count,c;
while(cin>>a>>b){
if(a==0 && b==0) break;
if(a>b)
swap(a,b);
d=0;
c=0;
for(long long i=a;i<=b;i++)
{
count = calculate(i);
if(d<count)
{d=count;
c=i;}
}

cout<<"Between "<<a<<" and "<<b<<", "<<c<<" generates the longest sequence of "<<d<<" values."<<endl;
}
return 0;
}

unsigned long long calculate(unsigned long long n){
long long cer=0;
while(n!=1){
if(n%2==0) n=n/2;
else n=(3*n)+1;
cer++;
}
return cer;
}
``````