diff options
author | Andinus <andinus@nand.sh> | 2021-08-11 19:42:54 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-08-11 19:42:54 +0530 |
commit | 2f3aeed02b0dda08397bcd421c66f4ac058f38f3 (patch) | |
tree | 8f470438ffe7fc9abb07e4b5c86713462f7fb3b2 /c | |
parent | 321825828ac918bad28d0597a8616c6dc9802c3c (diff) | |
download | exercism-2f3aeed02b0dda08397bcd421c66f4ac058f38f3.tar.gz |
C: Difference of Squares: Use pow from math.h
Diffstat (limited to 'c')
-rw-r--r-- | c/difference-of-squares/src/difference_of_squares.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/c/difference-of-squares/src/difference_of_squares.c b/c/difference-of-squares/src/difference_of_squares.c index 0d9b6b2..f59e92c 100644 --- a/c/difference-of-squares/src/difference_of_squares.c +++ b/c/difference-of-squares/src/difference_of_squares.c @@ -1,17 +1,18 @@ #include "difference_of_squares.h" +#include <math.h> unsigned int sum_of_squares(unsigned int number) { unsigned int sum = 0; - for (unsigned int idx = 1; idx <= number; idx++) - sum += (idx * idx); + for (; number > 0; number--) + sum += pow(number, 2); return sum; } unsigned int square_of_sum(unsigned int number) { unsigned int sum = 0; - for (unsigned int idx = 1; idx <= number; idx++) - sum += idx; - return sum * sum; + for (; number > 0; number--) + sum += number; + return pow(sum, 2); } unsigned int difference_of_squares(unsigned int number) { |