initial commit
This commit is contained in:
44
c/square-root/src/square_root.c
Normal file
44
c/square-root/src/square_root.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "square_root.h"
|
||||
|
||||
/* See GNUmakefile in following link for explanation
|
||||
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
|
||||
*/
|
||||
#if defined UNIT_TEST || defined DEBUG
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
/* Newton's method:
|
||||
* https://en.wikipedia.org/wiki/Newton%27s_method#Square_root
|
||||
* return the largest integer equal or less than i square root.
|
||||
*/
|
||||
unsigned square_root(unsigned i)
|
||||
{
|
||||
unsigned sq, sq2;
|
||||
|
||||
if (i<=1) /* 0 and 1 */
|
||||
return i;
|
||||
|
||||
sq = i/2; /* we take i/2 as initial seed */
|
||||
sq2 = ((i/sq) + sq) / 2; /* first iteration */
|
||||
|
||||
while (sq2 != sq) {
|
||||
sq = sq2;
|
||||
sq2 = ((i/sq) + sq) /2; /* next iteration */
|
||||
}
|
||||
|
||||
return sq;
|
||||
}
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int arg=1;
|
||||
unsigned i;
|
||||
|
||||
for (; arg<ac; ++arg) {
|
||||
i=atol(av[arg]);
|
||||
printf("sqrt(%u)=%u\n", i, square_root(i));
|
||||
}
|
||||
}
|
||||
#endif
|
16
c/square-root/src/square_root.h
Normal file
16
c/square-root/src/square_root.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef SQUARE_ROOT_H
|
||||
#define SQUARE_ROOT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern unsigned square_root(unsigned);
|
||||
|
||||
/* See GNUmakefile in following link for explanation
|
||||
* https://exercism.io/my/solutions/103b2f7d92db42309c1988030f5202c7
|
||||
*/
|
||||
#ifdef TESTALL
|
||||
#undef TEST_IGNORE
|
||||
#define TEST_IGNORE() {}
|
||||
#endif
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user