Skip to main content

Greatest Common Divisor (Eucledian)

Greatest Common Divisor (GCD for short) is the largest positive integer that divides two numbers. In other words, the greatest common divisor of a and b is, as its name suggests, the largest positive integer d such that d | a and d | b. In this post, we will see how we can calculate GCD efficiently using an algorithm that is known as Eucledian Algorithm.

The key thing in this algorithm is division with remainder, which is simply the method of "long division" that you learned in elementary school. Thus if a and b are positive integers and if you attempt to divide a by b, you will get a quotient q and a reminder r.

Below is the theorem used for this algorithm:
Let a and b be positive integers with a >= b. The following algorithm computes gcd(a,b) in a finite number of steps.
(1) Let r0 = a and r1 = b
(2) Set i = 1
(3) Divide ri-1 by ri to get a quotient qi and remainder ri+1, ri-1 = ri.qi + ri+1 with 0 <= ri+1 <= ri
(4) If the remainder ri+1 = 0, then ri=gcd(a,b) and the algorithm terminates.
(5) Otherwise, ri+1 > 0, so set i = i + 1 and go to Step 3.
Below is the sample source code on how it is implemented using the C programming language

int gcd ( int number1 , int number2 )
{
    if ( number2 == 0 )
        return number1 ;
    else
        return gcd ( number2 , number1 % number2 ) ;
}

Hope it helps anyone who needs it. Happy Programming :)

Comments

Popular posts from this blog

Blue Light Filtering with f.lux

Light, one of the very important parts of human living support, has tremendous benefit for us. Light itself is composed of many different rays. One of the components of light, called as blue light, has been known to tamper with our circadian rhythm.

Blue light itself can come from a lot of sources, including the lamp inside your room. However, the source which gives the most impact on people are from electronic devices. In this technology era, it's very hard for us to run away from using electronic devices. Those devices, such as Laptops and Handphones, have tremendously improve our quality of life by making our life easier and more fun through a lot of applications and systems available. Unfortunately, blue light emitted through the screen of those devices can affect our quality of sleep. Some research even advised us not to use our phone a few hours before we are going to sleep to improve our sleep quality.

For tech-savvy people, getting away from devices, especially handphone, …

Kisah Nyata Seorang Anak Bernama Andoy dari Filipina

This post is in Bahasa.
Ada seorang anak kecil kelas 4 SD yang selalu mengucap syukur dalam keadaan apapun. Ia tinggal di suatu desa di Negara Filipina. Setiap hari untuk sampai ke sekolahnya ia harus berjalan kaki melintasi daerah yang tanahnya berbatu dan menyeberangi jalan raya yang berbahaya dimana banyak kendaraan yang melaju kencang. Setiap kali berhasil menyeberangi jalan raya tersebut, Andoy selalu mampir sebentar ke Gereja untuk berdoa. Tindakannya ini diamati oleh Pdt. Agaton. Karena merasa terharu dengan sikap Andoy yang lugu dan beriman tersebut. Suatu hari ketika Andoy hendak masuk ke Gereja Pdt. Agaton menyapanya.
Bpk. Pdt : "Selamat pagi Andoy, apa kabarmu? Apakah kamu akan ke sekolah?"
Andoy : "Ya, Bapa Pendeta!" balas Andoy sambil tersenyum.
Bpk.Pdt : "Mulai sekarang saya akan membantu dan menemani kamu menyeberangi jalan raya tersebut setiap kali kamu akan menyeberang.
Andoy : Terima kasih, Bapa Pendeta."
Bpk. Pdt : "sekarang apa yang aka…

Needlefish Attack

Just recently, a news spread about a young lady, died due to attack of a Needle fish. The story began when she was having a vacation with her family at Tanjung Karang beach, Makassar, Indonesia. This unfortunate event happened on January 01, 2016.



Liena (39) was soaking her body near the shoreline, whilst suddenly a needlefish jump towards her face. She was not able to dodge because it was too sudden. The fish, known with its long sharp and spear like snout, pierced through her right eyelids. She was then screaming for help. Her husband's family was then run to help her. She was quite badly injured. The wound was so deep that it even pierced through the skull and caused her to vomit blood.

She was brought to the hospital by ambulance to Kabelota Donggala hospital. Nevertheless, the wound was so bad that she eventually could not make it. She passed away in that hospital and the corpse was brought back to Balaikota Palu.



It's highly advised that we are keep being careful when playi…