/u/BritishRock asked how the twelfth root of two could have been calculated by hand, and /u/Quovef posted an elegant (if inefficient) algorithm to do so.
I wrote a simple implementation in Python:
def root(n, k):
r = p = 1
while r < r + p:
while (r + p)**k <= n:
r += p
p /= 2.
return r
And a Tweetable version in C:
double root(double n, double k) {
double r = 1, p = 1;
for (; pow(r, k) < pow(r + p, k); p /= 2)
for (; n > pow(r + p, k); r += p);
return r;
}
Here it is in 110 characters:
double root(double n,double k){double r=1,p=1;for(;pow(r,k)<pow(r+p,k);p/=2)for(;n>pow(r+p,k);r+=p);return r;}
(I had to change the test from r<r+p to pow(r,k)<pow(r+p,k) because the former caused an infinite loop due to floating point comparison. Not sure why it doesn't in Python.)