Rekrusif java example

karena saya sangat penasaran ma namanya rekrusif di java ya jadinya gw coba-coba dech…:D

1.

public class Rerekrusif {

    public static int jumlah(int b, int n){
    	if(n == 1){
    		return 5;
    	}else{
    		return 5 + (n-1)*b+jumlah(b,n-1);
    		}
    	}
    public static void main(String[] args){
    	System.out.println("jumlah b and n = " + jumlah(5,1));
    	}
}

2.

//semakin kedalam semakin penasaran ma ne rekrusif...definisi dah ok
//tinggal kasus...kasus...
public class rekrusiff2 {

	public static int testing(int n){
		if(n == 1){
			return 1;
		}else{
			return n*n+testing(n-1);
			}
	}
		public static void main(String[] args){
			System.out.println("hasil nilai fungsi n = " + testing(3));
			}
}

3. Fibonanci rekrusif method.

//fibonnanci pake rekrusif booo...bukan pke iterasi
//walaupun iterasi jauh lebih cepat dalam pencarian..tapi iseng aja..dech..monggo

public class FibonanciRekrusif {
    public static int Fibob(int m){
    	if(m < 2) {
    		return 1;
    	}else{
			return	Fibob(m-1) + Fibob(m-2);
    		}
    	}
    public static void main(String[] args){
		int a, b;
		a = 10;
		b = Fibob(a);
		System.out.println("fibob angka " + a + "fibonanncinya"  + b +".");
    }

}

perbedaan paling mendasar dari iterasi sendiri ya delay waktu hitungnya sendiri dengan sistem prosessor yang kita punya..

* program diatas kelihatanya kurang bener karena menurut perhitungan manual fibo(10) = 55 tapi disana f(10) = 89
untuk itu silahkan dibetulkan sendiri…hehehehe…sorry guys..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s