{"id":20706,"date":"2021-09-21T09:00:12","date_gmt":"2021-09-21T03:30:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=20706"},"modified":"2021-11-22T18:36:16","modified_gmt":"2021-11-22T13:06:16","slug":"python-program-to-find-sum-of-modulo-k-of-first-n-natural-numbers","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-modulo-k-of-first-n-natural-numbers\/","title":{"rendered":"Python Program to Find Sum of Modulo K of First N Natural Numbers"},"content":{"rendered":"

In the previous article, we have discussed Python Program for Modular Multiplicative Inverse<\/a><\/p>\n

Given two numbers N and K, the task is to find the sum of Modulo K of the first N natural numbers in Python.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 5\r\nGiven k value = 6<\/pre>\n

Output:<\/strong><\/p>\n

The sum of modulo k of the given n natural numbers =  15<\/pre>\n

Explanation:<\/strong><\/p>\n

here 1%6+2%6+3%6+4%6+5%6 gives 15<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 4\r\nGiven k value =  7<\/pre>\n

Output:<\/strong><\/p>\n

The sum of modulo k of the given n natural numbers =  10<\/pre>\n

Program to Find Sum of Modulo K of First N Natural Numbers in Python<\/h2>\n

Below are the ways to find the sum of Modulo K of the first N natural numbers in Python.<\/p>\n