Question

Python Question: Write a function which returns the sum of squares of the integers 1 to...

Python Question:

Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0

You may not use the built-in Python sum function.

The Solution Must be recursive

>>> sum_of_squares(1)
1
>>> sum_of_squares(4)
30
>>> sum_of_squares(8) # 64 + 49 + 36 + ... + 4 + 1
17650828

Homework Answers

Answer #1

test.py


# Tester program for the Plan-Composition Study Problems

from sumofsquares import sumOfSquares
print( sumOfSquares( 4 ) )

sumofsquares.py


# Sum of Squares

"""
Design a program sumOfSquares that consumes an integer n and produces
the sum of the squares of all numbers from 1 through n.
You may assume that n is at least 1.

Example: sumOfSquares(4) yields 30
"""

def sumOfSquares( n ) :

   # Initialize helper variables
   sum_of_squares = 0

   # Go through all integers from 1 until n
   for x in range( 1, ( n + 1 ) ) :
      
       # Add x^2 to the running sum
       sum_of_squares += ( x ** 2 )

   return sum_of_squares

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...
1. Type a 'sumade2en2' function that receives an n number and returns the sum of the...
1. Type a 'sumade2en2' function that receives an n number and returns the sum of the numbers from 1 and increments of 2 without going over n. For example, if the function is invoked with 7, it must return 16 (1+3+5+7). Yes invoked with the 8 must return 16 (1+3+5+7). 2. Type a 'productmultiply' function that receives two integers a and b. The function must return the product of all multiples of a that do not exceed b. For example,...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...