`{"id":"c245152c-2471-42e7-b5b0-696ca37c3324","name":"Power-logarithmic","description":"1. You are given a number x.\r\n2. You are given another number n.\r\n3. You are required to calculate x raised to the power n. Don't change the signature of power function.\r\n\r\nNote1 -> The previous version expects the call stack to be of n height. This function expects call function to be only log(n) high.\r\n\r\nNote2 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number x\r\nA number n","outputFormat":"x raised to the power n","constraints":"1 &lt;= x &lt;= 10\r\n0 &lt;= n &lt;= 9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nint powerLogarithmic(int x,int n){\r\n // write your code here\r\n \r\n}\r\n\r\nint main(){\r\n int x,n; cin>>x>>n;\r\n cout<<powerLogarithmic(x,n);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static int power(int x, int n){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"import sys\nsys.setrecursionlimit(1500)\n\ndef powerLog(x,n):\n # write your code here\n\nnum = int(input())\npower = int(input())\nprint(powerLog(num,power))"}},"points":10,"difficulty":"easy","sampleInput":"2\r\n5","sampleOutput":"32","questionVideo":"https://www.youtube.com/embed/O84uumjBOMY","hints":[],"associated":[{"id":"04815040-73f9-4a8b-a64a-23b47070fc6d","name":"(Power Logarithmic) Why finding power of a number using logarithmic recursion is more efficient?","slug":"power-logarithmic-why-finding-power-of-a-number-using-logarithmic-recursion-is-more-efficient","type":4},{"id":"a4f8fa53-079e-4d06-9b4b-e35db662db23","name":"(Power Logarithmic) Which function definition is more efficient in finding power of number?","slug":"power-logarithmic-which-function-definition-is-more-efficient-in-finding-power-of-number","type":4},{"id":"ad7d439c-e1f9-42d1-b9bd-a7e35b352b51","name":"(Power Logarithmic) What will be the height of the stack while implementing the recursion ?","slug":"power-logarithmic-what-will-be-the-height-of-the-stack-while-implementing-the-recursion","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"8ed5e886-1ee3-48af-8ec4-580c2fe09da4","name":"Power-logarithmic","slug":"power-logarithmic","type":1}],"next":{"id":"bcbf6880-0490-4ef8-8036-8c3a2d3270ed","name":"Power Function","type":3,"slug":"power-function"},"prev":{"id":"c7f4bf63-b169-4a9b-8281-9d780357f275","name":"Power-linear","type":1,"slug":"power-linear"}}}`

# Power-logarithmic

1. You are given a number x. 2. You are given another number n. 3. You are required to calculate x raised to the power n. Don't change the signature of power function. Note1 -> The previous version expects the call stack to be of n height. This function expects call function to be only log(n) high. Note2 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

`{"id":"c245152c-2471-42e7-b5b0-696ca37c3324","name":"Power-logarithmic","description":"1. You are given a number x.\r\n2. You are given another number n.\r\n3. You are required to calculate x raised to the power n. Don't change the signature of power function.\r\n\r\nNote1 -> The previous version expects the call stack to be of n height. This function expects call function to be only log(n) high.\r\n\r\nNote2 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.","inputFormat":"A number x\r\nA number n","outputFormat":"x raised to the power n","constraints":"1 &lt;= x &lt;= 10\r\n0 &lt;= n &lt;= 9","sampleCode":{"cpp":{"code":"#include<iostream>\r\nusing namespace std;\r\n\r\nint powerLogarithmic(int x,int n){\r\n // write your code here\r\n \r\n}\r\n\r\nint main(){\r\n int x,n; cin>>x>>n;\r\n cout<<powerLogarithmic(x,n);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public static int power(int x, int n){\r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":"import sys\nsys.setrecursionlimit(1500)\n\ndef powerLog(x,n):\n # write your code here\n\nnum = int(input())\npower = int(input())\nprint(powerLog(num,power))"}},"points":10,"difficulty":"easy","sampleInput":"2\r\n5","sampleOutput":"32","questionVideo":"https://www.youtube.com/embed/O84uumjBOMY","hints":[],"associated":[{"id":"04815040-73f9-4a8b-a64a-23b47070fc6d","name":"(Power Logarithmic) Why finding power of a number using logarithmic recursion is more efficient?","slug":"power-logarithmic-why-finding-power-of-a-number-using-logarithmic-recursion-is-more-efficient","type":4},{"id":"a4f8fa53-079e-4d06-9b4b-e35db662db23","name":"(Power Logarithmic) Which function definition is more efficient in finding power of number?","slug":"power-logarithmic-which-function-definition-is-more-efficient-in-finding-power-of-number","type":4},{"id":"ad7d439c-e1f9-42d1-b9bd-a7e35b352b51","name":"(Power Logarithmic) What will be the height of the stack while implementing the recursion ?","slug":"power-logarithmic-what-will-be-the-height-of-the-stack-while-implementing-the-recursion","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"d341a7c9-1269-409c-b851-0bb512289544","name":"Recursion And Backtracking For Beginners","slug":"recursion-and-backtracking-for-beginners","type":0},{"id":"8ed5e886-1ee3-48af-8ec4-580c2fe09da4","name":"Power-logarithmic","slug":"power-logarithmic","type":1}],"next":{"id":"bcbf6880-0490-4ef8-8036-8c3a2d3270ed","name":"Power Function","type":3,"slug":"power-function"},"prev":{"id":"c7f4bf63-b169-4a9b-8281-9d780357f275","name":"Power-linear","type":1,"slug":"power-linear"}}}` Editor

# Power-logarithmic

easy

1. You are given a number x. 2. You are given another number n. 3. You are required to calculate x raised to the power n. Don't change the signature of power function. Note1 -> The previous version expects the call stack to be of n height. This function expects call function to be only log(n) high. Note2 -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you.

## Constraints

1 <= x <= 10 0 <= n <= 9

## Format

### Input

A number x A number n

### Output

x raised to the power n

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}2 5```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}32`

Question Video

Discussions

Show Discussion

Related Resources 