Program to Find Sum of Modulo K of First N Natural Numbers

Python Program to Find Sum of Modulo K of First N Natural Numbers

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 of the given n natural numbers =  15

Explanation:

here 1%6+2%6+3%6+4%6+5%6 gives 15

Example2:

Input:

Given Number = 4
Given k value =  7

Output:

The sum of modulo k of the given n natural numbers =  10

Program to Find Sum of Modulo K of First N Natural Numbers in Python

Below are the ways to find the sum of Modulo K of the first N natural numbers in Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Give the k value as static input and store it in another variable.
  • Pass the given number and k value as the arguments to the Sum_mod_k function.
  • Create a function to say Sum_mod_k which takes the given number and k value as the arguments and returns the sum of modulo K of the given first N natural numbers.
  • Take a variable to say rslt and initialize its value to 0.
  • Loop from 1 to the given number using the for loop.
  • Calculate the value of the iterator modulus given k value and store it in a variable.
  • Add the above result to the rslt and store it in the same variable rslt.
  • Return the value of rslt.
  • Print the sum of modulo K of the given first N natural numbers.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Sum_mod_k which takes the given number and k value as the
# arguments and returns the sum of modulo K of the given first N natural numbers.


def Sum_mod_k(gvn_numb, gvn_Kval):
    # Take a variable to say rslt and initialize its value to 0.
    rslt = 0
    # Loop from 1 to the given number using the for loop.
    for itr in range(1, gvn_numb + 1):
      # Calculate the value of the iterator modulus given k value and store it in a variable.
        mod_rslt = (itr % gvn_Kval)
      # Add the above result to the rslt and store it in the same variable rslt.
        rslt += mod_rslt
   # Return the value of rslt.
    return rslt


# Give the number as static input and store it in a variable.
gvn_numb = 5
# Give the k value as static input and store it in another variable.
gvn_Kval = 6
# Pass the given number and k value as the arguments to the Sum_mod_k function.
# Print the sum of modulo K of the given first N natural numbers.
print("The sum of modulo k of the given n natural numbers = ",
      Sum_mod_k(gvn_numb, gvn_Kval))

Output:

The sum of modulo k of the given n natural numbers =  15

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Give the k value as user input using the int(input()) function and store it in another variable.
  • Pass the given number and k value as the arguments to the Sum_mod_k function.
  • Create a function to say Sum_mod_k which takes the given number and k value as the arguments and returns the sum of modulo K of the given first N natural numbers.
  • Take a variable to say rslt and initialize its value to 0.
  • Loop from 1 to the given number using the for loop.
  • Calculate the value of the iterator modulus given k value and store it in a variable.
  • Add the above result to the rslt and store it in the same variable rslt.
  • Return the value of rslt.
  • Print the sum of modulo K of the given first N natural numbers.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Sum_mod_k which takes the given number and k value as the
# arguments and returns the sum of modulo K of the given first N natural numbers.


def Sum_mod_k(gvn_numb, gvn_Kval):
    # Take a variable to say rslt and initialize its value to 0.
    rslt = 0
    # Loop from 1 to the given number using the for loop.
    for itr in range(1, gvn_numb + 1):
      # Calculate the value of the iterator modulus given k value and store it in a variable.
        mod_rslt = (itr % gvn_Kval)
      # Add the above result to the rslt and store it in the same variable rslt.
        rslt += mod_rslt
   # Return the value of rslt.
    return rslt


# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Give the k value as user input using the int(input()) function and store it in another variable.
gvn_Kval = int(input("Enter some random number = "))
# Pass the given number and k value as the arguments to the Sum_mod_k function.
# Print the sum of modulo K of the given first N natural numbers.
print("The sum of modulo k of the given n natural numbers = ",
      Sum_mod_k(gvn_numb, gvn_Kval))

Output:

Enter some random number = 4
Enter some random number = 7
The sum of modulo k of the given n natural numbers = 10

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.