Introduction to Algorithm - Algorithm Vs Program


An algorithm is finite step-by-step rules or instructions to convert input(s) to output(s). It is simple and effective.

A computer program is a collection of instructions that performs a specific task when executed by a computer. It follows the syntax of the programming language.

But my point is not about defining the word. My point is to tell you what an algorithm can do but a program cannot, and what a program can do but an algorithm cannot.

Example-To solves a problem, you need to design an algorithm, and then implement the algorithm in the source code of any language. However, some problems can be solved by designing an algorithm, but no programming language can solve them.

I will give you an example. Write a program to compute the square root of any number.

Algorithm of Square Root

1. Define Value N

2. take a variable i

3. i = 1

4. While i*i is less than N

              Increment i

5. if successful, then “i is square root of N”

You can make an algorithm of square root but you cannot make a program that will give the square root of every input. In the algorithm, you just have to pass the logic. This is so simple but in a program that is syntactically and semantically correct, you cannot generate the output of every integer.

√4 is 2 ok, √9 is 3 ok, √16 is 4 ok but what about √5, √6, √7, and √8? 4, 9, 16, 25,… these are perfect square roots, but the square roots of non-perfect square roots cannot be generated exactly. There are some assumptions the √2 is equal to 1.414, and Hippasus discovered that square root of 2 is an irrational number, that is, he proved that the square root of 2 cannot be expressed as a ratio of two whole numbers. So computer program cannot do the justice with square root problem because it needs exactness but algorithms can give the logic.

“Every program is an algorithm” this statement is right but many times when we do coding, we have to make an empty program, so in that program, no algorithm is there

Post a Comment

0 Comments