SlideShare a Scribd company logo
RECURSION
TECHNIQUES
IN PROGRAMING
By Shck Tchamna: tchamna@gmail.com
Problem statement
•Given an array of numbers
•Calculate the sum
 
0 1 1
, ,... N
A x x x 

  0 1 1
... N
S A x x x 
   
     
0 1
0 ; 1 ; , 0, 1
k
A x A x A k x k N
    
There are many ways to solve it!
Iterative
Recursive
A = [2,8,12,0]
Sum_array = 0
for i in range(len(A)):
Sum_array = Sum_array + A[i]
print(Sum_array)
Iterative
Recursive
All we need to find in a recursive formula are:
1. Generalization of the steps: The recurrence
definition
2. Base case: The simplest case of te probe
  1 0 1
|step
S A x x
 
     
2 0 1 2 1 2
| |
step step
S A x x x S A x
    
       
0 1 1
| ... |
stepk k k k
step k
S A x x x S A x
 
     
1
k k k
S S x

 
  0 1 1
... N
S A x x x 
   
 |stepk k
Let S A S
 
     
1
| |
stepk k
step k
S A S A x

 
1
k k k
S S x

 
So our recursive function depends on A and k
   
, , 1 k
f A k f A k x
  
 
k
x A k

     
, , 1
f A k f A k A k
  
     
, , 1
f A k f A k A k
  
Generalization
Base Case
What happen in the above formula when k=0
   
0 1
| ...
stepk k k
S A x x x

   
  0 0
|
S A x

Step k: sum from 0 to k
Step 0: sum from 0 to 0
     
, , 1
f A k f A k A k
  
Generalization
Base Case
   
0 0 0
| ,0
S A x f A x
  
def f(A,k):
if k ==0:
return A[0]
else:
return f(A,k-1) + A[k]
def f(A,k):
if k == 0:
return A[0]
return f(A,k-1) + A[k]
def f(A,k):
if k ==0:
return A[0]
else:
return f(A,k-1) + A[k]
# Test the code
A = [1, 5, 8, 5 -18]
N = len(A)-1 # we substract 1 because indexes start in python from 0 to len(A)-1
Sum_A = f(A,N)
print(Sum_A)
The above function computes the sum of the k first elements of the array A
If you want to calculate the sum of all the elements of the array, then let k = len(A)

More Related Content

Similar to Recursion Algorithms Derivation

22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final
vibhuti bansal
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
Vlad Patryshev
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
Nv Thejaswini
 
MASSS_Presentation_20160209
MASSS_Presentation_20160209MASSS_Presentation_20160209
MASSS_Presentation_20160209Yimin Wu
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theorem
Wathna
 
Variations on the method of Coleman-Chabauty
Variations on the method of Coleman-ChabautyVariations on the method of Coleman-Chabauty
Variations on the method of Coleman-Chabauty
mmasdeu
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
CaseyHaaland
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
Pranshu Bhatnagar
 
4.3e.pptx
4.3e.pptx4.3e.pptx
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
Scilab
 
Karnaugh
KarnaughKarnaugh
Karnaugh
camilosena
 
How to design a linear control system
How to design a linear control systemHow to design a linear control system
How to design a linear control system
Alireza Mirzaei
 
Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107
Dennis Almeida
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdf
VishalPusadkar1
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
fashiontrendzz20
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Vivekananda Samiti
 
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability CalculusControl of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Leo Asselborn
 
ABC-Gibbs
ABC-GibbsABC-Gibbs
ABC-Gibbs
Christian Robert
 

Similar to Recursion Algorithms Derivation (20)

22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final22 01 2014_03_23_31_eee_formula_sheet_final
22 01 2014_03_23_31_eee_formula_sheet_final
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
MASSS_Presentation_20160209
MASSS_Presentation_20160209MASSS_Presentation_20160209
MASSS_Presentation_20160209
 
Rosser's theorem
Rosser's theoremRosser's theorem
Rosser's theorem
 
Variations on the method of Coleman-Chabauty
Variations on the method of Coleman-ChabautyVariations on the method of Coleman-Chabauty
Variations on the method of Coleman-Chabauty
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Introduction to the AKS Primality Test
Introduction to the AKS Primality TestIntroduction to the AKS Primality Test
Introduction to the AKS Primality Test
 
4.3e.pptx
4.3e.pptx4.3e.pptx
4.3e.pptx
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
Karnaugh
KarnaughKarnaugh
Karnaugh
 
How to design a linear control system
How to design a linear control systemHow to design a linear control system
How to design a linear control system
 
Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107Year 13 challenge mathematics problems 107
Year 13 challenge mathematics problems 107
 
Lecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdfLecture Notes on Adaptive Signal Processing-1.pdf
Lecture Notes on Adaptive Signal Processing-1.pdf
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
 
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT KanpurMid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
Mid semexam | Theory of Computation | Akash Anand | MTH 401A | IIT Kanpur
 
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability CalculusControl of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
Control of Uncertain Nonlinear Systems Using Ellipsoidal Reachability Calculus
 
ABC-Gibbs
ABC-GibbsABC-Gibbs
ABC-Gibbs
 

More from Rodrigue Tchamna

Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Rodrigue Tchamna
 
Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)
Rodrigue Tchamna
 
Constraint optimal control
Constraint optimal controlConstraint optimal control
Constraint optimal control
Rodrigue Tchamna
 
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Rodrigue Tchamna
 
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Rodrigue Tchamna
 
Salutations en langues camerounaises short
Salutations en langues camerounaises shortSalutations en langues camerounaises short
Salutations en langues camerounaises short
Rodrigue Tchamna
 
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Rodrigue Tchamna
 

More from Rodrigue Tchamna (7)

Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
Mets camerounais (bamilekes) en langue fe'efe'e (nufi)
 
Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)Les animaux en langue bamileke nufi (fe'efe'e)
Les animaux en langue bamileke nufi (fe'efe'e)
 
Constraint optimal control
Constraint optimal controlConstraint optimal control
Constraint optimal control
 
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
Sumo tutorial: 1) Manual Network creation, 2) OSM to Netwrok, 3) OD Matrix to...
 
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
Sumo, Simulation of Urban Mobility, (DLR, Open Source) tutorial
 
Salutations en langues camerounaises short
Salutations en langues camerounaises shortSalutations en langues camerounaises short
Salutations en langues camerounaises short
 
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
Mbɑ̄' yòh yì á ntáb pʉ̄h lɑ́, dhì zǒ mʉ̀ ! Notre père qui est aux cieux, rest...
 

Recently uploaded

DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
itech2017
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
Nettur Technical Training Foundation
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
AkolbilaEmmanuel1
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 

Recently uploaded (20)

DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 

Recursion Algorithms Derivation

  • 1. RECURSION TECHNIQUES IN PROGRAMING By Shck Tchamna: tchamna@gmail.com
  • 2. Problem statement •Given an array of numbers •Calculate the sum   0 1 1 , ,... N A x x x     0 1 1 ... N S A x x x            0 1 0 ; 1 ; , 0, 1 k A x A x A k x k N     
  • 3. There are many ways to solve it! Iterative Recursive
  • 4. A = [2,8,12,0] Sum_array = 0 for i in range(len(A)): Sum_array = Sum_array + A[i] print(Sum_array) Iterative
  • 5. Recursive All we need to find in a recursive formula are: 1. Generalization of the steps: The recurrence definition 2. Base case: The simplest case of te probe
  • 6.   1 0 1 |step S A x x         2 0 1 2 1 2 | | step step S A x x x S A x              0 1 1 | ... | stepk k k k step k S A x x x S A x         1 k k k S S x      0 1 1 ... N S A x x x       |stepk k Let S A S  
  • 7.       1 | | stepk k step k S A S A x    1 k k k S S x    So our recursive function depends on A and k     , , 1 k f A k f A k x      k x A k        , , 1 f A k f A k A k   
  • 8.       , , 1 f A k f A k A k    Generalization Base Case What happen in the above formula when k=0     0 1 | ... stepk k k S A x x x        0 0 | S A x  Step k: sum from 0 to k Step 0: sum from 0 to 0
  • 9.       , , 1 f A k f A k A k    Generalization Base Case     0 0 0 | ,0 S A x f A x    def f(A,k): if k ==0: return A[0] else: return f(A,k-1) + A[k]
  • 10. def f(A,k): if k == 0: return A[0] return f(A,k-1) + A[k] def f(A,k): if k ==0: return A[0] else: return f(A,k-1) + A[k] # Test the code A = [1, 5, 8, 5 -18] N = len(A)-1 # we substract 1 because indexes start in python from 0 to len(A)-1 Sum_A = f(A,N) print(Sum_A) The above function computes the sum of the k first elements of the array A If you want to calculate the sum of all the elements of the array, then let k = len(A)