exponential sum using recursion.python - python

I need to write a function using recursion that accepts the following variables:
n: int
x: real
and returns the exponential sum function:
I can't use loops at all, only recursion.
I started by writing two recursive functions for sum and factorial but when I tried to combine the two of them I got the error message:
TypeError: 'int' object is not iterable
I don't quite understand what it means because I didn't use loops, only recursion. Here is my code:
def sum_exp(n):
if n == 0:
return 0
return n + sum(n - 1)
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n-1)
def exp_n_x(n, x):
if n == 0:
return 1
else:
return sum_exp(x*(1/factorial(n)))
print(exp_n_x(10, 10))
I would love to get some help with this. Thanks a lot in advance.

you've made a typo in:
def sum_exp(n):
if n == 0:
return 0
return n + sum(n - 1)
^^^
where you use the global function sum() which takes an iterable and returns the sum of its elements. Thus:
>>> sum(42)
TypeError: 'int' object is not iterable
whereas
>>> sum([42])
42
so your fix is straightforward:
def sum_exp(n):
if n == 0:
return 0
return n + sum_exp(n - 1)
N.B.: This answer is only meant to fix your issue with the TypeError, and get you going to the next error you'll hit, which is an infinite recursion. As an advice you should double check your stop condition and recursion step.

Related

Make fibo faster [duplicate]

This question already has answers here:
Efficient calculation of Fibonacci series
(33 answers)
Closed 4 years ago.
I need to write a code to give a number and print me the F[number].This code is pretty slow.Any ideas for a faster code?
while True:
n=input()
if n=='END' or n=='end':
break
class Fibonacci:
def fibo(self, n):
if int(n) == 0:
return 0
elif int(n) == 1:
return 1
else:
return self.fibo(int(n)-1) + self.fibo(int(n)-2)
f=Fibonacci()
print(f.fibo(n))
I have written a bit about faster fibonacci in this post, maybe one of them is useful for you? https://sloperium.github.io/calculating-the-last-digits-of-large-fibonacci-numbers.html
Anyway. Your code is very slow because you get exponential running time calling the same subtrees over and over.
You can try a linear solution:
def fib3(n):
if n == 0:
return 0
f1 = 0
f2 = 1
for i in range(n-1):
f1,f2 = f2, f1+f2
return f2
You can use functools memoize to make it store previous values so it doesn't have to recursively call the fibonacci function. The example they list is literally fibonacci
You can use a dict to memoize the function:
class Fibonacci:
memo = {}
def fibo(self, n):
if n in self.memo:
return self.memo[n]
if int(n) == 0:
value = 0
elif int(n) == 1:
value = 1
else:
value = self.fibo(int(n) - 1) + self.fibo(int(n) - 2)
self.memo[n] = value
return value
Use dynamic programming: this prevents it calculating all the way down to 0 and 1 each time:
memory = {0:0, 1:1}
def fibo(n):
if n in memory:
return memory[n]
else:
ans = fibo(int(n)-1) + fibo(int(n)-2)
memory[n] = ans
return ans
Test:
>>> fibo(1000)
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
This is almost instantaneous.
Don't use a class; you're not gaining anything from it
Don't needlessly redefine your class each loop
Convert from str to int once, up front, rather than over and over
(If not required by assignment) Use iterative solution, not recursive
With just #1-3, you'd end up with:
def fibo(n): # Using plain function, defined once outside loop
if n < 2:
return n
return fib(n - 1) + fibo(n - 2)
while True:
n = input()
if n.lower() == 'end':
break
print(fibo(int(n))) # Convert to int only once
If you're not required to use a recursive solution, don't; Fibonacci generation is actually a pretty terrible use for recursion, so redefine the function to:
def fibo(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
which performs O(n) work rather than O(2**n) work. Memoizing could speed up the recursive solution (by decorating fibo with #functools.lru_cache(maxsize=None)), but it's hardly worth the trouble when the iterative solution is just so much faster and requires no cache in the first place.

Summing the elements of a list recursively

def sum(L):
if len(L) == 1:
return L[0]
i = sum (len (L) // 2)
if len(L) > 1:
return i + i
L=[2,4]
print (sum(L))
when i try to run it there is a TypeError: object of type 'int' has no len().
In sum (len (L) // 2), you're passing an integer (the result of len(L) // 2) as the L argument to your sum() function. (Please don't give functions the same name as built-in functions.) The recursively-called sum() then tries to evaluate len(L) == 1 on this integer, but integers don't support len(), and so you get the error message in question. What exactly are you actually trying to do?
I think what you were aiming for was to write a recursive sum function that continuously splits the list into smaller chunks. So basically what you need to do is compute the index of the midpoint, then use list slicing to pass the first sublist and second sublist recursively back into the function, until hitting your base case(s) of 0 or 1 elements remaining.
def add(values):
if len(values) == 0:
return 0
elif len(values) == 1:
return values[0]
mid = len(values)//2
return add(values[:mid]) + add(values[mid:])
>>> add([1,2,3,4,5])
15
Don't name your function sum, it shadows the builtin function
Implement your function so as to clearly define a base case and a recursive case.
For the base case, when the length is 1, return that element. You've got this right.
For the recursive case, split your list into half and recursively compute the sum for each half.
def sum_recursive(L):
if len(L) == 1:
return L[0]
idx = len(L) // 2
return sum_recursive(L[:idx]) + sum_recursive(L[idx:])
sum_recursive must always receive a list and return an integer.
Some dry runs:
In [5]: sum_recursive([1, 2, 4, 8])
Out[5]: 15
In [6]: sum_recursive([2, 4])
Out[6]: 6
Keep in mind that this won't be able to handle empty lists as input. If you want to account for that as well, change your base case to:
def sum_recursive(L):
if len(L) <= 1:
return sum(L)
...
We're making use of the builtin sum function here which handles empty lists gracefully by returning 0. For single-element lists, the first element is returned (this is also why it is important you don't shadow these utility functions).
If you don't want to use sum there, you'll need to split your base case into two parts:
def sum_recursive(L):
if len(L) == 0:
return 0
elif len(L) == 1:
return L[0]
...
In your code
i = sum (len (L) // 2)
line is throwing an error because in recursion after the first call of sum() you are passing an integer not list
def sum(L):
if len(L) == 0:
return 0
elif len(L) == 1:
return L[0]
else:
index = len(L)-1
return L[index] + sum(L[0:index])
L=[2,4]
print (sum(L))

recursive function to sum of first odd numbers python

I'm trying to convert below code to recursive function but seems i'm quite confusing how could i write below in recursive function. could help me to give some thoughts?
Basically, what I'm generating below is the sum of the first n odd numbers.
def sum_odd_n(n):
total=0
j=2*n-1
i=1
if i>j:
return 1
else:
total =((j+1)/2)**2
i+=2
return total
> >>> sum_odd_n(5)
> 25.0
> >>> sum_odd_n(4)
> 16.0
> >>> sum_odd_n(1)
> 1.0
This smells somewhat like homework so I'm going to offer some advice instead of a solution.
Recursion is about expressing a problem in terms of itself.
Suppose you know the sum of the odd numbers from N to N - 2.
Can you write the total sum in terms of this sum and the function itself (or a related helper function)?
Recursive functions have at least one base case and at least one recursive call. Here are some hints:
def f(n):
# Base case - for which
# n do we already know the answer
# and can return it without
# more function calls? (Clearly,
# this must also terminate any
# recursive sequence.)
if n == ???:
return ???
# Otherwise, lets say we know the answer
# to f(n - 1) and assign it to
# the variable, 'rest'
rest = f(n - 1)
# What do we need to do with 'rest'
# to return the complete result
return rest + ???
Fill out the question marks and you'll have the answer.
Try:
def sum_of_odd(n):
if n>0:
x=(n*2)-1
return x+sum_of_odd(n-1)
else:
return 0
The answer of this:
sum_of_odd(5)
will be:
25
Try :
def sum_odd_n(n):
if n>0:
if n==1:
return 1
else:
return 2*n-1 + sum_odd_n(n-1)

"Float object is not iterable"?

I've written a function to find the standard deviation of a list:
def avg(L):
return sum(L)/len(L)
def stdDev(L):
for i in range(len(L)-1):
return sqrt((sum(L[i]-avg(L)))**2)/len(L))
If I run this and give the Shell the input [20,10,30] it says "float object is not iterable". Where is the issue, and can I solve it without settling for integer results of either of these functions?
sum(L[i]-avg(L)) - here you have a sum of float.
You also have return statement every pass of the loop, that's clearly wrong.
Formula for standard deviation also is not right, as you misplaced the braces.
What I believe you wanted:
def stdDev(L):
s = 0
for i in range(len(L)):
s += (L[i] - avg(L))**2
return sqrt(s/len(L))
Better to iterate through elements than their indices:
for x in L:
s += (x - avg(L))**2
or
def stdDev(L):
ave = avg(L)
s = sum((x - ave)**2 for x in L)
return sqrt(s/len(L))

Recursion depth error in simple Python program

I am new to programming, and was trying to solve this problem on Project Euler using basic Python.
Essentially, I tried to use recursion based on the largest value chosen at every stage, and using a list to maintain possible options for future choices.
The code is short and is given below:
def func(n,l):
if n<0:
return 0
if l==[1] or n==0:
return 1
else:
j=0
while l != []:
j=j+func(n-l[0],l)
del l[0]
return j
print func(200,[200,100,50,20,10,5,2,1])
For instance, if we have
func(5,[5,2,1])
the recursion splits it into
func(0,[5,2,1]) + func(3,[2,1]) + func(4,[1])
But the code never seems to go through. Either it says that there is a list-index-out-of-range error, or a maximum-recursion-depth error (even for very small toy instances). I am unable to find the mistake. Any help will be much appreciated.
In Python lists are passed into functions by reference, but not by value. The simplest fix for your program is changing recursive call to func(n - l[0], l[:]). In this way list will be passed by value.
One thing you're failing to take into account is that the following:
j=j+func(n-l[0],l)
doesn't make a copy of l.
Therefore all recursive invocations of func operate on the same list. When the innermost invocation deletes the last element of l and returns, its caller will attempt to del l[0] and will get an IndexError.
At each recursion, make the following 2 decisions:
Take the first coin (say f) from available coin types, then check if we can made (n-f) from those coins. This results in a sub-problem func(n - f, l)
Ignore the first coin type, and check if we can make n from the remaining coin types. This results in a sub-problem func(n, l[1:])
The total number of combinations should be the sum of the two sub-problems. So the code goes:
def func(n, l):
if n == 0:
return 1
if n < 0 or len(l) == 0:
return 0
if l == [1] or n == 0:
return 1
return func(n - l[0], l) + func(n, l[1:])
Each recursion a copy of l is made by l[1:]. This can be omitted by pop element before next recursion and restore with append afterwards.
def func(n, l):
if n == 0:
return 1
if n < 0 or len(l) == 0:
return 0
if l == [1] or n == 0:
return 1
full = func(n - l[-1], l)
last = l.pop()
partial = func(n, l)
l.append(last)
return full + partial

Categories

Resources