Recursion Through Basic Problems
Mon Apr 01 2024
Binary Search:
Implemented a recursive binary search to find a target in a sorted array. By repeatedly dividing the search interval in half, the function narrows down the search area efficiently, demonstrating recursion's elegance in algorithmic search problems.
Factorial Calculation:
Crafted a recursive function to compute the factorial of a number. This fundamental example showcases recursion's natural fit for problems defined by a function calling itself with a gradually reduced problem size.
Fibonacci Sequence:
Developed a recursive solution to generate Fibonacci numbers. This classic problem illustrates recursion's ability to solve complex computations through simple, repetitive function calls.
Array Multiplication:
Devised a recursive method to multiply all elements in an array. This approach highlights how recursion can simplify iterating through data structures, reducing complex loops to straightforward recursive steps.
Palindrome Checker:
Constructed a recursive function to check if a number is a palindrome. By comparing digits and recursively shrinking the problem space, this method emphasizes recursion's utility in breaking down problems into more manageable sub-problems.
Range of Numbers:
Formulated a recursive function to create an array containing a range of numbers. This problem demonstrates recursion's capability to build solutions incrementally, showcasing its versatility in data construction tasks.
1const arr = [2, 4, 5, 7, 9, 12]
2
3function findMe(target, start, end){
4    if(start>end){
5        return "Not Found"
6    }
7
8    const middle = Math.floor((start+end)/2)
9
10    if(arr[middle]==target){
11        return `Found it at index ${middle}`
12    }
13    
14    if(arr[middle]>target){
15        return findMe(target, start, middle-1)
16    }
17
18    if(arr[middle]<target){
19        return findMe(target,  middle+1, end)
20    }
21}
22
23console.log(findMe(7, 0, 5))
24
25
26 
27    factorial(num){
28        console.log("num ===>",num)
29        if (num === 1)
30        { 
31            return 1
32        }
33        else
34        return num*this.factorial(num-1)
35    }
36
37function fibnonacci(n) {
38  if (n <= 1) return n;
39  return fibnonacci(n - 1) + fibnonacci(n - 2);
40}
41console.log(fibnonacci(6));
42console.log(fibnonacci(9));
43
44  multiplyRecurssion(arr){
45        console.log("arr ===>",arr)
46        if (arr.length === 0)
47        { 
48            return 1
49        }
50        else
51        return arr[arr.length-1]*this.multiplyRecurssion(arr.slice(0, -1))
52    }
53
54 isPalindromeRecursive(num) {
55    if (num < 0) return false;
56
57    const digitCount = Math.floor(Math.log10(num)) + 1;
58    return this.isPalindromeHelper(num, digitCount);
59  }
60
61  isPalindromeHelper(num, digitCount) {
62    if (num === 0) return true;
63    if (digitCount <= 1) return true;
64
65    const firstDigit = Math.floor(num / Math.pow(10, digitCount - 1));
66    const lastDigit = num % 10;
67
68    if (firstDigit !== lastDigit) return false;
69
70    num = Math.floor((num % Math.pow(10, digitCount - 1)) / 10);
71    digitCount -= 2;
72
73    return this.isPalindromeHelper(num, digitCount);
74  }
75
76rangeofnum(startNum, endNum) {
77        console.log("num ===>",startNum,endNum)
78        if (endNum < startNum)
79        { 
80            return []
81        }
82        else{
83            const numbers = this.rangeofnum(startNum, endNum-1);
84            numbers.push(endNum)
85            return numbers;
86        }
87       
88    }
89
90
91