Finding square root of a number
Square root of 4 is 2, square root of 9 is 3, square root of 16 is 4
START
Step 1 → Define value n to find square root of
Step 2 → Define variable i and set it to 1 (For integer part)
Step 3 → Define variable p and set it to 0.00001 (For fraction part)
Step 4 → While i*i is less than n, increment i
Step 5 → Step 4 should produce the integer part so far
Step 6 → While i*i is less than n, add p to i
Step 7 → Now i has the square root value of n
STOP
The pseudocode of this algorithm can be derived as follows −
procedure square_root( n )
SET precision TO 0.00001
FOR i = 1 TO i*i < n DO
i = i + 1
END FOR
FOR i = i - 1 TO i*i < n DO
i = i + precision
END FOR
DISPLAY i AS square root
end procedure
Generating prime numbers
Following is the algorithm to find all the prime numbers less than or equal to a given integer n by
Eratosthenes’ method:
1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
2. Initially, let p equal 2, the first prime number.
3. Starting from p2, count up in increments of p and mark each of these numbers greater
than or equal to p2 itself in the list. These numbers will be p(p+1), p(p+2), p(p+3), etc..
4. Find the first number greater than p in the list that is not marked. If there was no such
number, stop. Otherwise, let p now equal this number (which is the next prime), and
repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked are prime.
Explanation with Example:
Let us take an example when n = 50. So we need to print all print numbers smaller than
or equal to 50.
We create a list of all numbers from 2 to 50.
According to the algorithm we will mark all the numbers which are divisible by 2 and are
greater than or equal to the square of it.
Now we move to our next unmarked number 3 and mark all the numbers which are
multiples of 3 and are greater than or equal to the square of it.
We move to our next unmarked number 5 and mark all multiples of 5 and are greater than
or equal to the square of it.
We continue this process and our final table will look like below:
So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47.
Finding the maximum number in a set
To find the maximum value, you initialize a placeholder called max with the value of the first
element in the array. Then you go through the array element by element. If any element is greater
than max you replace max with that element. Here is the pseudocode:
SET Max to array[0]
FOR i = 1 to array length - 1
IF array[i] > Max THEN
SET Max to array[i]
ENDIF
ENDFOR
PRINT Max
Find Kth Smallest or Largest element in an Array
int[] arrA = { 2, 3, 11, 16, 27, 4, 15, 9, 8 };
Output: The 4th smallest element is : 8
In this technique we select a pivot element and after a one round of operation the pivot
element takes its correct place in the array.
Once that is done we check if the pivot element is the kth element in array, if yes then
return it.
But if pivot element is less than the kth element, that clearly means that the kth element is
on the right side of the pivot. So make a recursive call from pivot+1 to end.
Similarly if pivot element is greater than the kth element, that clearly means that the kth
element is on the left side of the pivot. So make a recursive call from start to pivot-1.