-1

I found a challenge online, and thought its pretty interesting but tried multiple times to understand what base 3 is and how u can get there, but unfortunately no clue how.

Write a method, convertIntegerToBase3() which does the following:

-- Accepts an integer parameter (from 0 to 26) and converts it to base 3, which is stored as a string, which is the return value.

public String convertIntgerToBase(int num){

    if(numberConv >= 0 && numberConv <= 26){
        //What do I do here?
     }
    else{
        System.out.println("Error! Number entered wasn't in the range of 0 and 26);
    }
}
  • That code won't even compile. It's `int` not `Int`. Also you forgot to terminate the string in the `println` call. – Zabuzard Jun 06 '18 at 15:54
  • 2
    `Integer.toString(numberConv, 3)`. – Andy Turner Jun 06 '18 at 15:55
  • how is it a duplicate ? – alroithmhelp Jun 06 '18 at 15:55
  • Also see [Base 10 to base 2,8,16 conversion in java](https://stackoverflow.com/questions/16008685/base-10-to-base-2-8-16-conversion-in-java) which links to the same duplicate. – Zabuzard Jun 06 '18 at 15:58
  • 1
    To help you understand what base 3 is : in base 10, you count like that : `0, 1, 2, 3 ..., 9, 10, 11, ...` ; in base 2 you count like that : `0, 1, 10, 11, 100, 101, ...`. In base 3, you count like that : `0, 1, 2, 10, 11, 12, 20, ...` – Aaron Jun 06 '18 at 15:59

2 Answers2

2

Update

Because below answer at just works up to base 10 I've come up with following answer which works up to base 36:

private static final char[] CHARS = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();

private static String convertIntToBase(int i, int base){
    final StringBuilder builder = new StringBuilder();
    do{
        builder.append(CHARS[i % base]);
        i /= base;
    } while(i > 0);
    return builder.reverse().toString();
}

The logic stays the same, but now by accessing the CHARS array we can get up to base 36. Because we have now the whole alphabet and the numbers to create a new number in an other base.

Using this will now yield correct numbers for e.g. base 16:

convertIntToBase(255, 16);

Will return the correct hex value:

ff

Old

It's pretty simple, by division and using the remainder of the base and can be made also generic:

public String convertIntToBase(int i, int base){
    final StringBuilder builder = new StringBuilder();
    do {
        builder.append(i % base);
        i /= base;
    } while(i > 0);
    return builder.reverse().toString();
}

which then can be used like the following:

convertIntToBase(24, 3);

Which yields:

220

This works, as said with division and the remainder (modulo). With the sample number 24 we can go through the steps pretty easy. The iteration and calculation (i /= base) % base can be split up into the following parts:

  1. divide i through the base (i /= base)
  2. split of the comma places (This is done automatically because division of integers in java are always floored)
  3. See what remainder stays by whole-dividing the i with the base (i % base)
  4. repeat if i is bigger than zero

So with the i = 24 and base = 3 it goes through the following steps:

  1. 24 % 3 = 0
  2. 24 / 3 = 8
  3. 8 % 3 = 2, because by whole-division we get that 3 is 8 times in 2, leaving 2
  4. 8 / 3 = 2
  5. 2 % 3 = 0
  6. 2 / 3 = 0

leaving you with the known result: 220

Lino
  • 19,604
  • 6
  • 47
  • 65
1

Base 3 is simply representing a number as a polynomial where the base of the polynomial is 3. It can extend for all basis, like base 1.2, base 99... etc.

Number in Decimal = (A0)*3^0 + (A1)*3^1 + (A2)*3^2 ...
where AX can have in value that falls with in (x)mod(3) or
(x)mod(base). In your case a range of [0,1,2]. (x)mod(Anything) is 
always positive or zero.

Try to think how you could guess with your base might, be but, its essentially a problem asking you to divide a number. If you know how to divide your integer number with that polynomial, you've figured out the problem.

Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133