1. ALGORITMA BILANGAN PRIMA
Metode Pertama
int m = 30; // batas
boolean [] prima = new boolean [m+1];
for ( int i = 0; i < = m; i++ )
prima [i] = true;
prima [0] = prima [1] = false;
double akarN = Math.sqrt (m);
for (int i = 2; i < = akarN; i++ )
if ( prima [i] )
for ( int j = i*i; j < = m; j++ )
if ( (j%i) == 0 )
prima [j] = false;
for (int i = 0; i < = m; i++ )
if ( prima [i] && ( i > = n ) )
System.out.println (i + "\n");
Metode kedua
Dim i, min, max, j As integer
Dim hsl As String = " "
Dim val As Boolean = True
For i = mint to max
val = True
For j = 2 to i - 1
If i Mod j = 0 Then
val = False
Exit for
end If
next
If val = True And i <> 0 And i <> 1 And i <> 2 Then
hsl = hsl + " " + i.ToString
End If
next
End Sub
2. ALGORITMA MENCARI TEXT
Metode Pertama
Dim x (input m, n : integer, input P : array[1..m] of char, input T : array[1..n] of char, output idx : integer)
s, j : integer
ketemu : boolean
For i = 1 to panjang text = 16
x ( i ) = Sub STR ( Text, i, 1 )
If x ( i ) = "SIKILE"
Next ( 1 ) = "S"
( 2 ) = "T"
( 3 ) = "M"
( 4 ) = "I"
( 5 ) = "K"
( 6 ) = " "
( 7 ) = "B"
( 8 ) = "A"
( 9 ) = "N"
( 10 ) = "I"
( 11 ) = " "
( 12 ) = "S"
( 13 ) = "A"
( 14 ) = "L"
( 15 ) = "E"
( 16 ) = "H"
s←0
ketemu←false
while (s ≤ n-m) and (not ketemu) do
j←1
while (j ≤ m) and (P[j] = T[s+j]) do
j←j+1
endwhile
{ j > m or P[j] ≠ T[s+j] }
if j = m then { kecocokan string ditemukan }
ketemu←true
else
s←s+1 { geser pattern satu karakter ke kanan teks}
endif
endfor
{ s > n – m or ketemu }
if ketemu then
idx←s+1 { catatan: jika indeks array dimulai dari 0,
idx ← s }
else
idx←-1
endif
Metode kedua
S T M I K B A N I S A L E H
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Jadi Untuk Membuat Text “SIKILE" Algoritma nya adalah....
- Start
- Read F
- STMIK BANI SALEH = true
- for i:=1 to 14 ( sqrt(F) ) do
- if (F mod i == 1,4,5,9,12,13)
- STMIK BANI SALEH = false
- break
- end if
- end for
- return STMIK BANI SALEH
- finish
Algoritma diatas Untuk Membuat Text “SIKILE” dari text STMIK BANI SALEH
0 komentar:
Posting Komentar