evinda
Gold Member
MHB
- 3,741
- 0
I like Serena said:Yep. (Nod)
We could calculate $a$ directly with $a=\sqrtn$.
Or we could calculate the upper bound for $a$ by using the smallest value of $b$ giving us $\sqrt n$.
But those are difficult to calculate with an integer algorithm.
So instead we should probably pick $n$ as upper bound so that we need $\log_2 n$ bisections. (Thinking)
I have thought of the following algorithm:
Code:
Algorithm:
k=-1;
b=2;
while ((b<=logn) and (k=-1)){
k=binarysearch(b,n);
b=b+1;
}
if (k!=-1) printf("n=%d^%d",k,b);
Code:
int binarysearch(int b, int n){
int low=2;
int high=n;
while (low<high){
int mid=low+floor((high-low)/2);
if (mid^b==n) return mid;
else if (mid^b<n) low=mid+1;
else high=mid-1;
}
return -1;
}
So will the number of arithmetic operations be greater as desired? (Thinking)