0

I am trying to write a program that takes in a non negative octal number with a max of 8 to the 200000 power. The input will not have any leading zeroes, and the output must not contain any extra leading zeroes. I cannot think of a test case that this would fail, but it certainly does not go through as the correct answer. Hence, I am missing an edge case, I wonder which one. I am aware that there are some "leading zeroes" problems that could be encountered during this conversion, but I do not fully understand what it entails, hence am not able to see the problem / come up with a solution for it.

import java.util.*;
import java.io.*;

public class arithmetic{
    public static void main(String[] args) throws Exception{
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ir);
        String octal = br.readLine();
        if(octal == null){
            System.out.println("0");
            return;
        }

        long decimal = Long.parseLong(octal,8);
        System.out.println(Long.toHexString(decimal).toUpperCase());

    }
}
  • You really want to allow a 200,000 digit octal number? If yes, your program fails at `long decimal = Long.parseLong(octal,8);`, since `long` is only 64 bits and a 200,000 digit octal number is 600,000 bits of precision. Any octal number 23 digits or longer will produce incorrect output. – Jim Garrison Jun 10 '18 at 04:34

2 Answers2

1

Try an octal number with lots and lots of digits. Your input can have up to 2000000. A Java Long is only a 64-bit signed number.

You can’t put a big-enough octal number into any Java basic type, so you need a completely different approach.

One way would be to read groups of 8 octal digits (24 bits) and convert them to groups of 6 hex digits.

Or check out this answer for more on BigInteger and BigDecimal.

Bob Jacobsen
  • 1,150
  • 6
  • 9
  • I tried an algorithm without the long, but it still was stuck on the same test case apparently. The algorithm I tried was : for(int i = octal.length()-1; i >= 0; i--){ decimal+= Integer.parseInt(Character.toString(octal.charAt(i))) * Math.pow(8,count); count++; } And then, I would change that to long, and then use the toHexString on it – Yashmin Sainju Jun 10 '18 at 04:33
  • Added a bit more. – Bob Jacobsen Jun 10 '18 at 04:51
  • Thank you for that idea, I added zeros to the front of it till the length of the input was 0 modulo 4. Then, broke it into pieces of 4 and changed each piece to hex. It worked that way. Still don't know what the test case was though, because it was not a NumberFormatException that I got, but just a straight Wrong Answer – Yashmin Sainju Jun 10 '18 at 05:02
1

This question is mostly answered already, but they there is one thing that needs to be added.

I am aware that there are some "leading zeroes" problems that could be encountered during this conversion.

There won't be problems with that. Leading zeros are only relevant for Java literals in source code. (Where the leading zero means "octal" ...)


Incidentally, you could convert a large octal number (represented as characters) to hexadecimal, and vice versa without converting to a BigInteger or BigDecimal (or equivalent). But you need to either need to start at the least significant (rightmost) end of the number string, or you need to know how many characters there are in the number string.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216