Problem 1: Prompt the user to enter a whole number for the power used in the las
ID: 3871909 • Letter: P
Question
Problem 1: Prompt the user to enter a whole number for the power used in the last element of the series (n) and a floating point number for x. The user should be able to enter a positive or negative number for n. When the user enters a positive number for n the series
1 + x + x2 + x3 + x4 ………. + xn
should be calculated and when the user enters a negative number for n the series
1 + 1/x + 1/x2 + 1/ x3 + 1/x4 ………. + 1/xn
should be calculated. Output the sum of the series. Do not use the pow function or create your own function for exponentiation.
PLEASE DO NOT POST THE SAME CODE AS THE HW BELOW!
we have NOT learned loops. please do not use any loops.
ONLY use if/else statements or if/else if statements
http://www.chegg.com/homework-help/questions-and-answers/use-c-use-concepts-beyond-chapter-5-textbook-put-arithmetic-expressions-cout-statements-pr-q23975751
Explanation / Answer
def posSeries(N, x, i, prevTerm) :
if (i > N) :
return 0;
currentTerm = (prevTerm * x);
if (i == 1) :
currentTerm = 1;
return currentTerm + posSeries(N, x, i+1, currentTerm);
def negSeries(N, x, i, prevTerm) :
if (i > N) :
return 0;
currentTerm = (prevTerm/x);
if (i == 1) :
currentTerm = 1;
return currentTerm + negSeries(N, x, i+1, currentTerm);
xValue = float(raw_input("Enter a value for x : "));
posNum = int(raw_input("Enter a positive number : "));
negNum = int(raw_input("Enter a negative number : "));
sumOfSeries = posSeries(posNum, xValue, 1, 0) + negSeries(abs(negNum), xValue, 1, 0);
print("Sum of series is ", sumOfSeries);