3n+1 problem at UVa

Posted by ShahradR on Stack Overflow See other posts from Stack Overflow or by ShahradR
Published on 2010-12-16T17:01:20Z Indexed on 2010/12/26 6:54 UTC
Read the original article Hit count: 426

Filed under:
|

Hello,

I am having trouble with the first question in the Programming Challenges book by Skiena and Revilla. I keep getting a "Wrong Answer" error message, and I don't know why. I've ran my code against the sample input, and I keep getting the right answer. Anyways, if anyone could help me, I would really appreciate it :)

Here is the problem URL: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=36

And here is the code:`

import java.util.Scanner;

public class Main
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)
 {
  while (kb.hasNext())
  {
   long[] numericalInput = {0, 0};

   long i = kb.nextLong();
   long j = kb.nextLong();

   if (i > j)
   {
    numericalInput[0] = i;
    numericalInput[1] = j;
   }
   else
   {
    numericalInput[0] = j;
    numericalInput[1] = i;
   }

   long maxIterations = 0;

   for (long n = numericalInput[0]; n <= numericalInput[1]; n += 1)
   {
    if (maxIterations < returnIterations(n))
     maxIterations = returnIterations(n);
   }

   System.out.println(i + " " + j + " " + maxIterations);
  }

  System.exit(0);
 }

 public static long returnIterations(long num)
 {
  long iterations = 0;

  while (num != 1)
  {
   if (num % 2 == 0)
    num = num / 2;
   else
    num = 3 * num + 1;

   iterations += 1;
  }
  iterations += 1;

  return iterations;
 }
}

` EDIT: I think the problem is with the output. I tried to make it accept all the input first and then display all the answers at once, but I didn't know the terminating condition. I resorted to this method, but I'm not sure that's what the judge wants...

© Stack Overflow or respective owner

Related posts about collatz

Related posts about uva

  • UVA #10410 Tree Reconstruction

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I have worked on UVA 10410 Tree Reconstruction several days. But I can't get the correct answer unitl now. I have used an algorithm similar to the one which we always use to recovery a binary tree through the preorder traversal and the inorder traversal. But it can't work. Can anyone help me? Thanks… >>> More

  • 3n+1 problem at UVa

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Hello, I am having trouble with the first question in the Programming Challenges book by Skiena and Revilla. I keep getting a "Wrong Answer" error message, and I don't know why. I've ran my code against the sample input, and I keep getting the right answer. Anyways, if anyone could help me, I would… >>> More

  • UVa #112 Tree Summing

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I'm working on UVa #112 Tree Summing. I have what I think should be a working solution but it is not accepted by the online judge due to a basic misunderstanding of the problem on my part. Consider the following inputs: -1 (-1()()) 77 (77(1()())()) or diagrammatically, the trees look like: -1… >>> More

  • Uva's 3n+1 problem

    as seen on Stack Overflow - Search for 'Stack Overflow'
    I'm solving Uva's 3n+1 problem and I don't get why the judge is rejecting my answer. The time limit hasn't been exceeded and the all test cases I've tried have run correctly so far. import java.io.*; public class NewClass{ /** * @param args the command line arguments */ … >>> More

  • UVA Online Judge 3n+1 : Right answer is Wrong answer

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Ive been toying with this problem for more than a week now, I have optimized it a lot, I seem to be getting the right answer, since it's the same as when I compare it to other's answers that got accepted, but I keep getting wrong answer. Im not sure what's going on! Anyone have any advice? I think… >>> More