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

tutorial6_7 (1)

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)
3 views

tutorial6_7 (1)

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/ 3

Data Structure & Algorithms 1 (DSA1)

Tutorial (week 6 & 7) - Modular Programming


2023 - 2024

03 November 2024

OBJECTIVES
Application of the modular approach:
• Breaking down tasks into distinct modules.

• Separate module design.


• Using Function and Procedure.
• Gaining a comprehension of the concepts of formal parameters and arguments, as well as the principals
of parameter passing (by value and by reference)

Exercise 1: Finding Aliquot Sequences and Identifying Number


Properties
Objective
If you take a natural number N > 1 and calculate the sum of its proper divisors (i.e., excluding itself), and
then repeat the same operation on this result, you obtain an aliquot sequence.
For instance, if N = 24, the obtained aliquot sequence is: N = 24, 36, 55, 17, 1. Generally, an aliquot
sequence stops when it reaches 1 (since 1 has no proper divisors). When the first calculated element is equal
to 1, N is a prime number. However, this is not always the case. Sometimes, the aliquot sequence is closed,
consisting of ’e’ calculated elements. In such cases, N is referred to as a sociable number of order ’e’.
Moreover, if ’e’ is equal to 1, N is a perfect number, and if ’e’ is equal to 2, N is an amicable number.

Definitions:
1. A closed aliquot sequence is a sequence in which the last element is equal to the first given element
of the sequence. Its order ’e’ is the number of calculated elements in the sequence (excluding the first
one).
2. A perfect number is a number for which the sum of its proper divisors is equal to the number itself.
3. Two numbers A and B are said to be amicable if the sum of the divisors of A is equal to B, and the
sum of the divisors of B is equal to A.
4. A prime number is a number that has only 1 and itself as divisors.

1
Examples:
1. N = 24: Aliquot sequence: 24, 36, 55, 17, 1. N is not prime.
2. N = 11: Aliquot sequence: 11, 1. N is a prime number.

3. N = 28: Aliquot sequence: 28, 28. N is sociable and a perfect number of order 1.
4. N = 220: Aliquot sequence: 220, 284, 220. N is sociable and an amicable number of order 2.
5. N = 12496: Aliquot sequence: 12496, 14288, 15472, 14536, 14264, 12496. N is sociable of order 5.

Task:
Write an algorithn to search for the aliquot sequence that starts with a given number N and detect the case
where N is prime. Additionally, in the case where N is sociable, determine that it is sociable, specify its
order ’e’, and verify if it is perfect or amicable.

Exercise 2: Number Base Conversion Algorithm


Objective:
Develop an algorithm to convert a number N from base B1 to base B2, where both B1 and B2 are integers
between 2 and 10. It is advisable to first convert N to its decimal (base 10) representation.

Task:
Write an algorithm to perform the following tasks:
1. Convert the number N from base B1 to its decimal (base 10) representation.

2. Convert the decimal representation obtained in step 1 to base B2.

Exercise 3: Generating the First 20 Gray Code Numbers


Objective:
The Gray code is widely used to solve puzzles, games, and various theoretical problems (operations research,
encoding, parallel computing, optimization, etc.). It is based on binary code where, to move from one number
to the next, only one bit is changed at a time, following specific rules:

• If the number of set bits (1s) is even, change the last bit.

• If the number of set bits is odd, find the rightmost set bit and change the bit to its left.

Gray Code Examples:


1. For N = 110101, with 4 set bits (which is even), the last bit is changed, resulting in the next number:
110100.
2. For N = 110100, with 3 set bits (which is odd), the rightmost set bit is found, and the bit to its left is
changed, leading to the next number: 111100.

2
Task:
Develop a solution that generates and displays the first 20 numbers encoded in Gray code.

Exercise 4: Number Operations and Fraction Reduction


Objective:
This exercise focuses on implementing and using functions and procedures to perform the following tasks:
1. Calculate the greatest common divisor (GCD) of two positive integers using Euclid’s algorithm.
2. Create a procedure named reduce to simplify fractions by dividing the numerator and denominator
by their GCD. Display ”error” for non-positive arguments and return the reduced fraction.
3. Develop an algorithm that prompts the user to input two numbers, calculates and displays their GCD,
determines and displays the smallest common multiple, and employs the reduce procedure to simplify
a given fraction.

Part 1: GCD Calculation


Write a function to find the greatest common divisor (GCD) of two positive integers, using Euclid’s algorithm.

Part 2: Fraction Reduction Procedure


Write a procedure named reduce that takes two positive integer arguments, ”num” and ”denom”, as the
numerator and denominator of a fraction, and reduces the fraction by dividing both numbers by their GCD.
If either argument is zero or negative, display ”error” to indicate failure to reduce the fraction. Otherwise,
return the new values of the numerator and denominator.

Part 3: Algorithm for User Input and Operations


Write an algorithm to:
1. Prompt the user to input two positive numbers, A and B.

2. Calculate and display the GCD of A and B.


3. Determine and display the smallest common multiple of A and B.
4. Call the reduce procedure to simplify the fraction (A/B) and display the reduced fraction.

You might also like