Lesson 9 of 24

RecursionRecursion

10 min 30 XP

Recursion: function தன்னைத்தானே சிறிய input-உடன் அழைக்கும். Base case நிறுத்தும். Tower of Hanoi recursion-ன் புகழ்பெற்ற உதாரணம்: n disks-க்கு 2ⁿ - 1 moves.

In recursion a function calls itself on a smaller input until a base case stops it. The Tower of Hanoi is the classic example: n disks need 2ⁿ - 1 moves.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+30 XP

long long fastPower(long long base, int exp) என்ற recursive function எழுதுங்கள்: exp இரட்டை எண் என்றால் half = fastPower(base, exp/2); return half * half. main()-ல் base, exp படித்து விடையை print செய்யுங்கள். Input: 2 40 → Output: 1099511627776

Write a recursive long long fastPower(long long base, int exp): when exp is even compute half = fastPower(base, exp / 2) and return half * half. In main(), read base and exp and print the result. Input: 2 40 → Output: 1099511627776

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.