0
Sigma Notation for Exponential Summations
Working on another piece of code that requires the use of sigma notation. Specifically, how would one represent the following sigma summation in python?: Σ (10^k), k=0 to n
5 Respuestas
+ 4
Just run a loop for k from 0 to n, and for each iteration add (10^k) to some variable sum which was initialized as 0 before loop.
After loop ends, sum will give value of above summation.
Here it will be, 1+10+100+...+100000(n zeroes) = 111...1(n+1 ones)
+ 3
Pythagoras
you are giving wrong value to y in first statement of each iteration, if you are using y in purpose to get summation.
0
I tried:
for i in range(some_num):
y = 10**k
y += y
But it did not return the correct values.
0
Ok, thank you for your help.
0
You could also use the built-in sum function which takes as argument an iterable of numbers:
sum((1.3, 5.5)) # 6.8
sum(range(100+1)) # 5050
sum(10**k for k in range(6)) # 111111
type(10**k for k in range(6)) # <class 'generator'>
or for your special case:
int("1" * 6) # 111111
For strings you could use ''.join(iterable_of_strings):
''.join(map(str.title, ['ba, 'na', 'na'])) # BaNaNa
and for lists you could make your own (works on any type which has + defined for them which also includes ints and strings):
from functools import reduce
reduce(lambda x, y: x + y, ([1, 2], [3, 4, 5], [])) # [1, 2, 3, 4, 5]