0% found this document useful (0 votes)
20 views5 pages

ADA - 06 - June - 2024

The document outlines algorithms for counting the number of binary digits in a decimal integer and summing natural numbers. It provides a step-by-step approach to design these functions, detailing the input, output, and time complexity. Additionally, it discusses the efficiency class of the algorithms and suggests improvements for better performance.

Uploaded by

Nagaraj Naik
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)
20 views5 pages

ADA - 06 - June - 2024

The document outlines algorithms for counting the number of binary digits in a decimal integer and summing natural numbers. It provides a step-by-step approach to design these functions, detailing the input, output, and time complexity. Additionally, it discusses the efficiency class of the algorithms and suggests improvements for better performance.

Uploaded by

Nagaraj Naik
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/ 5

Mow to deslgn a function to find the number of binary digits of n decimal Integer?

Design ALGORITHM count binary_digits (n)


/Purpose : Tocount number of binary digits
- I|Input :n: decimal number
I/Output : Number of binary digits
n/2 210 COunt
count= 0
n/2 2 5 -0 1
while (n >=1)
n/2 2 2 -1 1 n=n/ 2
n/2 21 -0 1 count = COUnt+ 1
0 -1 1 end while

(10),% =(1 0 1 0),= 4digits return count


How to design a function to find the number of binary digits of a decimal intoger?
The time complexity can be computed as shown below: ALGORITHM count binary digits (n )
Step 1: Parameter to be considered : n
/Purpose : To count number of binary digits
Step 2: Basic operation: n>= 1
Step 3: Thenumber of times the basic operation is executed
/Input :n: decimal number
depends on : whether n >= 1 Output : Number of binary digits
Step 4: Express total number of times basic operation is count = 0
executed in terms of mathematical function. while (n >= 1)
n=n/2
count = Count 1

Sinee we are returning count = k, we need to find end while


the value of k which gives number of digits in binary return count
number using the following relation:
n
How to design a function to find the number of binary digits of a decimal integor?
The time complexity can be computed as shown below:
Step 1: Parameter to be considered :
ALGORITHM count binary_digits(n)
I/ Purpose : To count number of binary digits
Step 2: Basic operation: n>= 1 I/ Input :D: decimal number
Step 3: The number of times the basic operation is executed
depends on : whether n>= 1 / Output : Number of binary digits
Step 4: Express total number of times basic operation is count= 0
executed in terms of mathematical function. while (n >= 1)|
n=n/ 2
= 1
count = count + 1
end while
Since we are returning count - k, we need to find
the value of k which gives number of digits in binary return count
number using the following relation:
k
2
Step 5: Find the value of kwhich gives number of binary digits.

k log 2 = log n Number of times basic operation is executed = log, n


log n
k
log 2 log, n
How to solve the given problen?
Consider the part of the algorithm: ALGORITHM SUM (n)
Sum = )
I/ Purpose To add natural numbers
for i= 1 to n )i =1+2+ 3 + .. n I/Input :A non negative integer
Sum = sum + i i=1
/Output :returns sum of all natural numbers
The algorithm computes : sum of natural numbers Sum = 0
for i= 1 to n
b.The basic operation: sum = sum +i Sum = Sum +i
The number of times the basic operation is executed end for
can be obtained as shown below: return sum
for i = 1 to n
Sum = Sum +0
a. What does this algorithm compute?
b. What is its basic operation?
C. How many times the basic operation is executed?
f(n)= 1 = n-1+1=n d. What is the efficiency class of this algorithm?
i 1 e. Suggest an improved algorithm and indicate the
So, total number of times basic operation is executed efficiency class
f(n)=n
d. The eficiency class of this algorithm = 0 (n))
e. The algorithmcanbe improved using: n (n+1)/2 and its efficiency = 0(1)
How to find the order of growth of an algorithm?
Problem 1: For each of the following functions, indicate how much the function's value change if the argument value is
increased fourfold.
a. log,n b. n C. D d. n' e. n? f. 20
Solution:
Let f (n) = n o. (1)
If the argument value is increases four fold, we get 4 n
Let g(n) =N4n ...2)
Let us express g (n) in terms of f(n).
g (n) = 4n
= 2n
= 2f(D)
Function's value is increases 2 times

You might also like