220k views
0 votes
3. Write a function named sum_of_squares_until that takes one integer as its argument. I will call the argument no_more_than. The function will add up the squares of consecutive integers starting with 1 and stopping when adding one more would make the total go over the no_more_than number provided. The function will RETURN the sum of the squares of those first n integers so long as that sum is less than the limit given by the argument

1 Answer

3 votes

Answer:

Following are the program to this question:

def sum_of_squares(no_more_than):#defining a method sum_of_squares that accepts a variable

i = 1#defining integer variable

t = 0#defining integer variable

while(t+i**2 <= no_more_than):#defining a while loop that checks t+i square value less than equal to parameter value

t= t+ i**2#use t variable to add value

i += 1#increment the value of i by 1

return t#return t variable value

print(sum_of_squares(12))#defining print method to call sum_of_squares method and print its return value

Output:

5

Step-by-step explanation:

In the program code, a method "sum_of_squares" is declared, which accepts an integer variable "no_more_than" in its parameter, inside the method, two integer variable "i and t" are declared, in which "i" hold a value 1, and "t" hold a value that is 0.

In the next step, a while loop has defined, that square and add integer value and check its value less than equal to the parameter value, in the loop it checks the value and returns t variable value.

User BrettAHale
by
5.4k points