Polynomial Time U - 5
Polynomial Time U - 5
POLYNOMIAL TIME
X A
k
P O(n )
X Problem
A Algorithm
(Procedure for solve a problem based on
sequence of steps)
T(n) = O(nk )
where,
n is the input size
k is the constant
X< PY
X Y
Polynomial Time
Reduction
Non deterministic
Polynomial Time
Reduce
NP
P NP
HARD
Algorithm for Polynomial Time:-
To compute the first n natural numbers.
#include<iostream>
int num(int n)
{
int sum=0;
#loop through the numbers from 1 to n
for(int i=1; i<=n; i++)
{
#add the values upto n
sum+=i;
}
return sum
}
int main()
{
#assume the value of n is 5
int n=5;
int value=num(n);
std::cout<<”Sum of the first”<<n<<”natural
numbers:”<<result<<std::endl;
return 0;
}
In this algorithm, the function num computes the sum of
first n natural numbers using for loop.
The Time Complexity of algorithm is O(n).
The loop iterates through the numbers from 1 to n, and
the sum is updated in each iteration.