Understanding recursion with the Fibonacci Series - python

I am trying to better understand recursion and how return statements work. As such, I'm looking at a piece of code meant to identify the fibonacci number associated with a given term -- in this case, 4. I'm have difficulty understanding the else statement.
def f(n):
if n == 0:
return 0
if n == 1:
return 1
else:
return f(n-1) + f(n-2)
f(4)
I have tried using Visualize Python to examine what happens at each step, but I get lost when it hits the else statement.
It looks like it is taking the value of n and subtracting 1, to create a new n value of 3 which it returns to the function definition. So it appears to only be returning the value from the first function in the else statement. However, the else statement is written to return the sum of 2 functions f(n-1) + f(n-2), in which case I thought the return value would be 5? Can you even add 2 functions together?
Thanks in advance for your help.
Here is a link to the code in Visualize Python Sum of 2 functions

When in doubt, just break it down.
The tree flow is actually counter-intuitive to the actual control flow, but once you understand the sequence of calls, it becomes clearer. The thing to understand here is that you keep breaking down a larger computation to a sum of smaller computations, and you stop when you hit the base case (the if statements). Now you can carry out all the small computations, and combining the results of those small computations to form a bigger, larger result, until you have your final answer.
Every time a recursive call hits the base case, it will return either 1, or 0, depending on what case was hit. This value will be returned to the previous caller. To understand, consider:
f(1)3 + f(0)3
Note here that the subscript represents the depth of the recursion call tree. The call is made by f(2)2. f(1)3 is computed first, and 1 is returned to f(2)2. f(0)3 is then computed, and 0 is returned to f(2)2. The two return values are summed, and the result is 1.
f(2)2 then returns 1 to whoever called it, which in this case happens to be f(3)1. f(3)1 called f(2)2 + f(1)2, meanwhile this other f(1)2 also returns 1 to f(3)1, who now adds that with the result of f(2)2, to form 2.
f(3)1 now passes 2 to f(4)0, its caller, which happened to call f(3)1 + f(2)1 ... and so it goes.
An alternative way of looking at this is to start from the first function call that is actually made: f(4)0.
f(4)0 computes f(3)1 + f(2)1. But to compute f(3)1, it needs to know f(2)2 + f(1)2, and similarly, to compute f(2)1, it needs to know f(1)2 + f(0)2, and so on.

Adding some print statements can also help clarifying the sequence:
def f(n):
print("Number received:", n)
if n == 0:
return 0
if n == 1:
return 1
else:
print("---- First recursion ----")
a = f(n-1)
print("---- Second recursion ----")
b = f(n-2)
print(" a=:",a,"; b=",b,"; returning:", a+b)
return a + b
print("Final f(4)=", f(4))
Output:
Number received: 4
---- First recursion ----
Number received: 3
---- First recursion ----
Number received: 2
---- First recursion ----
Number received: 1
---- Second recursion ----
Number received: 0
a=: 1 ; b= 0 ; returning: 1
---- Second recursion ----
Number received: 1
a=: 1 ; b= 1 ; returning: 2
---- Second recursion ----
Number received: 2
---- First recursion ----
Number received: 1
---- Second recursion ----
Number received: 0
a=: 1 ; b= 0 ; returning: 1
a=: 2 ; b= 1 ; returning: 3
Final f(4)= 3

Related

How to count number of times we can halve a number until it reaches 1?

I want to have a number that halves until it reaches 1, then it should return a count of how many times it halved.
example:
halve(4)
2
halve(11)
3
since 4/2 = 2 and 2/2= 1, hence it halved twice before reaching 1, and this is what I want it to return but my code isn't working, why? Can a modification be made ?
Here's my code
Python
def halve(n):
i = 0
for i in range(n,1):
if float(i/2) >=1:
i+=1
return i
Thanks,
It seems to me you can employ some math and just write:
import math
def halve(n):
return math.floor(math.log(n, 2))
You attempt is wrong for three reasons.
You are returning from within the loop. Thus it will never execute more than once.
Your loop will never execute unless n is 0 because range needs a third parameter as a negative number to increment "backwards."
The i assigned by your loop is shadowing the i you have previously assigned. Let's just use a while loop.
def halve(n):
i = 0
while n/2 >= 1:
i += 1
n /= 2
return i
Since you wanted to know what is wrong with your code:
First, immediate problem is that you return on first loop iteration. You need to return only when the number is smaller than 1 (so if condition is not met):
else:
return i
Now, there is another problem - you are iterating over range(n,1) which... doesn't really make sense. You need way less than n divisions to reach number smaller than 1. Use a while loop instead - this way you loop as long as you need. You're also using i as an iterator, but also seem to be dividing it to see if it's more than one - shouldn't you use n there? You're also not reducing n, so you'd actually never reach n < 1.
Taking all of those your code might look like this:
while True:
if float(n/2) >=1:
i+=1
n /= 2
else:
return i
We can improve it even further - as you want your code to end if the condition is not met, we can simply move that to while condition:
def halve(n):
i = 0
while float(n/2) >=1:
i+=1
n /= 2
return i
As #Sembei Norimaki commented, a while loop is a more natural fit for this use case.
def halve(n):
halves = 0
while n > 1:
n /= 2
halves += 1
return halves
This loop can be summarized "As long as n is greater than one, cut it in half and add one to the number of halves we have performed."

Creating a function that returns prime numbers under a given maximum?

Instructions are to write a function that returns all prime numbers below a certain max number. The function is_factor was already given, I wrote everything else.
When I run the code I don't get an error message or anything, it's just blank. I'm assuming there's something I'm missing but I don't know what that is.
def is_factor(d, n):
""" True if `d` is a divisor of `n` """
return n % d == 0
def return_primes(max):
result = []
i = 0
while i < max:
if is_factor == True:
return result
i += 1
You should test each i against all divisors smaller than math.sqrt(i). Use the inner loop for that. any collects the results. Don't return result right away, for you should fill it first.
def return_primes(max):
result = []
for i in range(2, max):
if not any(is_factor(j, i) for j in range(2, int(math.sqrt(i)) + 1)):
result.append(i)
return result
print(return_primes(10))
As a side note, use for and range rather than while to make less mistakes and make your code more clear.
The reason that your code is returning blank when you run it, is because you are comparing a function type to the value of True, rather than calling it.
print(is_factor)
<function is_factor at 0x7f8c80275dc0>
In other words, you are doing a comparison between the object itself rather than invoking the function.
Instead, if you wanted to call the function and check the return value from it, you would have to use parenthesis like so:
if(is_factor(a, b) == True):
or even better
if(is_factor(a, b)):
which will inherently check whether or not the function returns True without you needing to specify it.
Additionally, you are not returning anything in your code if the condition does not trigger. I recommend that you include a default return statement at the end of your code, not only within the condition itself.
Now, in terms of the solution to your overall problem and question;
"How can I write a program to calculate the prime numbers below a certain max value?"
To start, a prime number is defined by "any number greater than 1 that has only two factors, 1 and itself."
https://www.splashlearn.com/math-vocabulary/algebra/prime-number
This means that you should not include 1 in the loop, otherwise every single number is divisible by 1 and this can mess up the list you are trying to create.
My recommendation is to start counting from 2 instead, then you can add 1 as a prime number at the end of the function.
Before going over the general answer and algorithm, there are some issues in your code I'd like to address:
It is recommended to use a different name for your variable other than max, because max() is a function in python that is commonly used.
Dividing by 0 is invalid and can break the math within your program. It is a good idea to check the number you are dividing by to ensure it is not zero to make sure you do not run into math issues. Alternatively, if you start your count from 2 upwards, you won't have this issue.
Currently you are not appending anything into your results array, which means no results will be returned.
My recommendation is to add the prime number into the results array once it is found.
Right now, you return the results array as soon as you have calculated the first result. This is a problem because you are trying to capture all of the prime numbers below a specific number, and hence you need more than one result.
You can fix this by returning the results array at the end of the function, not in between, and making sure to append each of the prime numbers as you discover them.
You need to check every single number between 2 and the max number to see if it is prime. Your current code only checks the max number itself and not the numbers in between.
Now I will explain my recommended answer and the algorithm behind it;
def is_factor(d, n):
print("Checking if " + str(n) + " is divisible by " + str(d))
print(n % d == 0)
return n % d == 0
def return_primes(max_num):
result = []
for q in range(2, max_num+1):
count_number_of_trues = 0
for i in range(2, q):
if(i != q):
if(is_factor(i, q)):
print("I " + str(i) + " is a factor of Q " + str(q))
count_number_of_trues += 1
if(q not in result and count_number_of_trues == 0):
result.append(q)
result.append(1)
return sorted(result)
print(return_primes(10))
The central algorithm is that you want to start counting from 2 all the way up to your max number. This is represented by the first loop.
Then, for each of these numbers, you should check every single number from 2 up to that number to see if a divisor exists.
Then, you should count the number of times that the second number is a factor of the first number, and if you get 0 times at the end, then you know it must be a prime number.
Example:
Q=10
"Is I a factor of Q?"
I:
9 - False
8 - False
7 - False
6 - False
5 - True
4 - False
3 - False
2 - True
So for the number 10, we can see that there are 2 factors, 5 and 2 (technically 3 if you include 1, but that is saved for later).
Thus, because 10 has 2 factors [excluding 1] it cannot be prime.
Now let's use 7 as the next example.
Example:
Q=7
"Is I a factor of Q?"
I:
6 - False
5 - False
4 - False
3 - False
2 - False
Notice how every number before 7 all the way down to 2 is NOT a factor, hence 7 is prime.
So all you need to do is loop through every number from 2 to your max number, then within another loop, loop through every number from 2 up to that current number.
Then count the total number of factors, and if the count is equal to 0, then you know the number must be prime.
Some additional recommendations:
although while loops will do the same thing as for loops, for loops are often more convenient to use in python because they initialize the counts for you and can save you some lines of code. Also, for loops will take care of the incrementing process for you so there is no risk of forgetting.
I recommend sorting the list when you return it, it looks nicer that way.
Before adding the prime factor into your results list, check to see if it is already in the list so you don't run into a scenario where multiples of the same number is added (like [2,2,2] for example)
Please note that there are many different ways to implement this, and my example is but one of many possible answers.

initialization in while loop

I'm new to python and having problem understanding the code snippet below.
I don't understand why sum is initialized to 0, not 1. Can anyone explain this initialization?
n = 4
sum = 0 # initialize sum
i = 1 # initialize counter
while i <= n:
sum = sum + i
i = i+1 # update counter
print("The sum is", sum)
This is actually more of a basics in computer programming than Python question. I'll try to explain in brief here.
Looks like your goal is to find the sum of integers 1 to n.
If you initialize the sum with 1, then the result you get will be always 1 more than the actual sum of integers 1 to n. You can verify that by doing runs with small values of n, say up to 5 and comparing the output you get with the actual sum. I'll put a table here for convenience:
n | sum(1, n)
-------------
1 | 1
2 | 3
3 | 6
4 | 10
5 | 15
Now compare this to the output when sum is initialized with 1. For curiosity's sake, you can play with initializing with other values too.
Sum is initialized to zero, but because the less than or equal to of n and i you always run at least one loop, take i = 1, then the sum is still one because the loop checks if 1 is less than or equal to 1 (i <= n). since it is then it adds i to the sum (ie. 1) making sum 1. it then updates i to 2 and goes to the top of the while loop. Since 2 <= 1 or i <= n is false here it breaks out and prints one as the sum

Breaking an iterative function in Python before a condition turns False

This is for a school assignment.
I have been tasked to define a function determining the largest square pyramidal number up to a given integer(argument). For some background, these are square pyramidal numbers:
1 = 1^2
5 = 1^2+2^2
14 = 1^2+2^2+3^2
So for a function and parameter largest_square_pyramidal_num(15), the function should return 14, because that's the largest number within the domain of the argument.
I get the idea. And here's my code:
def largest_square_pyramidal_num(n):
sum = 0
i = 0
while sum < n:
sum += i**2
i += 1
return sum
Logically to me, it seemed nice and rosy until I realised it doesn't stop when it's supposed to. When n = 15, sum = 14, sum < n, so the code adds one more round of i**2, and n is exceeded. I've been cracking my head over how to stop the iteration before the condition sum < n turns false, including an attempt at break and continue:
def largest_square_pyramidal_num(n):
sum = 0
for i in range(n+1):
sum += i**2
if sum >= n:
break
else:
continue
return sum
Only to realise it doesn't make any difference.
Can someone give me any advice? Where is my logical lapse? Greatly appreciated!
You can do the following:
def largest_pyr(x):
pyr=[sum([i**2 for i in range(1,k+1)]) for k in range(int(x**0.5)+1)]
pyr=[i for i in pyr if i<=x]
return pyr[-1]
>>>largest_pyr(15)
14
>>> largest_pyr(150)
140
>>> largest_pyr(1500)
1496
>>> largest_pyr(15000)
14910
>>> largest_pyr(150000)
149226
Let me start by saying that continue in the second code piece is redundant. This instruction is used for scenario when you don't want the code in for loop to continue but rather to start a new iteration (in your case there are not more instructions in the loop body).
For example, let's print every number from 1 to 100, but skip those ending with 0:
for i in range(1, 100 + 1):
if i % 10 != 0:
print(i)
for i in range(1, 100 + 1):
if i % 10 == 0:
# i don't want to continue executing the body of for loop,
# get me to the next iteration
continue
print(i)
The first example is to accept all "good" numbers while the second is rather to exclude the "bad" numbers. IMHO, continue is a good way to get rid of some "unnecessary" elements in the container rather than writing an if (your code inside if becomes extra-indented, which worsens readability for bigger functions).
As for your first piece, let's think about it for a while. You while loop terminates when the piramid number is greater or equal than n. And that is not what you really want (yes, you may end up with a piramid number which is equal to n, but it is not always the case).
What I like to suggest is to generate a pyramid number until in exceedes n and then take a step back by removing an extra term:
def largest_square_pyramidal_num(n):
result = 0
i = 0
while result <= n:
i += 1
result += i**2
result -= i ** 2
return result
2 things to note:
don't use sum as a name for the variable (it might confuse people with built-in sum() function)
I swapped increment and result updating in the loop body (such that i is up-to-date when the while loop terminates)
So the function reads like this: keep adding terms until we take too much and go 1 step back.
Hope that makes some sense.
Cheers :)

How can I complete my Python code for the "persistence bugger"?

I'm not sure how to complete my code for the "persistence bugger" problem. The goal is to create a function called "persistence" to return the number of iterations until the digits of a number multiplied together equal a single-digit number.
For example: persistence(39) = 3, because 3*9 = 27, 2*7 = 14, and 1*4 = 4; hence, three iterations.
Below is my code so far, and I'm not sure where to go from here or if what I have is correct. Any help would be appreciated.
def persistence(num):
num_str = str(num)
total = 1
for i in num_str:
total = total * int(i)
while len(str(total)) <> 1:
total = total * total(i)
#not sure what to do from here...
You'll notice that if you want to do what you did to 39 to 27, you'll be repeating code. This is a case where recursion can help (calling the function in itself):
def persistence(num):
if num < 10:
return 0 # Only one digit. Can't iterate over it
num_str = str(num)
total = 1
for i in num_str:
total = total * int(i)
return 1 + persistence(total) # We do 1 + because we just did an iteration
Let's imagine the input was 39:
39 is not less than 10 so we go to the next stage.
We do the same code you provided to get the total of multiplying the digits
We now have a new number (27) allocated at total. We repeat the code above by calling the function again, but instead of passing 39, we pass through 27.
27 is not less than 10 so we go to the next stage
We get the multiplication of digits
We repeat until we get a total of 4 (1*4).
We call persistence(4), but it returns 0 because 4 < 10. No iteration has been done for the number 4 (hence we return 0)
At this point, recursion has stopped. Python now backtracks through all previous calls. It adds 0 + 1 + 1 + 1 to give 3.
Recursion is a little tricky to wrap your head around at first, but basically it's a function which calls itself, but it has "base cases" to stop the function running indefinitely. In this case, our base case is if the number is less than 10 (it has one digit). No iterations take place on this number.
TerryA's answer is really good, and when you want to keep applying the result of a function to the same function again (as in this example) recurssion is normally a very good idea. Just for the sake of completeness though, the solution can be implemented with a simple while loop, not too dissimilar to what you have attempted:
def persistance(num):
counter=0
while num>9:
counter+=1
num_str=str(num)
total=1
for i in num_str:
total=total* int(i)
num=total
print (counter)
The counter keeps track of how many times the loop runs, which gives you your final answer.

Categories

Resources