leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 74934eb304f3205b3eb61f4e970fc70e9b6383e2
parent 0f8510ead88cab82038d1ab8cba3997a508370eb
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon, 26 May 2025 18:31:00 -0600

2894. divisible and non-divisible sums difference

Diffstat:
Adiv_non_div_sum_diff.c | 19+++++++++++++++++++
Adiv_non_div_sum_diff.py | 25+++++++++++++++++++++++++
2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/div_non_div_sum_diff.c b/div_non_div_sum_diff.c @@ -0,0 +1,19 @@ +// 2894. Divisible and Non-divisible Sums Difference +#include "leetcode.h" + +/* + * you are given a positive integers 'n' and 'm'. define two integers as + * follows: 'num1': the sum of all integers in the range '[1, n]' that are not + * divisible by 'm'. 'num2': the sum of all integers in the range '[1, n]' that + * are divisible by 'm'. return the integer 'num1 - num2' + */ + +int differenceOfSums(int n, int m) { + return (1 + n) * n / 2 - (1 + n / m) * (n / m) * m; +} + +int main() { + printf("%d\n", differenceOfSums(10, 3)); // expect: 19 + printf("%d\n", differenceOfSums(5, 6)); // expect: 15 + printf("%d\n", differenceOfSums(5, 1)); // expect: -15 +} diff --git a/div_non_div_sum_diff.py b/div_non_div_sum_diff.py @@ -0,0 +1,25 @@ +# 2894. Divisible and Non-divisible Sums Difference + +""" +you are given a positive integers 'n' and 'm'. define two integers as +follows: 'num1': the sum of all integers in the range '[1, n]' that are not +divisible by 'm'. 'num2': the sum of all integers in the range '[1, n]' that +are divisible by 'm'. return the integer 'num1 - num2' +""" + + +class Solution(object): + def differenceOfSums(self, n, m): + """ + :type n: int + :type m: int + :rtype: int + """ + return (1 + n) * n // 2 - (1 + n // m) * (n // m) * m + + +if __name__ == "__main__": + obj = Solution() + print(obj.differenceOfSums(n=10, m=3)) + print(obj.differenceOfSums(n=5, m=6)) + print(obj.differenceOfSums(n=5, m=1))