An alternating sum is a sum in which the signs of the terms alternate between po
ID: 3816517 • Letter: A
Question
An alternating sum is a sum in which the signs of the terms alternate between positive and negative values. For example an alternating sum of the first 8 positive integers isl-2 + 3- 4 + 5- 6 + 7- 8 = -4, and alternating sum of the squares of the first 5 positive integers would be 1^2 - 2^2 + 3^2 - 4^2 + 5^2 = 15. Write the code for an integer-valued function altSumSquares(N), where N is an integer. This function should return an integer value that represents the alternating sum of the integers between 1 and N inclusive. Name your file for this code Labl12-AltSumSq-yourIn.py. YOU MAY NOT USE A FORMULA TO CALCULATE THIS VALUE, BUT MUST USE A LOOP!! Once the code for altSumSquares has been entered and there are no syntax errors, use the following main () program to test your code. If you run main for two code runs with N= 4 and N = 5, you should get -10 for a return value for N = 5 and you should get 15 for a return value when N = 5. def main(): codeRuns = eval(input("How many times do you want to call altSumSquares? ")) for value in range(codeRuns): N = eval(input("Enter a positive integer: ")) returnValue = altSumSquares(N) print("The alternating sum of the squares of the first", N, "integers is", returnValue) #end for #end main main()Explanation / Answer
#save file as Lab12-AltSumSq-yourIntials.py
def main():
#prompt to enter number of times the run loop executes
codeRuns=eval(input("How many times do you want to call altSumSquares ?"))
for value in range(codeRuns):
N=eval(input("Enter a positive integer"))
#calling altSumSquares
returnValue=altSumSquares(N)
print("The alternating sum of the squares of the first",N,
"integers is ",returnValue)
#end for
#The method altSumSquares that takes N as argument
#and finds the alternative sum of squares and
#returns the result
def altSumSquares(N):
#set sum=0
sum=0
#find a range in 1 to N+1
arr=range(1,N+1)
for value in arr:
if value%2==1:
#for odd number,add square tosum
sum+=(value*value)
else:
#for even number,add square tosum
sum-=(value*value)
#return sum
return sum
#end main
main()
------------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
>>>
How many times do you want to call altSumSquares ?5
Enter a positive integer5
The alternating sum of the squares of the first 5 integers is 15
Enter a positive integer4
The alternating sum of the squares of the first 4 integers is -10
Enter a positive integer3
The alternating sum of the squares of the first 3 integers is 6
Enter a positive integer2
The alternating sum of the squares of the first 2 integers is -3
Enter a positive integer1
The alternating sum of the squares of the first 1 integers is 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.