For18. A real number A and an integer N (>0) are given. Using one cycle, find the value of the expression
\(1 - A + A^2 - A^3 + ... + (-1)^N \cdot A^N.\)
Do not use a conditional statement.

Solution in Python 3:

import random

N = random.randrange(1,10)
print('N = ', N)
A = random.randrange(-10,10)
print('A = ', A)

P = 1.0
S = 1.0
for i in range(1,N+1):
P *= A * (-1)
S += P
print(i," : ", P," : ", S)
print("Result:",S)