Recursion Techniques: Decimal to Binary, Palindromes, and GCD, Study notes of Computer Science

Various recursive techniques, including converting decimal numbers to binary, checking for palindromes, and calculating the greatest common divisor (gcd). Examples and recursive functions for each technique.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-1vx-1
koofers-user-1vx-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Recursion - III
Decimal to Binary
Printing a string in reverse order
Palindrome
GCD of two numbers
1
pf3
pf4
pf5

Partial preview of the text

Download Recursion Techniques: Decimal to Binary, Palindromes, and GCD and more Study notes Computer Science in PDF only on Docsity!

Recursion - III

Decimal to Binary

Printing a string in reverse order

Palindrome

GCD of two numbers

Decimal to Binary representation

Here we keep on dividing the number by 2 recursively till it reduces to zero. Then we print the remainders in reverse order. Example: convert decimal number 5 to its binary representation 5/ 2 = 2 remainder is 1 2/ 2 = 1 remainder is 0 1/ 2 = 0 remainder is 1 We stop here as the number has been reduced to zero and collect the remainders in reverse order. The binary representation is 101. Example: convert decimal number 11 to its binary representation 11/ 2 = 5 remainder is 1 5/ 2 = 2 remainder is 1 2/ 2 = 1 remainder is 0 1/ 2 = 0 remainder is 1 We stop here as the number has been reduced to zero and collect the remainders in reverse order. The binary representation is 1011. Example: convert decimal number 14 to its binary representation 14/ 2 = 1 remainder is 0 7/ 2 = 3 remainder is 1 3/ 2 = 1 remainder is 1 1/ 2 = 0 remainder is 1 We stop here as the number has been reduced to zero and collect the remainders in reverse order. The binary representation is 1110. Here is a recursive function which prints the binary equivalent of any given decimal number. void decibinary ( int num) {

It is a ‘string’ whose first and last characters match AND the remaining substring is also a palindorme. As per this definition the examples above would be termed palindromes if you ignore the spaces between the words. anutforajaroftuna The recursive definition can be easily coded into a computer code segment. It needs the length of the string to be supplied along with the substring itself. Every time the function is being called, it makes sure that the first and last characters match and then calls itself recursively after dropping the first and last characters from the substring. nutforajaroftun utforajaroftu tforajaroft int checkPalindrome (char string[]) { if ( check (string, strlen (string) )) printf("\nyes, it is a palindrome\n"); else printf("\nNo, it is not a palindrome"); } Function check returns 1 if the remaining characters in the array str form a palindrome. int check ( char str[], int len) { if ( len <= 1) return 1 ; else { // if first and last characters match // and the remaining string is a palindrome if ( (str[0] == str[len-1]) && check(str+1, len-2) ) return 1; else return 0; } }

Note that array arithmetic is being used here. Since the first character of the string is located at memory location str , the new substring begins at memory location str+1 and the length of the new substring is 2 less than the current string length. The Greatest common Divisor (GCD) The GCD of Two non-negative integers is the largest integer that divides evenly into both. For example, numbers 2, 3, 4, 6 and 12 divide evenly into both the numbers 24 and 36. So GCD of 24 and 36 is 12, the largest integer. The GCD can be obtained by factorizing the numbers and picking up all the common elements. However for very large numbers it would involve large number of operations. For example consider obtaining GCD of 129618 and 576234 by factorizing. In 3rd^ century B.C., the great mathematician Euclid had proposed an algorithm to find GCD. It was based on the facts that GCD(p, q) = GCD(q, p), and GCD( p, 0) = p Euclid’s algorithm:

**1. Make p the larger of two numbers p and q.

  1. Divide p by q. Let r be the remainder. If r is zero, then q is the gcd.
  2. Else, gcd of p and q equals gcd of q and r.** Using this algorithm we can find the gcd of 1296 and 576 as follows; gcd(1296,576) = gcd ( 576, 1296 % 576) = gcd (576, 144) = 144 as 144 divides 576 without leaving a remainder. Write a recursive function int GCD(int p, int q) using the Euclid’s algorithm.