Algorithm examples

Let's take a look at several algorithm examples, while solving various problems.

Find the sum of two numbers N and M

Advertise on this site. I promise you will like the rates :)

The procedure is:

  1. Enter the two numbers in the variables N and M.
  2. Sum them and save the result in the variable sum.
  3. Output the result.
Sum two numbers

Obviously, this is a very simple example. If you are serious about learning algorithms, have a look at the Introduction to algorithms book.

Find if a given number “n” is odd or even

A number is even if it can be divided by 2 without remainder. Such numbers are 2, 4, 6, 8.. and so on. The numbers that leave a remainder are called odd. They are 1, 3, 5, 7.. and so on. In programming we find the remainder of a division with the operator %. Also we use the double equals “==” to compare values for equality. You can read more about operators in the math operators lesson.

Even or odd number

Euclidean algorithm

The Euclidean algorithm is a method to find the greatest common divisor (GCD) of two numbers. It repeatedly subtracts smaller numbers from larger numbers until you find their common divisor.

We use this algorithm as part of our C example of converting a decimal number to a fraction.

Here's how it works:

  1. Take Two Numbers: Let's say you have two numbers, a and b, for which you want to find the GCD.
  2. Divide and Get Remainder: You divide the larger number by the smaller number and find the remainder. So, a is divided by b, and you get a quotient q and a remainder r. Mathematically, it's a = b * q + r.
  3. Replace and Repeat: Now, instead of working with the original a and b, you replace a with b and b with r. You repeat the division and remainder process.
  4. Keep Repeating: You keep doing this division and remainder process until the remainder becomes zero. When the remainder becomes zero, the last non-zero remainder is the GCD of the two numbers!

The reason this works is that if you have two numbers a and b, and you subtract the smaller number (b) from the larger number (a), the remainder (r) is the part that's left after you've taken away as many multiples of b from a as you can. This remainder is the key, and it will keep getting smaller until you can't subtract any more multiples of b, and that's exactly when r becomes zero. At that point, the largest number that divides both a and b without leaving any remainder is the last non-zero remainder you had.

In simpler terms, you're playing a game of "keep subtracting the smaller number from the larger number until you can't anymore." And the last number you subtract before you can't play the game anymore is the GCD!

Find the sum of the first 50 numbers

Summing two numbers was easy – the calculation was one block from the flow chart. But how about 50? Do you have to write 50 blocks to solve this task? Happily – no!
You can automate this process by repeatedly incrementing the value of a variable and checking it every time if it exceeds the last value – 50. Then sum that number every step and... there you go! This construction is called a loop. Learn more about loops in the lesson from the beginners programming tutorial.

Long division (non-programming algorithm example)

A very common algorithm example from mathematics is the long division. Rather than a programming algorithm, this is a sequence that you can follow to perform the long division. For this example we will divide 52 by 3.

  1. Take the most significant digit from the divided number( for 52 this is 5) and divide it by the divider.
  2. Write the result as as a first digit of the end result.(5 / 3 = 1, so we write 1)
  3. Multiply the remainder by the weight of the divided digit. ( 5/3 = 1 and a remainder 2. 5 has a weight of 10 in 52, so we have 2 * 10).
  4. Sum the result from 3. with the next most significant digit and continue with this number from step 1. Repeat the steps until no more digits remain from the divided number.

The result from 3. is 20. The next digit in 52 is 2. So.. 20 + 2 = 22
    1. 22 / 3 = 7
    2. 17. Since this is the last digit 17 is the final answer. If you continue the division you will find the fractional part.

Let's go a step higher and continue with some more complex algorithm examples.

Find the biggest of 100 prices and reduce it by 10%

Given is the array prices with 100 elements(prices[100]). The problem consists of two parts

  1. Find the highest price in the array
  2. Reduce that price

For part 1 we iterate through the whole array, starting with index 0. We compare the first value with the next prices and when a greater price is found, we remember the new value in the variable “max” and its location in “maxIndex”.

Once we compared all elements of the array we have to reduce the max price with 10%. This is the same as multiplying it by 0.9, so that is what you see in the algorithm.
The last note here – we use short version of the multiply-assign operator:

  prices[maxIndex] *= 0.9
  is the same as
  prices[maxIndex] = prices[maxIndex] * 0.9

Reduce max price in array

Output the numbers x,y,z in descending order.

The last of the algorithm examples will be more branched. As you will see, we will need to do several consecutive examinations and this will spread our flow chart a bit.

Output numbers in ascending order

See also:

   Search this site: