From c7912e7b1be729088aa11a5aeefb308b1db4a46b Mon Sep 17 00:00:00 2001 From: chestamittal <53469607+chestamittal@users.noreply.github.com> Date: Tue, 20 Oct 2020 03:03:12 +0530 Subject: [PATCH] Update math/lcm_sum.cpp Co-authored-by: David Leal --- math/lcm_sum.cpp | 57 ++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/math/lcm_sum.cpp b/math/lcm_sum.cpp index b29de72de..ee341bd2b 100644 --- a/math/lcm_sum.cpp +++ b/math/lcm_sum.cpp @@ -13,41 +13,46 @@ #include /// for std::vector /** - * Function to compute sum of euler totients in sumOfEulerTotient vector - * @param num input number - * @returns int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num + * @namespace math + * @brief Mathematical algorithms */ -int lcmSum(int num) { +namespace math { + /** + * Function to compute sum of euler totients in sumOfEulerTotient vector + * @param num input number + * @returns int Sum of LCMs, i.e. ∑LCM(i, num) from i = 1 to num + */ + int lcmSum(int num) { - int i=0, j=0; - int limit = 1000; - std::vector eulerTotient(limit); - std::vector sumOfEulerTotient(limit); + int i=0, j=0; + int limit = 1000; + std::vector eulerTotient(limit); + std::vector sumOfEulerTotient(limit); - // storing initial values in eulerTotient vector - for(i=1; i<=limit; i++) { - eulerTotient[i] = i; - } + // storing initial values in eulerTotient vector + for(i=1; i<=limit; i++) { + eulerTotient[i] = i; + } - // applying totient sieve - for(i=2; i<=limit; i++) { - if(eulerTotient[i] == i) { - for(j=i; j<=limit; j+=i) { - eulerTotient[j] = eulerTotient[j]/i; - eulerTotient[j] = eulerTotient[j]*(i-1); + // applying totient sieve + for(i=2; i<=limit; i++) { + if(eulerTotient[i] == i) { + for(j=i; j<=limit; j+=i) { + eulerTotient[j] = eulerTotient[j]/i; + eulerTotient[j] = eulerTotient[j]*(i-1); + } } } - } - // computing sum of euler totients - for(i=1; i<=limit; i++) { - for(j=i; j <=limit; j+=i) { - sumOfEulerTotient[j] += eulerTotient[i]*i; + // computing sum of euler totients + for(i=1; i<=limit; i++) { + for(j=i; j <=limit; j+=i) { + sumOfEulerTotient[j] += eulerTotient[i]*i; + } } + + return ((sumOfEulerTotient[num] + 1 ) * num) / 2; } - - return ((sumOfEulerTotient[num] + 1 ) * num) / 2; -} } // namespace math /**