01_time_and_space_complexity
01_time_and_space_complexity
for cg
x is a
factor of n
then n 0
8 1 2 4 8
Given total
a number n
find out the
number
of factors
BRUTE FORCE
T.int int i 1
for ien itt
L
if i
n o
Count tf
3
3
Iteration 1 loop
108iterations 1 sec
n no of iteration Timetaken
317 years
101 iteration 10
8
1010
9 5
19
n i is a factor
then is also a factor
24 25
i Ni i Ni
1 24 1 25
2 12 5 5
a
8
6
3
IIT
12 2
24 1 ixi n
i ñ
for int i 1 ixi n it L
L
if
n i o
i Ii
if Count count 2
else
count count 1
3
3
24 25
i count I count
1 2 1 2
2 4 5 3
3 6
8
Total iterations Jn
Total iteration Total time
1818
109
of number
T a
a a n
6
Go
6 6 36
x̅ a
10T
1018 2 109
Pjime numbers Number where
count 2
of factors
10 11 23 2 25 27 31
4
a b Range
of numbers
when both a b
are inclusive
a b Range
of numbers
when both a b
are exclusive
b
Avish 3.10 a
10 3 1 8 b at
3 4 5 6 7 8 9,10 no
of integers
in the range
a b
Sum of 1ˢᵗ natural
If
n
number
1 2 3 the 100
1 01 505
4 12 36 108
f Common
3 ration
a first term
Sum 1ˢᵗ n terms GP
of in
a ar ar as as
s
off 8 1
5 10 20 40 80 160
A 5 8 2 n 6
of 542
5 64 1
5 63 315
for int i 1 n L
i
if
n
break
3
1 n n it
n iterations
for int i 0 100 L
S St it i
3
0 100 I 100 0 1
101 iterations
Quiz 2
L
n iterations
m iterations
Ntm
How to compare 2 algorithms
Sumanth Amogh
10 sec 20sec
writes in Ctt
10sec 8sec
5sec 8sec
100 logn No
Asymptotic
Analysis Analysing algorithm
of very large
fornumber
Algorithms Δ
BIG
1 no
of iterations
2
ignore lower order terms
3
ignore constant
niftions
14
constant
06 Fn
did f Un 3nlogn I
Un 3nlogn 1
nlogn
O nlogn
106
F n Unlogn 3nF
oarn
Comparison
Qer
log n A n nlogn
non m m 2 n
Neglect Lower Oodes terms
Iterations m loon
Contribution
Total iterations of
Infge lower orderteam
D 10 100 1000 90
lologn n Alost
loologn N Algo 1
1stI.su
When we that alog 1 is better
say
than algo 2 using Bigo we can
only make that that claim for large
input
2ⁿᵈ Issue
Algol 2m Un Algo2 3m
n n
2024 4m
3
9
1n
204