In: Computer Science
In Python
Complete the function powersOf5(). Have the function take a parameter n and return a list of the first n powers of 5. One way to calculate powers of 5 is by starting with 1 and then multiplying the previous number by 5.
Examples:
powersOf5( 1 ) returns [1]
powersOf5( 2 ) returns [1, 5]
powersOf5( 3 ) returns [1, 5, 25]
powersOf5( 6 ) returns [1, 5, 25, 125, 625, 3125]
powersOf5( 10 ) returns [1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125]
powerof5.py
def powersOf5( n ):
result = [1]
i = 1
init = 1
while n>1:
result.append(init*5)
i = i+1
n = n-1
init = init*5
print(result)
powersOf5(1)
powersOf5(2)
powersOf5(3)
powersOf5(6)
powersOf5(10)
Output :
[1]
[1, 5]
[1, 5, 25]
[1, 5, 25, 125, 625, 3125]
[1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125]