The definition of prime numbers is nothing but the natural numbers greater than 1 and can only be divided by the number 1 and number itself. Examples of some prime numbers, among others, 2, 3, 5, 7, 11, 13 ...
Well, here I will share how to create Java applications to search for prime numbers recursively.
jeliot.io impor .*;
public class Perdana {
swasta statis int NilaiRekursif (nomor int, int index) {
if (index == 1)
return 1;
else if (angka% index == 0)
return 1 + NilaiRekursif (nomor, - index);
lain
return 0 + NilaiRekursif (nomor, - index);
}
public boolean cekBilanganPrima statis (int num) {
if (num> 1)
kembali (NilaiRekursiff (num, num) == 2);
lain
return false;
}
public static void main (String [] args) {
int num = 30;
if (cekBilanganPrima (num))
System.out.println ("Nomor Perdana");
lain
System.out.println ("Bukan Bilangan Perdana");
}
}
0 comments:
Posting Komentar