Comments on: Divide & Conquer Approach to find a^n https://demo.ritambhara.in/divide-conquer-approach-to-find-an/ Coding / System Design Interviews Mon, 02 Jan 2023 14:39:36 +0000 hourly 1 https://wordpress.org/?v=6.8.1 By: Vipin https://demo.ritambhara.in/divide-conquer-approach-to-find-an/#comment-7049 Mon, 02 Jan 2023 14:39:36 +0000 http://www.ritambhara.in/?p=401#comment-7049 In reply to Yash.

yes, It’s one(1)

]]>
By: Kathan Parekh https://demo.ritambhara.in/divide-conquer-approach-to-find-an/#comment-6792 Tue, 23 Aug 2022 16:32:40 +0000 http://www.ritambhara.in/?p=401#comment-6792 you had written wrong code of divide and conquer approach of exponentiation

Correct Code:-

#include
long int power(int x,int n){
if(n==0){
return 1;
}
else if(n%2==0){
return power(x,n/2)*power(x,n/2);
}
else{
return x*power(x,(n-1)/2)*power(x,(n-1)/2);
}
}
int main(){
int x,y;
printf(“Enter Number:- “);
scanf(“%d”,&x);
printf(“Enter Power:- “);
scanf(“%d”,&y);
printf(“%d^%d=%d”,x,y,power(x,y));
return 0;
}

]]>
By: rohitx https://demo.ritambhara.in/divide-conquer-approach-to-find-an/#comment-4516 Mon, 03 Aug 2020 18:58:13 +0000 http://www.ritambhara.in/?p=401#comment-4516 Thanks a lot.

]]>
By: Yash https://demo.ritambhara.in/divide-conquer-approach-to-find-an/#comment-1739 Sat, 12 Dec 2015 17:28:26 +0000 http://www.ritambhara.in/?p=401#comment-1739 1 more error. You need to put an exit condition. I put n=n/2 in my code and used the condition while (n>1), so that once value of n becomes less than 1. The recursion stops.

]]>
By: Yash https://demo.ritambhara.in/divide-conquer-approach-to-find-an/#comment-1738 Sat, 12 Dec 2015 16:01:23 +0000 http://www.ritambhara.in/?p=401#comment-1738 Thanks.This post was really helpful. Especially the divide and conquer approach.
I would like to point out one error in the divide and conquer code.
I believe it should be-
if (n==0)
return 1;

]]>