#include <stdio.h>
#include <math.h>
#include <stdint.h>
#ifndef DBL_EPSILON
#define DBL_EPSILON 2.2204460492503131e-16
#endif
float power(float a, float b)
{
float res = 1;
for (int i = 0; i < b; ++i)
{
res *= a;
}
return res;
}
double root(int n, double x)
{
double d, r = 1;
if (!x)
{
return 0;
}
if (n < 1 || (x < 0 && !(n&1)))
{
return 0.0 / 0.0;
}
do
{
d = (x / power(r, n - 1) - r) / n;
r += d;
} while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
return r;
}
long gcd(long a, long b)
{
return b == 0 ? a : gcd(b, a % b);
}
void frac(float value, long* numerator, long* denominator)
{
double integral
= floor(value
); double frac = value - integral;
const long precision = 1000000;
long commonDenominator = gcd(round(frac * precision), precision);
*numerator = round(frac * precision) / commonDenominator;
*denominator = precision / commonDenominator;
*numerator = *numerator + (integral * *denominator);
}
int main() {
float base = 2;
//OR
//A ^ (B/C) is the same as CthRoot(A ^ B)
long num = 0;
long den = 0;
printf("SECOND: %f\n", root
(den
, power
(base
, num
)));
base = 3;
//OR:
num = 0;
den = 0;
printf("FOURTH: %f\n", root
(den
, power
(base
, num
)));
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KCiNpZm5kZWYgREJMX0VQU0lMT04KI2RlZmluZSBEQkxfRVBTSUxPTiAyLjIyMDQ0NjA0OTI1MDMxMzFlLTE2CiNlbmRpZgoKZmxvYXQgcG93ZXIoZmxvYXQgYSwgZmxvYXQgYikKewogICAgZmxvYXQgcmVzID0gMTsKICAgIAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBiOyArK2kpCiAgICB7CiAgICAgICAgcmVzICo9IGE7CiAgICB9CiAgICAKICAgIHJldHVybiByZXM7Cn0KCmRvdWJsZSByb290KGludCBuLCBkb3VibGUgeCkKewogICAgZG91YmxlIGQsIHIgPSAxOwogICAgaWYgKCF4KQogICAgewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgCiAgICBpZiAobiA8IDEgfHwgKHggPCAwICYmICEobiYxKSkpCiAgICB7CiAgICAgICAgcmV0dXJuIDAuMCAvIDAuMDsKICAgIH0KICAgIAogICAgZG8KICAgIHsKICAgICAgICBkID0gKHggLyBwb3dlcihyLCBuIC0gMSkgLSByKSAvIG47CiAgICAgICAgciArPSBkOwogICAgfSB3aGlsZSAoZCA+PSBEQkxfRVBTSUxPTiAqIDEwIHx8IGQgPD0gLURCTF9FUFNJTE9OICogMTApOwogICAgCiAgICByZXR1cm4gcjsKfQoKCmxvbmcgZ2NkKGxvbmcgYSwgbG9uZyBiKQp7CiAgICByZXR1cm4gYiA9PSAwID8gYSA6IGdjZChiLCBhICUgYik7Cn0KCnZvaWQgZnJhYyhmbG9hdCB2YWx1ZSwgbG9uZyogbnVtZXJhdG9yLCBsb25nKiBkZW5vbWluYXRvcikKewogICAgZG91YmxlIGludGVncmFsID0gZmxvb3IodmFsdWUpOwogICAgZG91YmxlIGZyYWMgPSB2YWx1ZSAtIGludGVncmFsOwogICAgY29uc3QgbG9uZyBwcmVjaXNpb24gPSAxMDAwMDAwOwogICAgCiAgICBsb25nIGNvbW1vbkRlbm9taW5hdG9yID0gZ2NkKHJvdW5kKGZyYWMgKiBwcmVjaXNpb24pLCBwcmVjaXNpb24pOwogICAgKm51bWVyYXRvciA9IHJvdW5kKGZyYWMgKiBwcmVjaXNpb24pIC8gY29tbW9uRGVub21pbmF0b3I7CiAgICAqZGVub21pbmF0b3IgPSBwcmVjaXNpb24gLyBjb21tb25EZW5vbWluYXRvcjsKICAgIAogICAgKm51bWVyYXRvciA9ICpudW1lcmF0b3IgKyAoaW50ZWdyYWwgKiAqZGVub21pbmF0b3IpOwp9CgppbnQgbWFpbigpIHsKICAgIAogICAgZmxvYXQgYmFzZSA9IDI7CiAgICBmbG9hdCBleHAgPSAyLjU7CiAgICAKICAgIAogICAgcHJpbnRmKCJGSVJTVDogJWZcbiIsIHBvdyhiYXNlLCBleHApKTsKICAgIAogICAgLy9PUgogICAgCiAgICAvL0EgXiAoQi9DKSBpcyB0aGUgc2FtZSBhcyBDdGhSb290KEEgXiBCKQogICAgbG9uZyBudW0gPSAwOwogICAgbG9uZyBkZW4gPSAwOwogICAgZnJhYyhleHAsICZudW0sICZkZW4pOwogICAgCiAgICBwcmludGYoIlNFQ09ORDogJWZcbiIsIHJvb3QoZGVuLCBwb3dlcihiYXNlLCBudW0pKSk7CiAgICAKICAgIAogICAgYmFzZSA9IDM7CiAgICBleHAgPSAyLjc7CiAgICAKICAgIHByaW50ZigiVEhJUkQ6ICVmXG4iLCBwb3coYmFzZSwgZXhwKSk7CiAgICAKICAgIC8vT1I6CiAgICAKICAgIG51bSA9IDA7CiAgICBkZW4gPSAwOwogICAgZnJhYyhleHAsICZudW0sICZkZW4pOwogICAgCiAgICBwcmludGYoIkZPVVJUSDogJWZcbiIsIHJvb3QoZGVuLCBwb3dlcihiYXNlLCBudW0pKSk7CiAgICAKICAgIHJldHVybiAwOwp9