100 - The 3n + 1 problem

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

Moderator: Board moderators

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Your main() function is supposed to return an integer.

Red Cent
New poster
Posts: 5
Joined: Sun Jan 22, 2006 9:23 pm

ugh

Post by Red Cent »

oh ...


like DUH...

Lets see if that works then.

Red Cent
New poster
Posts: 5
Joined: Sun Jan 22, 2006 9:23 pm

laughing all the way...

Post by Red Cent »

Okay.

Now it compiles (because I forgot that stupid return statement)

Now it reports TIME LIMIT EXCEEDED.

Is my program that inefficient?

Red Cent
New poster
Posts: 5
Joined: Sun Jan 22, 2006 9:23 pm

Wow - I bow to those who solved this deceivingly simple prob

Post by Red Cent »

Okay.

I narrowed down what my program was doing.

Geez.

It's a simple problem that's cursed website owners and commercial enterprises.

I found a test case number (by putting in ranges of test inputs) which I wont reveal here, unless asked of course, that when I put in that number as input, my program would run on forever.

It's because the number would eventually lead to such a high number in 3N+1, that it would overflow and become negative - thus never reaching a solution.

Geez.

It reminds me of the old news story (help me out if you can remember) where a guy was buying products from a store online. He was putting in a number so big (I dont remember if it was quantity or price), he would be getting products delivered to his door along with a sizeable check (because the price turned out to be negative, he was owed money according to the system).

Basically an employee of the company noticed this and asked (why the **** are we shipping a product to this guy along with a check?)

Ugh!

Back to the drawing board.

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Well, there's no magical number. From the look of you code (I haven't tested) your program should fail on input like

Code: Select all

10 100
100 10
10 100

Red Cent
New poster
Posts: 5
Joined: Sun Jan 22, 2006 9:23 pm

duh

Post by Red Cent »

I forgot I changed the loop from counter != second number to counter <=secondnumber

making the bigger number entered first not work!

Oops

Thanks.

sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Location: Vancouver, BC, Canada
Contact:

Post by sclo »

this problem is not the easiest problems, if you're new, try some other problems first

tellmewhy
New poster
Posts: 7
Joined: Tue Feb 07, 2006 7:30 pm

100 Compile error 6 times... Help!!

Post by tellmewhy »

my code:
-------------------------------------------------------------------------------------
import java.util.*;

class Main
{
static int Begin(int a, int b, int flag)
{
int count = 0, cyclelength = 0, c=0;
int temp = b;
if (a>b){
b=a;
a=temp;}
for (int i=a;i<=b;i++,count=0)
{
c=i;
while(c!=1)
{
count++;
if (c%2==1)
c=3*c+1;
else
c=c/2;
}

if (count>cyclelength)
{
cyclelength=count+1;
}
}
return cyclelength;
}

public static void main(String[] args)
{
int flag = 0;
int i=0;
String[] storedline = new String[1000];
Main TryOne = new Main();
Scanner scanner = new Scanner(System.in);

while (flag!=1)
{
String getinput = scanner.nextLine();
if (!(getinput.equals("")))
{
Scanner scanner1 = new Scanner(getinput);
int a = scanner1.nextInt();
int b = scanner1.nextInt();
int temp = TryOne.Begin(a,b,flag);
storedline = a + " " + b + " " + temp;
i++;
}
else
flag++;
}
for (int j=0; j<i; j++)
System.out.println(storedline[j]);
}
}

compile error!! i read the sticky, made amendemnts and still get CE... i suspected it is becos of the Scanner class i used... any help??
.....Life is just like a LM741........

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Yes. Scanner class is only available in JDK 1.5 and the JDK used in this judge unfortunately is very old, which is JDK 1.1

f.eliel
New poster
Posts: 10
Joined: Wed Feb 08, 2006 4:31 pm

Post by f.eliel »

I'm getting WA and i can't find the problem. It works just fine when i test it.

Code: Select all

#include <stdio.h>

int main(void)
{
   long x, a, b, n;
   int m, w, c;
   while(scanf("%ld %ld", &a, &b) == 2){
      if (a < b)
         w = 1;
      else
         w = -1;
      m = 0;
      x = a;
      while (x != b){
         n = x;
         c = 1;
         while (n > 1){
            if (n%2)
               n = n*3 + 1;
            else
               n /= 2;
            c++;
         }
         if (c > m)
            m = c;
         x +=w;
      }
      printf("%li %li %d\n", a, b, m);
   }
   return 0;
}
Can anyone help me?

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
Input

Code: Select all

22 22
21 22
Output

Code: Select all

22 22 16
21 22 16

f.eliel
New poster
Posts: 10
Joined: Wed Feb 08, 2006 4:31 pm

Post by f.eliel »

ahhh
finally...

thanx man...

tellmewhy
New poster
Posts: 7
Joined: Tue Feb 07, 2006 7:30 pm

Post by tellmewhy »

wth???? so i need to write a similar type of class to scanner??? any help ppl?
.....Life is just like a LM741........

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Have a look the HOWTOs forum. Something there should help.

tellmewhy
New poster
Posts: 7
Joined: Tue Feb 07, 2006 7:30 pm

Post by tellmewhy »

ok... i replaced the scanner class using the token..... now got a WA liao.... finally.... :D
.....Life is just like a LM741........

Post Reply

Return to “Volume 1 (100-199)”