Sunday, May 4, 2008

Extended Euclid Method (Java)


Method untuk aplikasi algoritma Euclid yaitu mencari GCD (Great Common Divisor)
atau FPB (Faktor Persekutuan Terbesar)pada dua bilangan integer

Kemudian Mencari kombinasi linear antara GCD, bilangan1 dan bilangan2
dengan menggunakan algoritma extended euclid

private int[] generateEuclid(int first, int second){
if (first < second){
int swapper = first;
first = second;
second = swapper;
}

if (second == 0){
TAoutput.append("\nGCD : "+first+"\n\n"+"Extended Euclid\n\n");
return new int[] { first, 1, 0 };
}

TAoutput.append(first+" = "+first/second+"."+second+" + "+first%second
+"\n");
int[] prev = generateEuclid(second, first % second);
int gcd = prev[0];
int a = prev[1] - (prev[2]*(first/second)) ;
int b = prev[2];

int swap = a;a=b;b=swap;
swap=first;first=second;second=swap;

TAoutput.append(gcd+" = "+b+"."+first+" + "+a+"."+second+"\n");

return new int[] { gcd, a, b };

}

Saturday, April 19, 2008

Loop for Shapes [1] (Pascal)

Request dari Jonny Irawan
untuk permasalahan bentuk-bentuk menggunakan perulangan
dikombinasikan dengan deretan angka
Semoga dapat membantu...

output :
1 1 1 1 1
3 3 3 3
5 5 5
7 7
9
7 7
5 5 5
3 3 3 3
1 1 1 1 1

Code :

uses crt;
VAR
loop1, loop2 : byte;
number : byte;
temp : byte;
BEGIN
clrscr;
for loop1:=1 to 9 do
BEGIN
temp:=abs(5-loop1)+1;
for loop2:=1 to temp do
BEGIN
write(2*(6-temp)-1);
END;
writeln;
END;
readkey;
END.


Thursday, February 28, 2008

Hexadecimal to Binary Converter (Pascal)


program meng-konversikan bilangan heksadesimal (basis 16)
menjadi bilangan biner (basis 2)

uses crt;

var

hexa : string;{variable for storing hexa input}

biner : string;{variable for storing binary output}

desimal : integer;{temporary variable for decimal conversion}

index,conv,sub : integer;{variable for looping and subtractor}



BEGIN

clrscr;{clearing the screen}

{ask for hexa input and read the input}

write('Input on Hexa : ');readln(hexa);

{primary loop converter hexa to binary}

for index:=1 to length(hexa) do

begin

{case of hexa written on numeric character}

if (ord(hexa[index])>=ord('0')) and (ord(hexa[index])<=ord('9')) then begin {ascii code of hexa - ascii code of '0'} desimal:=ord(hexa[index])-ord('0'); end else {case of hexa written on uppercase letter} if (ord(hexa[index])>=ord('A')) and (ord(hexa[index])<=ord('F')) then begin {ascii code of hexa - ascii code of 'A'+10} {A represent 10 on decimal} desimal:=ord(hexa[index])-ord('A')+10; end else {case of hexa written on lowercase letter} if (ord(hexa[index])>=ord('a')) and (ord(hexa[index])<=ord('f')) then begin {ascii code of hexa - ascii code of 'a'} desimal:=ord(hexa[index])-ord('a')+10; end else {case of the input is wrong} begin writeln('INPUTAN SALAH!!'); readln; {terminate the program immediately} exit; end; writeln('BINARY'); {the subtractor start on 8 (2^3)} sub:=8; for conv:=3 downto 0 do begin {the decimal bigger than subtractor} if (desimal>=sub) then

begin

write('1');

{subtract the decimal with subtractor}

desimal:=desimal-sub;

end

else

begin

{write 0 and do nothing}

write('0');

end;

{decrement the power of subtractor by 1}

sub:=sub div 2;

end;

{just for separating output per character}

write(' ');

end;

readln;

END.

Monday, February 18, 2008

Selection Sort (C)


program untuk sorting data menggunakan algoritma seleksi
atau sering disebut selection sort

#include "stdio.h"
#include "conio.h"

main()
{

int data[10] = {22,2,5,7,9,0,32,34,37,56},x;

for (int index=0;index<=9;index++)
{
printf(
"%3d",data[index]);
}

for (int index=0;index<9;index++)
{
int x=index;
for(int index2=index+1;index2<=9;index2++)
{
if(data[x]>data[index2])
{
x=index2;
}
}
int trans=data[index];
data[index]=data[x];
data[x]=trans;
}

printf(
"\n\n");

for (int index=0;index<=9;index++)
{
printf(
"%3d",data[index]);
}

getch();
}

Tower of Hanoi (C)


program untuk menyelesaikan permasalahan tower of hanoi
dimana keping pada tiang pertama harus dipindah ke tiang ketiga
dengan bantuan satu tiang bantu

dengan syarat keping yang lebih besar tidak boleh berada di atas
keping yang lebih kecil

#include "stdio.h"
#include "conio.h"

void pindah(int jumlah, char asal, char bantu, char tujuan)
{
if (jumlah==0)
{
return;
}
else
{
pindah(jumlah-1,asal,tujuan,bantu);
printf("%c KE %c\n",asal,tujuan);
pindah(jumlah-1,bantu,asal,tujuan);
}
}

main()
{
int jumlah;
printf("Masukkan jumlah piringan : ");
scanf("%d",&jumlah);

pindah(jumlah,'A','B','C');

getch();
}