Exercise 757

Given a non-zero natural integer n, write a python algorithm as function which determines the remainder in the Euclidean division of the sum of the first n integers by n.

Solution

# create a function calculating the sum of the first n numbers 1 + 2 + 3 + ... + n
def sumNumbers(n):
# initializing the sum of first n numbers
sumNb = 0
for i in range(0 , n + 1):
sumNb = sumNb + i
return sumNb
def divEuclid(n):
return sumNumbers(n)%n

# Testing algorithm
print("The remainder in the Euclidean division of the sum of the first 4 integers by 4 is : ", divEuclid(4))

Younes Derfoufi
my-courses.net

Leave a Reply