I come from a scientific background, so my approach to the solution of
this problem is a little different.
It makes use of some numerical approximations, but that's not
necessarily a bad thing, because it helps avoid singularities. So it
could be a little more robust than other solutions presented here.
It also has a little more functionality: Say you wanted to return
three times a variable, you wouldnt want to write another function to
do that for you, so now you just pass in how many times you want the
variable repeated as the first parameter.
Hope this helps! Cheers!
import math
def repeat(how_many_times, x):
def f(n):
return 1./(2**n)
def summation(func, howmany):
if howmany == 1:
return func(1)
else:
return func(howmany) + summation(func, howmany-1)
def eulerify(num):
return abs(math.cos(math.pi) + 1j*(math.sin(math.pi))) * num
def get_coefficient(multiplier):
return eulerify(multiplier * summation(f, 100))
return int(eulerify(get_coefficient(how_many_times) * x))