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
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters,...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters, each greater or equal to zero, and evaluates to their sum. In this problem, you must use the built-in functions "add1" and "sub1" and may not use the built-in functions "+" or "-". For example, (sum 2 3) should evaluate to 5. Note: (add1 5) evaluates to 6 and (sub1 4) evaluates to 3. Hint: like you saw in the "append" lecture, treat one...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
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.Write a function which takes in a dictionary are returns the sum of the keys plus...
1.Write a function which takes in a dictionary are returns the sum of the keys plus the sum of the values, but only if all the keys and all the values are integers. Otherwise it returns False. >>>f({'a':1,'b':4,'c':7,'d':11}) False >>>f({1:2,3:4,5:6,7:8}) 36 2.Write a function to quickly compute the recaman sequence. 3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1,...
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,...
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT