Thursday, 19 January 2017

Base...

Numbers have different representations depending on the bases on which they are expressed. For example in base 3, the number 12 is written as 110 (1 x 32 + 1 x 31 + 0 x 30), but in base 8 it is written as 14(1 x 81 + 4 x 80).
Consider, for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose 12 was a base 3 number and 5 was a base 6 number then what happens, 12 base 3 = 1 x 31 + 2 x 30, or 5 base 6 or 5 base 10 (5 in any base is equal to 5 base 10). So 12 and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers, X and Y, and calculate the smallest base for X and smallest base for Y (likely different from X) so that X and Y represent the same value. The base associated with X and Y will be between 1 and 20 (both inclusive). In representing these numbers the digits 0 to 9 have their usual decimal interpretations. The upper case alphabetic characters A through J represent digits 10 through 19 respectively.
Test your program for the following data and some random data:
SAMPLE DATA:
INPUT:
X = 12, Y = 5
OUTPUT:
12(base 3) = 5 (base 6)

INPUT:
X = 10, Y = A
OUTPUT:
10 (base 10) = A (base 11)

INPUT:
X = 12, Y = 34
OUTPUT:
12 (base 17) = 34 (base 5)

INPUT:
X = 123, Y = 456
OUTPUT:
1 2 3 is not equal to 456 in any base between 2 to 20

INPUT:
X = 42, Y = 36
OUTPUT:

42 (base 7) = 36 (base 8)



Solution:-

import java.util.*;
class base
{
    int BaseToDecimal(String number, int baseN)
    {
        number=number.toUpperCase();
        int answer=0;
        int length=number.length();
        int digit, multiplier;
        for(int i=0;i<length;i++)
           {
            multiplier=(int)Math.pow(baseN,length-i-1);
            if(Character.isDigit(number.charAt(i)))
                {
                digit=(number.charAt(i)-'0');
                     }
            else
                {
                digit=(number.charAt(i)-55);
                    }
            answer=answer+digit*multiplier;
           }
        return answer;
    }
    int digitValue(char c)
    {
         int result=0;
         if (Character.isDigit(c)) result= c-'0';
         else if(Character.isLetter(c)) result= c-'A'+10;
         return result;
    }
    int maximumDigit(String strNumber)
    {
       int i=0,result=0,l=strNumber.length();
       int digit;
       for(i=0;i<=l-1;i++)
       {
           digit= digitValue(strNumber.charAt(i));
           if(digit>result) result=digit;
       }
       return result;
    }
     void perform()
     {
     Scanner s = new Scanner(System.in);
        System.out.println("Enter X: ");
        String X = s.nextLine();
        System.out.println("Enter Y: ");
        String Y = s.nextLine();        
        int value1, value2;
        boolean found = false;
        int initial_i=maximumDigit(X)+1;
        int initial_x=maximumDigit(Y)+1;
        for (int i = initial_i; i <= 20; i++)
        { 
            value1 = BaseToDecimal(X, i);
            for (int x = initial_x; x <= 20; x++)
                {
                value2 = BaseToDecimal(Y, x);
                if (value1==value2)
                     {
                    System.out.println(X + "(base " + i + ")=" + Y + "(base " + x + ") ");
                    found = true;
                    break;
                    }
            }
            if(found) break;
        }
        if (!found)
           {
            System.out.println(X + " is not equal to " + Y + " in any base between 2 to 20.");
               } 
     }
    public static void main(String args[])
    {        
        base obj=new base();
        obj.perform();
    }
}






No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...