Lesson 9 of 24

Recursion

10 min 30 XP

Recursion मध्ये function base case थांबवेपर्यंत लहान input वर स्वतःलाच call करते. Tower of Hanoi हे क्लासिक उदाहरण: n disks साठी 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

Recursive long long fastPower(long long base, int exp) लिहा: exp सम असल्यास half = fastPower(base, exp / 2) काढा आणि half * half परत द्या. main() मध्ये base आणि exp वाचा आणि निकाल print करा. Input: 2 40 → Output: 1099511627776

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