Opinions on this implementation of perfect square root finder?
This is a small program I’ve come up with, with the intention of finding the root of a perfect square. I wanted some comments on the performance impact, as well as the use of resources.
square_root.cc #include “square_root.h” static uint64_t power(uint8_t base, uint8_t exp) { uint64_t result = 1; while (exp–) { result *= base; } return result; } static uint64_t seed(uint64_t radicand) { uint64_t a = radicand; uint64_t n = 0; while (radicand /= 100) { a = radicand; ++n; } return ((a < 10) ? ((0.28 * a) + 0.89) : ((0.089 * a) + 2.8)) * power(10, n); } static uint64_t heron(uint64_t x, uint64_t s) { while (s != power(x, 2)) { x = (x + (s / x)) / 2; } return x; } uint16_t square_root(uint64_t radicand) { return heron(seed(radicand), radicand); }
square_root.hc #ifndef SQUARE_ROOT_H #define SQUARE_ROOT_H #include <stdbool.h> #include <stdint.h> #include <stddef.h> uint16_t square_root(uint64_t radicand); #endif
Add comment