From 2f3aeed02b0dda08397bcd421c66f4ac058f38f3 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 11 Aug 2021 19:42:54 +0530 Subject: C: Difference of Squares: Use pow from math.h --- c/difference-of-squares/src/difference_of_squares.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'c') 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 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) { -- cgit 1.4.1-2-gfad0