Search This Blog

Sunday, January 22, 2012

Mathematical Combination Problem - JAVA



/** This is a program showing an example of 'combination'.
  * In this example, every number (0-9) have been assigned some characters.
  * For example, 0 : p,q,r
  * 1 : a,b,c
  * 2 : x,y,z
  * 3 : d,e,f
  * 4 : b,h,s,d
  * 5 : z,o,p,a,q
  * 6 : k,o,l,p,d,a,q,w
  * 7 : u,i,y,c,x,s
  * 8 : d,o,n
  * 9 : NULL
  *
  * Now, if a user enters a number, say 120, this will print the strings -
  * axp,axq,axr,ayp,ayq,ayr,azp,azq,azr,bxp,bxq,bxr,byp,byq,byr,bzp,bzq,bzr,cxp,cxq,cxr,cyp,cyq,cyr,czp,czq,czr
  * onto the screen.
  *
  * NOTE : The total number of strings ought to be printed is equal to product of number of characters written above
  * in front of the input numbers. (If number of characters is 0 i.e. NULL is written in front of a number, take
  * number of characters as 1).
  * In our example, it is equal to 3*3*3 = 27.
  *
  */


import java.util.HashMap;
import java.util.Scanner;

public class MathematicalCombinationProblem {

static Key[] Key;
static int i=1;
static String str = "";
static int inputLength;

public static void main(String[] args){

// I am using hashmap to store the 'number-value' data.

HashMap<Integer, String> key = new HashMap<Integer, String>();
key.put(0, "pqr");
key.put(1, "abc");
key.put(2, "xyz");
key.put(3, "def");
key.put(4, "bhsd");
key.put(5, "zopaq");
key.put(6, "kolpdaqw");
key.put(7, "uiycxs");
key.put(8, "don");
key.put(9, "");

// Scanner is used to get the user input

Scanner s = new Scanner(System.in);
System.out.println("Enter a number :");
String input = s.nextLine();
inputLength = input.length();
Key = new Key[inputLength];
int p=0;
int ind;
while(p<inputLength){
ind = Integer.parseInt(input.substring(p, p+1));
Key[p] = new Key(ind,key.get(ind));
p++;
}
p=0;
while(p<inputLength-1){
if(p<inputLength-1)
Key[p].nextKey = Key[p+1];
p++;
}
Key[p].nextKey = null;
getCode(Key[0]);
}

/*
* We will be using the 'RECURSION' concept in solving this problem.
* Starting from the first key i.e. keys[0], we will be traversing the last key i.e. keys[inputLength-1].
* After reaching at the last key, we will be using the 'getStringAt' method to get the character as a
* string at index=keys[n].counter where n runs from 0 to inputLength-1.
*
*/
private static void getCode(Key k){
if(k.value.length() == 0){
for(k.counter=0 ; k.counter<1; k.counter++){
if(k.nextKey != null){
getCode(k.nextKey);
}
else{
str = "";
for(int j=0 ; j<inputLength ; j++){
str +=Key[j].getStringAt(Key[j].counter);
}
System.out.println(str);
}
}
}
else{
for(k.counter=0 ; k.counter<k.value.length() ; k.counter++){
if(k.nextKey != null){
getCode(k.nextKey);
}
else{
str = "";
for(int j=0 ; j<inputLength ; j++){
str +=Key[j].getStringAt(Key[j].counter);
}
System.out.println(str);
}
}
}
}
}
/*
 * Class Key has got number,value,counter and nextKey as the attributes.
 * Suppose, user enters 52 as input then, two objects of type Key will be created keys[0] and keys[1].
 * keys[0].number=5, keys[0].value="zopaq", keys[0].counter=0, keys[0].nextKey=keys[1] and
 * keys[1].number=2, keys[1].value="xyz", keys[1].counter=0, keys[1].nextKey=null.
 * It has got one method 'getStringAt' which returns a single character as a string at a given index.
 */

class Key {

int number;
String value;
int counter;
Key nextKey;
public Key(int number , String value){
this.number = number;
this.value = value;
counter = 0;
}


/*
* This method takes an integer value as index and returns the character at that index as a string
*/

public String getStringAt(int index){
if(this.value.length() == 0)
return "";
else if(index != value.length()-1)
return value.substring(index,index+1);
else
return value.substring(index);
}
}

Sunday, January 15, 2012

Nth Prime Number - JAVA



 import java.util.Scanner;

public class NthPrimeNumberTest {

public static void main(String[] args){

PrimeNumber obj = null; // 1. Creating a reference variable obj of type PrimeNumber
int primeNumberCount = 1; // 2. to track the number of prime numbers
int j=3; // 3. counter which will go upto n
Scanner s = new Scanner(System.in);
System.out.println("Enter the number :");
int n = s.nextInt(); // 4. Taking user input as n

if(n <= 0 ){
System.out.println("Bad Input");
}
else if(n == 1){
System.out.println("2");
}
else{
while(primeNumberCount<n){ // 5. While loop will execute till we get nth prime
obj = new PrimeNumber(j); // 6. creating new object of type PrimeNumber, referred by obj
obj.checkForPrime();
if(obj.isPrime){ // 7. Checking if the number j is prime
primeNumberCount++; // 8. If it is, increase the count of prime number
// System.out.println(j); // 9. printing that prime number
}
j++;
}
System.out.println(--j); // 10. print the nth prime
}

}
}


class PrimeNumber{

int number;
boolean isPrime = true;

public PrimeNumber(int number){
this.number = number;
}

public void checkForPrime(){
for(int i=2 ; i<=(int)Math.sqrt((double)number) ; i++){
if(number%i == 0){
isPrime = false;
break;
}
}
}
}

Sunday, January 8, 2012

Factorial Of Large Number - JAVA



public class FactorialOfLargeNumberTest {

public static void main(String[] args){

int num = 80;
String product = "1";
String firstNum;
String secondNum;

MultiplyTwoLargeNumbers obj = null;

for (int i=2 ; i<=num ; i++){
secondNum = new Integer(i).toString();
firstNum = product;
obj = new MultiplyTwoLargeNumbers(firstNum,secondNum);
product = obj.mulTwoNum();
}
System.out.println(product);
}
}


class MultiplyTwoLargeNumbers{

private String number1,number2;


/** The constructor of this class 'MultiplyTwoLargeNumbers' sets the instance variables number1 and number2
*   which are of String type with the integer values passed into itusing the toString() method for String.
*/
public MultiplyTwoLargeNumbers(String n1 , String n2){
number1 = n1;
number2 = n2;
}


/** This method 'mulTwoNum' is doing all the main stuff of multiplying two numbers stored as a String in the
*   instance variables number1 and number2.
*/
public String mulTwoNum(){

int arr1[];
int arr2[];

arr1 = convertToArray(number1);
arr2 = convertToArray(number2);

int arr3[][] = new int[arr2.length][arr1.length+arr2.length];
int arr4[] = new int[arr1.length+arr2.length+2];

for(int p=0 ; p<arr1.length+arr2.length+1 ; p++)
arr4[p]=0;

for (int y=0 ; y<arr2.length ; y++){
for (int z=0 ; z<arr1.length+arr2.length ; z++){
arr3[y][z] = 0;
}
}

for (int k=0,row=0 ; k<arr2.length ; k++,row++){ //for num2
for (int j=0 , col=arr2.length+arr1.length-1-k ; j<arr1.length ;col--, j++){ //for num1

arr3[row][col] = arr1[arr1.length-1-j]*arr2[arr2.length-k-1];

}

}
for (int y=0 ; y<arr2.length ; y++){
for (int z=0 ; z<arr1.length+arr2.length ; z++){
}
}


for (int y=arr1.length+arr2.length-1 ; y>0 ; y--){
for (int z=0 ; z<arr2.length ; z++){
arr4[(arr1.length+arr2.length+1)-(arr1.length+arr2.length-1)+y] += arr3[z][y];

}
}

for(int u=0 ; u<arr4.length ; u++){
}

for(int q=arr4.length-1 ; q>0 ; q--){
if(arr4[q]>9){
arr4[q-1] += arr4[q]/10;
arr4[q]=arr4[q]%10;
}
}
String strToRet="";

for(int u=0 ; u<arr4.length ; u++){
strToRet += new Integer(arr4[u]).toString();
}

return trimResultString(strToRet);
}


 /** The method 'convertToArray' takes String as a parameter containing an integer value and returns
 *   an array of integer.
 *   For example, if str = "216" then, this method will return a with values a[0]=2, a[1]=1 and a[2]=6.
 */
public int[] convertToArray(String str){

int a[] = new int[str.length()];
for(int i=0 ; i<str.length() ; i++){
a[i] = Integer.parseInt(str.substring(i,i+1));
}
return a;
}


    /** The method 'trimResultString' takes a String parameter and returns a String value.
* This method is to get rid of extra 0s present at the beginning of 'res'.
    * For example, if res = "000156" then, this method will return "156".
*/
public String trimResultString(String res){

int ind[] = new int[10];
int index=99999;

for(int i=0 ; i<10 ; i++){
ind[i] = res.indexOf(new Integer(i).toString());
}
for(int i=1 ; i<9 ; i++){
if(ind[i]>0)
index = Math.min(index, ind[i]);
}
res = res.substring(index);

return res;
}
}


Sunday, January 1, 2012

Multiply Two Large Numbers - JAVA



public class MultiplyTwoLargeNumbersTest {


/** a and b hold very large integer values such that if these are multiplied, the result can't be stored in
 *  another integer variable as the size will cross the limit or the maximum value an integer variable can hold.
 *  Thus, this code is designed to multiply such big numbers and store the result into a String variable.
 */
public static void main(String[] args){

int a = 999999999;
int b = 888888888;

MultiplyTwoLargeNumbers obj = new MultiplyTwoLargeNumbers(new Integer(a).toString(),new Integer(b).toString());
String result = obj.mulTwoNum();
System.out.println(result);
}
}

class MultiplyTwoLargeNumbers{

private String number1,number2;


/** The constructor of this class 'MultiplyTwoLargeNumbers' sets the instance variables number1 and number2
*   which are of String type with the integer values passed into itusing the toString() method for String.
*/
public MultiplyTwoLargeNumbers(String n1 , String n2){
number1 = n1;
number2 = n2;
}


/** This method 'mulTwoNum' is doing all the main stuff of multiplying two numbers stored as a String in the
*   instance variables number1 and number2.
*/
public String mulTwoNum(){

int arr1[];
int arr2[];

arr1 = convertToArray(number1);
arr2 = convertToArray(number2);

int arr3[][] = new int[arr2.length][arr1.length+arr2.length];
int arr4[] = new int[arr1.length+arr2.length+2];

for(int p=0 ; p<arr1.length+arr2.length+1 ; p++)
arr4[p]=0;

for (int y=0 ; y<arr2.length ; y++){
for (int z=0 ; z<arr1.length+arr2.length ; z++){
arr3[y][z] = 0;
}
}

for (int k=0,row=0 ; k<arr2.length ; k++,row++){ //for num2
for (int j=0 , col=arr2.length+arr1.length-1-k ; j<arr1.length ;col--, j++){ //for num1

arr3[row][col] = arr1[arr1.length-1-j]*arr2[arr2.length-k-1];

}

}
for (int y=0 ; y<arr2.length ; y++){
for (int z=0 ; z<arr1.length+arr2.length ; z++){
}
}


for (int y=arr1.length+arr2.length-1 ; y>0 ; y--){
for (int z=0 ; z<arr2.length ; z++){
arr4[(arr1.length+arr2.length+1)-(arr1.length+arr2.length-1)+y] += arr3[z][y];

}
}

for(int u=0 ; u<arr4.length ; u++){
}

for(int q=arr4.length-1 ; q>0 ; q--){
if(arr4[q]>9){
arr4[q-1] += arr4[q]/10;
arr4[q]=arr4[q]%10;
}
}
String strToRet="";

for(int u=0 ; u<arr4.length ; u++){
strToRet += new Integer(arr4[u]).toString();
}

return trimResultString(strToRet);
}


 /** The method 'convertToArray' takes String as a parameter containing an integer value and returns
 *   an array of integer.
 *   For example, if str = "216" then, this method will return a with values a[0]=2, a[1]=1 and a[2]=6.
 */
public int[] convertToArray(String str){

int a[] = new int[str.length()];
for(int i=0 ; i<str.length() ; i++){
a[i] = Integer.parseInt(str.substring(i,i+1));
}
return a;
}


    /** The method 'trimResultString' takes a String parameter and returns a String value.
* This method is to get rid of extra 0s present at the beginning of 'res'.
    * For example, if res = "000156" then, this method will return "156".
*/
public String trimResultString(String res){

int ind[] = new int[10];
int index=99999;

for(int i=0 ; i<10 ; i++){
ind[i] = res.indexOf(new Integer(i).toString());
}
for(int i=1 ; i<9 ; i++){
if(ind[i]>0)
index = Math.min(index, ind[i]);
}
res = res.substring(index);

return res;
}
}