Senin, 16 Juli 2012

Algorithm Binary representation for Java Programming

Write a program IntegerToBinary.Java  that takes a positive integer N (in decimal) from the command line and prints out its binary representation. Recall, in program 1.3.7, we used the method of subtracting out powers of 2. Instead, use the following simpler method: repeatedly divide 2 into N and read the remainders backwards. First, write a while loop to carry out this computation and print the bits in the wrong order. Then, use recursion to print the bits in the correct order.


IntegerToBinary.java



/*************************************************************************
 *  Compilation:  javac IntegerToBinary.java
 *  Execution:    java IntegerToBinary N
 *  
 *  Prints out the binary representation of N.
 *
 *  % java IntegerToBinary 8
 *  1000
 *
 *  % java IntegerToBinary 366
 *  101101110
 *
 *************************************************************************/

public class IntegerToBinary {

   public static void convert(int n) {
      if (n == 0) return;
      convert(n / 2);
      System.out.print(n % 2);
   }

   public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      convert(N);
      System.out.println();
   }

}



Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Aug 5 12:25:48 EDT 2011.

Euclid's algorithm Java Programming


The greatest common divisor (gcd) of two positive integers is the largest integer that divides evenly into both of them. For example, the greatest common divisor of 102 and 68 is 34 since both 102 and 68 are multiples of 34, but no integer larger than 34 divides evenly into 102 and 68. We can efficiently compute the gcd using the following property, which holds for positive integers p and q:
If p > q, the gcd of p and q is the same as the gcd of q and p % q.

The static method gcd() in Euclid.java is a compact recursive function whose reduction step is based on this property.
gcd(1440, 408) 
   gcd(408, 216) 
      gcd(216, 192) 
         gcd(192, 24)
            gcd(24, 0)
               return 24
            return 24 
         return 24 
      return 24 
   return 24 
 


 
Euclid.java

/*************************************************************************
 *  Compilation:  javac Euclid.java
 *  Execution:    java Euclid p q
 * 
 *  Reads two command-line arguments p and q and computes the greatest
 *  common divisor of p and q using Euclid's algorithm.
 *
 *  Remarks
 *  -----------
 *    - may return the negative of the gcd if p is negative
 *
 *************************************************************************/

public class Euclid {

    // recursive implementation
    public static int gcd(int p, int q) {
        if (q == 0) return p;
        else return gcd(q, p % q);
    }

    // non-recursive implementation
    public static int gcd2(int p, int q) {
        while (q != 0) {
            int temp = q;
            q = p % q;
            p = temp;
        }
        return p;
    }

    public static void main(String[] args) {
        int p = Integer.parseInt(args[0]);
        int q = Integer.parseInt(args[1]);
        int d  = gcd(p, q);
        int d2 = gcd2(p, q);
        System.out.println("gcd(" + p + ", " + q + ") = " + d);
        System.out.println("gcd(" + p + ", " + q + ") = " + d2);
    }
}



Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:05:37 EST 2011.

 

Algorithms Functions Recursion

The idea of calling one function from another immediately suggests the possibility of a function calling itself. The function-call mechanism in Java supports this possibility, which is known as recursion. Recursion is a powerful general-purpose programming technique, and is the key to numerous critically important computational applications, ranging from combinatorial search and sorting methods methods that provide basic support for information processing (Chapter 4) to the Fast Fourier Transform for signal processing (Chapter 9).

Your first recursive program.

The HelloWorld for recursion is to implement the factorial function, which is defined for positive integers N by the equation
N! = N × (N-1) × (N-2) × ... × 2 × 1
N! is easy to compute with a for loop, but an even easier method in Factorial.java is to use the following recursive function:
public static int factorial(int N) { 
   if (N == 1) return 1; 
   return N * factorial(N-1); 
} 
You can persuade yourself that it produces the desired result by noting that factorial() returns 1 = 1! when N is 1 and that if it properly computes the value
(N-1)! = (N-1) × (N-2) × ... × 2 × 1
then it properly computes the value
N! = N × (N-1)! = N × (N-1) × (N-2) × ... × 2 × 1
We can trace this computation in the same way that we trace any sequence of function calls.
factorial(5) 
   factorial(4) 
      factorial(3) 
         factorial(2) 
            factorial(1) 
               return 1 
            return 2*1 = 2 
         return 3*2 = 6 
      return 4*6 = 24 
   return 5*24 = 120
Our factorial() implementation exhibits the two main components that are required for every recursive function.
  • The base case returns a value without making any subsequent recursive calls. It does this for one or more special input values for which the function can be evaluated without recursion. For factorial(), the base case is N = 1.
  • The reduction step is the central part of a recursive function. It relates the function at one (or more) inputs to the function evaluated at one (or more) other inputs. Furthermore, the sequence of parameter values must converge to the base case. For factorial(), the reduction step is N * factorial(N-1) and N decreases by one for each call, so the sequence of parameter values converges to the base case of N = 1.

Mathematical induction.

Recursive programming is directly related to mathematical induction, a technique for proving facts about discrete functions. Proving that a statement involving an integer N is true for infinitely many values of N by mathematical induction involves two steps.
  • The base case is to prove the statement true for some specific value or values of N (usually 0 or 1).
  • The induction step is the central part of the proof. For example, we typically assume that a statement is true for all positive integers less than N, then use that fact to prove it true for N.
Such a proof suffices to show that the statement is true for all N: we can start at the base case, and use our proof to establish that the statement is true for each larger value of N, one by one.

Rabu, 11 Juli 2012

Pengumuman Hasil Olimpiade Sains Provinsi (OSK) 2012 Tingkat SMA

Pengumuman hasil OSP tahun 2012, daftar peserta OSN 2012 dapat kamu download pada link berikut :
  1. Matematika hal 1 dan hal 2
  2. Fisika hal 1 dan hal 2
  3. Kimia hal 1 dan hal 2
  4. Biologi hal 1 dan hal 2
  5. Astronomi hal 1 dan hal 2
  6. Komputer hal 1 dan hal 2
  7. Kebumian hal 1 dan hal 2
  8. Ekonomi hal 1 dan hal 2
Sumber : http://www.pelatihan-osn.com