



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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.