0% found this document useful (0 votes)
40 views

Recursion - 2 - E - Java

1. The document discusses recursive functions to calculate the sum of digits of a number, power of a number, and factorial of a number. 2. It explains that the time complexity (Tc) of a recursive function is the time taken by a single function call (x) multiplied by the total number of function calls (y). 3. The overall Tc of a recursive function is O(n) where n is the number of recursive calls, since each call takes a constant time and there are n calls.

Uploaded by

master ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Recursion - 2 - E - Java

1. The document discusses recursive functions to calculate the sum of digits of a number, power of a number, and factorial of a number. 2. It explains that the time complexity (Tc) of a recursive function is the time taken by a single function call (x) multiplied by the total number of function calls (y). 3. The overall Tc of a recursive function is O(n) where n is the number of recursive calls, since each call takes a constant time and there are n calls.

Uploaded by

master ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Agenda

is Poulain
ii How to find Tc recursive ode
of
I

a r Givin n In ol find sum of all digits


N roo y sun i ti to 4 6

N V28 sum 11 2 8 11

int sum int n

if in a
07 3 Assumption Given N sun
return o
returning sum of all digits
3

int d n t to Main logic


i
int In int d ni lo
tap sun 10

return
inttemp sum IN lo
tenptd
return tempt d
3
Bas condition
if In so 3
return 0
int sum int n

if in 07 9

y return o
1234

aint da N t.io

3 int tap sun In 110

in

int sum int n

if in 07 9

y return o

234

aint da N t.io

s int tap sun In 110

3
a 2 hives a n Calculate an
a 2 n s I ans a 25 32

9 3 n n I ans 3 I 81

as i n too I ans y s r

int pow tint a int n

if ne o 3 Assumption given a n Pow

return 1 returns an

Main logic
int temp pro la n it
ah ah t a
return tempta
Bast condition
3 wig
if Ina 1

return a return a

3
int pow tint a int n

is Ina o
1 return 2

2 int temp pro la n it

3 return temp ai
g
ng

better
something

an 2 an a f n is odd

58 5 5

59 5 5 x 5
at row tint a int n

is n so

al return a

aint temp

if I n t 22
a pro la n 12

01
i
return tempt temp else

return temp temp a

int row tint a int n

is n so
a return a
3 lets

aint temp a pro la n 12


i
if I n t 22 01 3 lets
return tempt temp
g
t
return temp temp a Pow 12 lol
71024
main
int pow tint a int n if n so
returns
is Ina so g
int temp pro la n 12

if In t 22 01
int temp Pow la n it return tempt temp

turn tempea
else
g
return temp temp a

5
3120
t 3,20
3,19
t t
3 18 3 to
t t
3,17 3,5
t t
3,16 3,2 dog n
I I calls
3 r

3,4 t
t 3,0
3,3
t
3,2 n calls
t
3 I
A 3 Given a n p lalculate ah l p

o c a 10
I L 105
I p c 109

long
I pow tint a int n.int P
if n so

return a

Intltemps pro la n 12 P

if In t 22 01
return Hemp't tempt t p

else
return temp trap i p a l p

g
TC of recursive code

Recursive code a function getting called


multiple times

Tc of single Tuition x

total no of function calls y

Overall Tc x y

int sum lint n t of single surition on


if In p g total no of calls n

return i
n 5
g TC Oln
t
nay
int tamp sum In it
t
n 3
return temptn
t
ne 2
3
int factorial tint at 3 Tl of single June 0117
In s total no of calls
if a a n

return 1
n s Tc Oln
g t
n y
int temp factorial n n t
n 3
return tenpin I
3 Me 2

I
n o

Te of single function oil

total no of calls 2
TC 2
int fib tint ni
if n so 11 n it has they
return n
n h I th z
n 3

int tempo sib in a


ne Ea n
ther n tha h th
int temps fib in 21
ah ha hi to hino n th o
return tempt t temps
3
Lino
n 6 9

hes
l
ne y 2
t th t
AU
n the 2
3 4
t ha ha ter tha nt
n 3 n not 8

n te hito kino n tn
µg
total calls I t 27 448

St A z g

att 8

t
2
N

w an
int s int e
booltan palindrome I share a

if Is se it s s e t A single function oh
return true n 12
total no of calls
if Afs Ale 5 0 i a
Tc O Int
return false t
g Ser e 8
t
Stl e l 2 e
boolean ans palindrome IA
I
return ans
s 3 9 8
g
I
5 4 1 5
I
5 9 es a
int pow tint a int n
Tc of single function ol
is Ina o total no calls n
of
return 1
13,5 Tl Oln
g t
13141
int temp Pow la n it I
13131
return tampon nan
I
g
1312

Irl
1st

int pow tint a int


Tc of single function on
if n so
returns ya no calls 109
of
int temps pro la n 12 3,20
t
if In t 22 01 Smart 3,10
return tempt temp t
13,51
g
t
else 13121
return temp temp a I
131
s I
5 3101
TC logan
int pow tint a int n 3
TC of single function oft
if n so
total no n
return a
of calls

TC Oln

if In t 22 01
pseudo smart
return Pow la n 121 pro la n'z

else
return Pow la n 121 pro la n 2 a

5
2
3 101
I y
13157 2
13,53
I I
113121
13121 1312 13 2 4

131 Fir Sir in


Boston B Bio 8

his 1st tin biota it p.o t.oty.at ot io tz.otxirtI 13,017 10

total calls 1 2 44 stir t


g g
St
Ift
V2 2
t logan
21092 a'rain
1
1249
L I I n
Doubts bar

bar

15

bar l x y x y

for

nai
in front total pair with sum K
x
2 5 10 7 5 7 9 1 12

K Ali 242
542
1042
772
locate It a a 2 at a
us freq

r u9
k is

3 1 I
É Asi a

A 9 s y
8 17
14 I
Ali tie a ats in

You might also like