694 - The Collatz Sequence

All about problems in Volume 6. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Try my formatting...

Code: Select all

"Case %d: A = %d, limit = %d, number of terms = %d\n"
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

694 P.E.

Post by WR »

Thanks a lot, Jan!

It DID help. And special thanks for the fast response!!!

Care to share your knowledge?

The format specifier for a LONG variable is %ld, isn't it?
Why does %d work instead?

Needless to say, my results were ok, otherwise the verdict would have been a WRONG ANSWER (:().
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

You are welcome. I used int (not long) because in uva's compiler (and mine, too) int and long both require 32bit. May be that's why both identifier work for both (int & long).
Ami ekhono shopno dekhi...
HomePage
scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm

Post by scott1991 »

I'm getting tle and have no idea on making it faster...can someone help please?

Code: Select all

#include<iostream>

int main()
{	
		unsigned int start, max, terms=1,test=1,A;
a:
	scanf("%u %u",&start,&max);
		if(start<0&&max<0)
		{return 0;}
		else
		{
			A=start;
		while(start<=max&&start!=1)
		{
			start = (start&1)==0?(start>>1):(start*3+1);
			terms++;
		}
		printf("Case %u: A = %u, limit = %u, number of terms = %u\n",test,A,max,terms);
		test++;terms=1;
		goto a;
		}
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Both start and end are unsigned integers. So, you will not find -1 in the input. I mean if the input is -1 -1, then start and max both will be unsigned(-1) = 4294967295.
Kikuneechan
New poster
Posts: 1
Joined: Sun Nov 01, 2009 10:06 pm

Re: 694 - The Collatz Sequence

Post by Kikuneechan »

Heya. I'm doing this in Java. I've coded everything and everything prints out as the page says so, but I get TIme Limit Exceeded which is really aggravating.

Code: Select all

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner fred = new Scanner(System.in);
    int start;
    int limit;
    int terms;
    int a = 0;
    
    while (fred.hasNext()) {
      start = fred.nextInt();
      limit = fred.nextInt();
      if (start > 0) {
        terms = getLength(start, limit);
        a++;
      } else {
        break;
      }
      System.out.println("Case "+a+": A = "+start+", limit = "+limit+", number of terms = "+terms);
    }
  }
  
  public static int getLength(int x, int y) {
    int length = 1;
    while (x != 1) {
      if (x <= y) {
        if ( x % 2 == 0) {
          x = x / 2;
          length++;
        }else{
          x = x * 3 + 1;
          length++;
        }
      } else {
        length--;
        break;
      }
    }
    
    return length;
  }
}
Thankies~
devmehedee
New poster
Posts: 2
Joined: Sun Apr 17, 2011 11:52 am
Location: CSE BUET, Bangladesh.

Re:

Post by devmehedee »

Tanzim-Saqib wrote:

Code: Select all

1 1
2147483647 1
2147483647 2
2147483647 214748364
1 0
In the problem, they said The initial value of A is always less than L. Then why are you giving these inputs? I got AC while my code gives different results for your inputs.
Think FREE, Think OPEN SOURCE
muctadir.dinar
New poster
Posts: 4
Joined: Sat Apr 16, 2011 10:04 pm
Location: IIT, DU
Contact:

Re: 694 - The Collatz Sequence

Post by muctadir.dinar »

Can someone tell where is problem in my code...???

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int main()
{
//    freopen("input.txt", "r", stdin);

    long long int a, limit;
    int caseCount = 0;
    while (scanf("%lld %lld", &a, &limit)!=EOF && a>-1 && limit>-1)
    {
        long long int aBack = a;

        caseCount++;

        int i = 0;
        for (; a!=1 && a<=limit;i++)
        {
            if (a%2 == 0)
            {
                a = a/2;
            }
            else if (a%2!=0)
            {
                a = (a*3)+1;
            }
        }


        printf("Case %d: A = %lld, limit = %lld, number of terms = %d\n",caseCount, aBack, limit, i);
    }

    return 0;
}
You are the one, who can make a difference...
NIPU
New poster
Posts: 1
Joined: Thu Aug 11, 2011 11:01 pm

Re: 694 - The Collatz Sequence

Post by NIPU »

i've got wrong answer so many time that its starting to frustrate me. i cant really find my problem. can anyone tell me what i am doing wrong

#include<stdio.h>
void main()
{
long long int a,b,c,d;
d=1;
while(1)
{
scanf("%lld %lld",&a,&b);
if(a<0&&b<0)break;
c=1;
while(a>1)
{
if (a%2==0)a=a/2;
else a=3*a+1;
if(a>b)break;
c++;
}
printf("case %lld:",d);
printf(" A = %lld,",a);
printf(" limit = %lld,",b);
printf(" number of terms = %lld\n",c);
d++;
}
}
ignited
New poster
Posts: 1
Joined: Thu Sep 08, 2011 1:24 pm

Re: 694 - The Collatz Sequence

Post by ignited »

NIPU wrote:i've got wrong answer so many time that its starting to frustrate me. i cant really find my problem. can anyone tell me what i am doing wrong

#include<stdio.h>
void main()
{
long long int a,b,c,d;
d=1;
while(1)
{
scanf("%lld %lld",&a,&b);
if(a<0&&b<0)break;
c=1;
while(a>1)
{
if (a%2==0)a=a/2;
else a=3*a+1;
if(a>b)break;
c++;
}
printf("case %lld:",d);
printf(" A = %lld,",a);
printf(" limit = %lld,",b);
printf(" number of terms = %lld\n",c);
d++;
}
}
ur answer for tc 1 is ==> Case 1: A = 1, limit = 100, number of terms = 8
the right answer is Case 1: A = 3, limit = 100, number of terms = 8
see where's wrong there ? :)
sumon sarker
New poster
Posts: 4
Joined: Tue Mar 20, 2012 7:58 am
Location: Dhaka,Bangladesh
Contact:

Re: 694 - The Collatz Sequence

Post by sumon sarker »

Sample Input

3 100
34 100
75 250
27 2147483647
101 304
101 303
-1 -1

Sample Output

Case 1: A = 3, limit = 100, number of terms = 8
Case 2: A = 34, limit = 100, number of terms = 14
Case 3: A = 75, limit = 250, number of terms = 3
Case 4: A = 27, limit = 2147483647, number of terms = 112
Case 5: A = 101, limit = 304, number of terms = 26
Case 6: A = 101, limit = 303, number of terms = 1

I got AC with
Case 5: A = 101, limit = 304, number of terms = 2
For input with 101 304
stadoblech
New poster
Posts: 3
Joined: Thu Nov 08, 2012 6:36 pm

694 - The Collatz Sequence - Time limit exceeded

Post by stadoblech »

Code: Select all

import java.io.PrintWriter;
import java.util.Scanner;


public class Main { 
    
    public static void main(String[] args) throws Exception{ 
        
        Scanner scan = new Scanner(System.in);
        
        int A = 0;
        int limit = 0;
        
        while(scan.hasNextInt()) { 
            A = scan.nextInt();
            limit = scan.nextInt();
            if(A == -1 && limit == -1) { 
                return;
            }
            System.out.println("Case 1: A = "+A+", limit = "+limit+", number of terms = "+sequence(A, limit));
        }
    }
    
    private static int sequence(int A,int limit) { 
        int result = 0;

        
        while(A != 1){ 
            
            if(A > 2147483647 && limit > 2147483647) { 
                break;
            }
            
            
            if(A % 2 == 0) {                
                A = A/2;
                result++;
            } else { 
                A = (A * 3) + 1;
                result++;
            }
            
            if(A > limit) { 
                break;               
            }
            
            if(A == 1) { 
                result++;
            }

        }

        return result;

    }
}
anyone could help ?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 694 - The Collatz Sequence - Time limit exceeded

Post by brianfry713 »

A line that contains two negative integers follows the last case. That doesn't mean they are -1.
Input 1 1, number of terms is 1.
Check input and AC output for thousands of problems on uDebug!
mahbubcseju
New poster
Posts: 2
Joined: Fri Apr 26, 2013 5:27 pm

uva 694

Post by mahbubcseju »

why my code is compile error
#include<stdio.h>
int main()
{
long long int a,l,m,k=0,A,n=0,temp;

while(scanf("%lld%lld",&A,&l)==2)
{
if(A<0&&l<0)
{
break;
}

if(A>l)
{
temp=A;
A=l;
l=temp;
}

k=0;
a=A;
while(a>1)
{
if(a%2==0)
{
a=a/2;
}
else
{
a=3*a+1;
}
k++;

if(a>l)
{
break;
}
}
if(a==1)
{
k=k+1;
}
n++;

printf("Case %lld:",n);

printf(" A = %lld,",A);
printf(" limit = %lld,",l);
printf(" number of terms = %lld\n",k);





}
return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: uva 694

Post by brianfry713 »

That is AC code. You can click "My Submissions" to see the reason for your Compile Error.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 6 (600-699)”