0% found this document useful (0 votes)
147 views

Algorithm MYP 5

The document describes several algorithms including bubble sort, Euclidean algorithm, square root approximation, least common multiple calculation, decimal to binary conversion, Fibonacci sequence, factorial calculation, and primality test.

Uploaded by

shaurya.sanyal
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)
147 views

Algorithm MYP 5

The document describes several algorithms including bubble sort, Euclidean algorithm, square root approximation, least common multiple calculation, decimal to binary conversion, Fibonacci sequence, factorial calculation, and primality test.

Uploaded by

shaurya.sanyal
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/ 11

Algorithm

Algorithm: Euclidean Algorithm (Subtraction Method) for GCD


Algorithm: Newton-Raphson Square Root Approximation
Algorithm: Least Common Multiple (LCM) Calculation
Algorithm: Decimal to Binary Conversion
Algorithm: Fibonacci Sequence
Algorithm: Bubble Sort

Input: A list of n numbers.

Start from the beginning of the list and compare each pair of adjacent elements.

If the elements are in the wrong order, swap them.

Continue iterating through the list, comparing and swapping adjacent elements until
no more swaps are needed.

Repeat steps 1-3 until the entire list is sorted.

Let's use this algorithm with a simple list: [5, 2, 9, 1, 5].

Initial list: [5, 2, 9, 1, 5].

Pass 1: Compare and swap (5, 2), (5, 9), (9, 1), (9, 5). Updated list: [2, 5, 1, 5, 9].

Pass 2: Compare and swap (2, 5), (5, 1), (5, 9). Updated list: [2, 1, 5, 5, 9].
Pass 3: Compare and swap (2, 1), (2, 5), (5, 5). Updated list: [1, 2, 5, 5, 9].

No more swaps are needed, and the list is sorted.

The sorted list is [1, 2, 5, 5, 9]. Bubble Sort is a simple sorting algorithm, and while it
may not be the most efficient for large datasets, it is well-defined and demonstrates
the concept of analyzing and using well-defined procedures for solving problems.

Algorithm: Euclidean Algorithm for GCD

Input: Two positive integers, a and b.

Set r = a % b (where % denotes the modulus operator, i.e., the remainder when a is
divided by b).

If r = 0, then the GCD is b. Return b.

Set a = b and b = r.

Repeat steps 1-3 until r becomes 0.

The GCD is the last non-zero remainder, which is b.

Let's use this algorithm with specific numbers, a = 48 and b = 18:

Initial values: a = 48, b = 18.

Iteration 1: r = 48 % 18 = 12. Set a = 18, b = 12.

Iteration 2: r = 18 % 12 = 6. Set a = 12, b = 6.

Iteration 3: r = 12 % 6 = 0. Since r = 0, the algorithm stops.

The GCD is the last non-zero remainder, which is 6.

Therefore, the GCD of 48 and 18 is 6. This algorithm is well-defined, meaning that it


follows a clear set of instructions and will always produce the correct result for any
pair of positive integers.

Algorithm: Babylonian Method for Square Root


Algorithm: Factorial Calculation
Algorithm: Primality Test

You might also like