📜  门| GATE CS Mock 2018 |设置 2 |问题 15(1)

📅  最后修改于: 2023-12-03 15:28:39.441000             🧑  作者: Mango

GATE CS Mock 2018 - Set 2 - Question 15

This question is a programming question from the GATE CS Mock Exam 2018, Set 2, and the code is written in Python. The problem statement is as follows:

Problem Statement

Consider the following function:

def sum_mod(m):
    total = 0
    for i in range(m):
        total += i*i
    return total%m

The value returned by the function sum_mod(50) is _____.

Solution

The given Python function sum_mod(m) computes the sum of squares of numbers from 0 to m-1, and returns the remainder when this sum is divided by m. We have to compute the value of sum_mod(50) using this function.

To compute sum_mod(50), we need to call the function with the argument m=50. The function will then compute the sum of squares of the numbers from 0 to 49, and return the remainder when this sum is divided by 50.

sum_mod(50) = (0^2 + 1^2 + 2^2 + ... + 49^2) % 50
            = (0 + 1 + 4 + 9 + ... + 2401) % 50

We can compute the sum of squares using the formula:

1^2 + 2^2 + ... + n^2 = n*(n+1)*(2n+1)/6

Here, n=49, so:

0^2 + 1^2 + 2^2 + ... + 49^2 = 49*50*99/6 = 40425

Therefore,

sum_mod(50) = 40425 % 50 = 25

Therefore, the value returned by the function sum_mod(50) is 25.

Code
def sum_mod(m):
    total = 0
    for i in range(m):
        total += i*i
    return total%m

print(sum_mod(50)) # output: 25