Saturday, September 1, 2012

Tower of Hanoi program in Java ?



The towers of hanoi is a popular problem. You have three poles and n disks which fit on the poles. All disks have different size. They are stacked on pole 1 in the orders of their size. The largest disk is on the bottom, the smallest is on the top.

The task is to move all disk from pole 1 to pole 3 under the following restrictions.

Only one disk can be moved

A larger disk can not be placed on a smaller disk.

The recursive algorithm works like the following: move n-1 disk from the starting pole to the pole which is neither start nor target (intermediate), move disk n to the target pole and then move n-1 disk from the intermediate pole to the target pole. The n-1 disks are moved recursively.


public class MinimumWindow {

public static void move(int n, int startPole, int endPole) {
if (n == 0) {
return;
}
int intermediatePole = 6 - startPole - endPole;
move(n - 1, startPole, intermediatePole);
System.out.println("Move " + n + " from " + startPole + " to "
+ endPole);
move(n - 1, intermediatePole, endPole);
}

public static void main(String[] args) {
move(5, 1, 3);
}

}


Below is the output...

Move 1 from 1 to 3
Move 2 from 1 to 2
Move 1 from 3 to 2
Move 3 from 1 to 3
Move 1 from 2 to 1
Move 2 from 2 to 3
Move 1 from 1 to 3

Prime Factorization in Java ?


import java.util.ArrayList;
import java.util.List;

public class PrimeFactorization {

public static List<Integer> primeFactors(int numbers) {
int n = numbers;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 1) {
factors.add(n);
}
return factors;
}

public static void main(String[] args) {
for (Integer integer : primeFactors(44)) {
System.out.println(integer);
}

System.out.println("---------");

for (Integer integer : primeFactors(3)) {
System.out.println(integer);
}
}

}

Below is the Output....

2
2
11
---------
3

Determine Prime number with the sieve of Eratosthenes in Java in java ?


import java.util.ArrayList;
import java.util.List;

public class DeterminePrime{

public static List<Integer> calcPrimeNumbers(int n) {
boolean[] isPrimeNumber = new boolean[n + 1]; // boolean defaults to
// false
List<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i < n; i++) {
isPrimeNumber[i] = true;
}
for (int i = 2; i < n; i++) {
if (isPrimeNumber[i]) {
primes.add(i);
// Now mark the multiple of i as non-prime number
for (int j = i; j * i <= n; j++) {
isPrimeNumber[i * j] = false;
}
}

}

return primes;
}

public static void main(String[] args) {
List<Integer> calcPrimeNumbers = calcPrimeNumbers(21);
for (Integer integer : calcPrimeNumbers) {
System.out.println(integer);
}
System.out.println("Prime counting function (Pie(N)) : "
+ calcPrimeNumbers.size());
}

}


Below is the output...

2
3
5
7
11
13
17
19
Prime counting function (Pie(N)) : 8

Euclid's Greatest Common divisor alogorithmn ?


public class EuclidGCdAlgorithmn {

public static int gcd(int p, int q) {

if (q == 0) {
return p;
}

return gcd(q, p % q);
}

// Test enable assert check via -ea as a VM argument

public static void main(String[] args) {
System.out.print(" "+gcd(4, 16));

System.out.print(" "+gcd(16, 4));

System.out.print(" "+gcd(15, 60));
System.out.print(" "+gcd(15, 65));

System.out.print(" "+gcd(1052, 52));
}

}


Below is the output....

 4
 4
 15
 5
 4

Odd even Transposition algorithmn in Java ?



public class OddEvenTransposition {

public static void main(String a[]) {

int i;
int array[] = { 12, 9, 4, 99, 120, 1, 3, 10, 13 };

System.out.println(" Odd Even Transposition Sort\n\n");

System.out.println("Values Before the sort:\n");

for (i = 0; i < array.length; i++){
System.out.print(array[i] + "  ");
}

System.out.println();

odd_even_srt(array, array.length);

System.out.print("Values after the sort:\n");

for (i = 0; i < array.length; i++){
System.out.print(array[i] + "  ");
}

}

public static void odd_even_srt(int array[], int n) {
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j + 1 < n; j += 2)
if (array[j] > array[j + 1]) {
int T = array[j];
array[j] = array[j + 1];
array[j + 1] = T;
}
for (int j = 1; j + 1 < array.length; j += 2)
if (array[j] > array[j + 1]) {
int T = array[j];
array[j] = array[j + 1];
array[j + 1] = T;
}
}
}
}


Below is the output....

Values Before the sort:

12  9  4  99  120  1  3  10  13  

Values after the sort:

1  3  4  9  10  12  13  99  120  

Merge sort algorithmn in Java?


public class MergeSort {

public static void main(String a[]) {
int i;
int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };

System.out.println("Values Before the sort:\n");

for (i = 0; i < array.length; i++) {
System.out.print(array[i] + "  ");
}

System.out.println();

mergeSort_srt(array, 0, array.length - 1);

System.out.print("Values after the sort:\n");

for (i = 0; i < array.length; i++) {
System.out.print(array[i] + "  ");
}

}

public static void mergeSort_srt(int array[], int lo, int n) {
int low = lo;
int high = n;
if (low >= high) {
return;
}

int middle = (low + high) / 2;
mergeSort_srt(array, low, middle);
mergeSort_srt(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high - 1; k >= low; k--) {
array[k + 1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
}


Below is the output...

Values Before the sort:

12  9  4  99  120  1  3  10

Values after the sort:

1  3  4  9  10  12  99  120