{"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 Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of Modulo K of the first N natural numbers in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Practice Python Program Examples<\/a> to master coding skills and learn the fundamental concepts in the dynamic programming language Python.<\/p>\n In the previous article, we have discussed Python Program for Modular Multiplicative Inverse Given two numbers N and K, the task is to find the sum of Modulo K of the first N natural numbers in Python. Examples: Example1: Input: Given Number = 5 Given k value = 6 Output: The sum of modulo k …<\/p>\nGiven Number = 5\r\nGiven k value = 6<\/pre>\n
The sum of modulo k of the given n natural numbers = 15<\/pre>\n
here 1%6+2%6+3%6+4%6+5%6 gives 15<\/pre>\n
Given Number = 4\r\nGiven k value = 7<\/pre>\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
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say Sum_mod_k which takes the given number and k value as the\r\n# arguments and returns the sum of modulo K of the given first N natural numbers.\r\n\r\n\r\ndef Sum_mod_k(gvn_numb, gvn_Kval):\r\n # Take a variable to say rslt and initialize its value to 0.\r\n rslt = 0\r\n # Loop from 1 to the given number using the for loop.\r\n for itr in range(1, gvn_numb + 1):\r\n # Calculate the value of the iterator modulus given k value and store it in a variable.\r\n mod_rslt = (itr % gvn_Kval)\r\n # Add the above result to the rslt and store it in the same variable rslt.\r\n rslt += mod_rslt\r\n # Return the value of rslt.\r\n return rslt\r\n\r\n\r\n# Give the number as static input and store it in a variable.\r\ngvn_numb = 5\r\n# Give the k value as static input and store it in another variable.\r\ngvn_Kval = 6\r\n# Pass the given number and k value as the arguments to the Sum_mod_k function.\r\n# Print the sum of modulo K of\u00a0the given first N natural numbers.\r\nprint(\"The sum of modulo k of the given n natural numbers = \",\r\n Sum_mod_k(gvn_numb, gvn_Kval))\r\n<\/pre>\n
The sum of modulo k of the given n natural numbers = 15<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say Sum_mod_k which takes the given number and k value as the\r\n# arguments and returns the sum of modulo K of the given first N natural numbers.\r\n\r\n\r\ndef Sum_mod_k(gvn_numb, gvn_Kval):\r\n # Take a variable to say rslt and initialize its value to 0.\r\n rslt = 0\r\n # Loop from 1 to the given number using the for loop.\r\n for itr in range(1, gvn_numb + 1):\r\n # Calculate the value of the iterator modulus given k value and store it in a variable.\r\n mod_rslt = (itr % gvn_Kval)\r\n # Add the above result to the rslt and store it in the same variable rslt.\r\n rslt += mod_rslt\r\n # Return the value of rslt.\r\n return rslt\r\n\r\n\r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Give the k value as user input using the int(input()) function and store it in another variable.\r\ngvn_Kval = int(input(\"Enter some random number = \"))\r\n# Pass the given number and k value as the arguments to the Sum_mod_k function.\r\n# Print the sum of modulo K of\u00a0the given first N natural numbers.\r\nprint(\"The sum of modulo k of the given n natural numbers = \",\r\n Sum_mod_k(gvn_numb, gvn_Kval))\r\n<\/pre>\n
Enter some random number = 4\r\nEnter some random number = 7\r\nThe sum of modulo k of the given n natural numbers = 10<\/pre>\n
\n