Skip to main content

Factorial (Python)

Factorial example:

 

 def r_factorial(n):
    if n==1:
        return n
    else:
        t=r_factorial(n-1)
        t=t*n
    return t
    
print(r_factorial(5)) 


Why once t=1 the function continue?

The reason the recursion continues after reaching n == 1 is because of how recursive function calls work. Let's break it down step by step.


Understanding Recursive Execution:

Each recursive call pauses execution until the next recursive call returns a value. The recursion only stops when it reaches the base case, which is n == 1. At that point, the function starts returning values back up the recursive stack.

Step-by-Step Breakdown:

When you call recur_factorial(5), the function calls itself repeatedly, decreasing n each time:

1. r_factorial(5): Calls r_factorial(4), waiting for its result.


2. r_factorial(4): Calls r_factorial(3), waiting for its result.


3. r_factorial(3): Calls r_factorial(2), waiting for its result.


4. r_factorial(2): Calls r_factorial(1), waiting for its result.


5. r_factorial(1): Base case reached → returns 1.



Now, as each function returns, it resumes execution from where it left off:

6. r_factorial(2): t = 1 * 2 = 2, returns 2.


7. r_factorial(3): t = 2 * 3 = 6, returns 6.


8. r_factorial(4): t = 6 * 4 = 24, returns 24.


9. r_factorial(5): t = 24 * 5 = 120, returns 120.



Why the Loop "Keeps Working":

The function doesn't loop in the traditional sense (like a for or while loop). Instead, it stacks function calls.

Each function call pauses execution until the smaller subproblem is solved.

Once n == 1 is reached, the function starts returning values back up.


This is a fundamental principle of recursion: solve the smallest problem first, then use that result to solve the larger problems.






Example from the Python course:

https://www.youtube.com/watch?v=fW_OS3LGB9Q&t=869s

 

Comments

Popular posts from this blog

What should you know about HA 'override enabled' setting on Fortigate?

High availability is mandatory in most of today's network designs. Only very small companies or branches can run their business without redundancy. When you have Fortigate firewall in your network you have many options to increase network availability. You can use Fortigate Clustering Protocol ( FGCP ) or Virtual Router Redundancy Protocol ( VRRP ). FGCP has two modes: 'override' disabled (default) and 'override' enabled . I'm not going to explain how to set up HA as you can find many resources on Fortinet websites: https://cookbook.fortinet.com/high-availability-two-fortigates-56/ https://cookbook.fortinet.com/high-availability-with-fgcp-56/ Let's recap what is the main difference between them. The default HA setting is 'override' disabled and this is an order of selection an active unit: 1) number of monitored interfaces - when both units have the same number of working (up) interfaces check next parameter 2) HA uptime - an ...

FortiGate and GRE tunnel

Recently I worked on one project where a client requested to re-route web traffic to the GRE tunnel to perform traffic inspection. I would like to share with you what is required if you configure it on FortiGate. We need a new GRE interface and policy base routing (PBR) to change the route for specific source IPs. Of course you need firewall policies to permit the traffic. Let's start with GRE interface. Unfortunately you can't configure it using the GUI, only CLI is the option: config system gre-tunnel edit "gre1" set interface "port1" set local-gw 55.55.55.55 set remote-gw 44.44.44.44 next end When the end peer is Cisco router, you need to set the IP for the GRE interface: config system interface edit gre1 set ip 192.168.10.10 255.255.255.255 set remote-ip192.168.10.20 end In next step we need to fix routing. We need the alternate path via GRE but to keep the route in the active routing table you need to set the same AD (adminis...

Data Leak Prevention (DLP) on Fortigate

Today I would like to present one interesting feature you may find on your Fortigate - Data Leak Prevention. I know there are much better, dedicated solutions on the market but in certain situations the DLP feature available on FortiOS is good enough. Why you should use it? This is very important to say: the DLP in such deployment (on Fortigate) can't protect your data against every data leak. Users in your network with his/her mobile can easily take a photo of any document. Why we should still consider it? It is a good (and easy to deploy) method to prevent users' mistakes. It happened hundreds of time when a user attached a wrong file. Sound familiar? Using the DLP you can create policies which stop such leak. Let me show you how you can configure it. Step #1 First, you have to check if DLP is enabled in a "Feature Visibility" and "Security Features" section: When you do not see the feature, make sure your Fortigate works in a proxy-ba...