0% found this document useful (0 votes)
12 views4 pages

TD 01

The document contains 14 exercises proposing algorithms on various topics: 1) Converting a duration in seconds to hours, minutes and seconds 2) Solving equations and determining maximum/minimum values 3) Calculating electricity and gas costs based on consumption tranches 4) Displaying the season based on the month number 5) Calculations involving powers, products, sums, occurrences, etc. of numbers 6) Calculating the greatest common divisor of two numbers using subtraction or division 7) Calculating the product of two positive integers using Russian multiplication 8) Approximating the cube root of a number using Newton's method 9) Calculating the cosine of a number to a given order using only

Uploaded by

maloukdz32
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)
12 views4 pages

TD 01

The document contains 14 exercises proposing algorithms on various topics: 1) Converting a duration in seconds to hours, minutes and seconds 2) Solving equations and determining maximum/minimum values 3) Calculating electricity and gas costs based on consumption tranches 4) Displaying the season based on the month number 5) Calculations involving powers, products, sums, occurrences, etc. of numbers 6) Calculating the greatest common divisor of two numbers using subtraction or division 7) Calculating the product of two positive integers using Russian multiplication 8) Approximating the cube root of a number using Newton's method 9) Calculating the cosine of a number to a given order using only

Uploaded by

maloukdz32
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/ 4

University of Science and Technology Houari Boumediene

Faculty of computer science


Algorithmics and Data Structures INGENIEUR.INFO: 2023/2024

TD N° 1: Algorithmic Language

Exercise 1:

Propose an algorithm that takes a duration in seconds as input and converts it into hours,
minutes, and seconds for display purposes.

Exercise 2:

Set the flowchart, then the algorithm allowing to solve each of the following problems:
1- Solution in R of the first degree equation Ax+B=0 (A and B are real numbers).
2- The bill of a product depending on the sold quantity, knowing that the unit price depends
on the sold quantity as shown in the following table:

Quantity (Kg) Unit Price (DA)


Qte <10 10
10 <= Qte < 100 9
Qte ≥ 100 8

3- Determination of the Max of three integer numbers ( A , B , C ).


4- Ordering in decreasing order of three integers ( X , Y , Z ).
5- Solution of the second degree equation A x 2 + B x + C = 0 in the set of real numbers R.

Exercise 3:

Write an algorithm that calculates the total electricity and gas cost consumed by a
subscriber, given that SONELGAZ uses a rate (tariff) per tranche (part) represented by the
following table:

Electricity Gas
Tranche Quantity (KWH) Unit Price (DA) Quantité (TH) Unit Price (DA)
1 125 1.7787 1125 0.1682
2 125 4.1789 1375 0.3245
3 750 4.8120 4712 0.4025
4 THE REST 5.4796 THE REST 0.4599

Exercice 4:
Write an algorithm which displays the season according to the month number.
Exercice 5:
Write an algorithm to solve each of the following problems:
1- Calculation of the Nth power (N >= 0) of a strictly positive real number.
2- Calculation of the product of two strictly positive integers using only the addition
operation.
3- Calculation of the number of positive values, number of negative values, the sum of
negative values, the sum of positive values and the number of null values in a suite of N
integers.
4- Search for the minimum and the maximum of a set of de N real numbers.
5- Calculation of the number of occurrences of a given value VAL in a sequence of N real
numbers.
6- Calculation of the quotient and the remainder of the division of two integers A and B
without using the division operation (we assume we don’t have operations Mod and Div).
7- Calculation of the number of occurrences of the characters 'A' and 'a' in a sequence of
characters.
8- Full alphabet display ('A' to 'Z').
9- Display of the number of upper case letters and lower case letters from a sequence of
characters ending by the character '#'.
10- Checking if two integers X and Y strictly positive integers are friends.
11- Checking if a positive integer X is automorphic or not.
12- Verification if a positive integer X is prime or not.
13- Calculation of the sum of semi-prime numbers in the range from 1 to N.
14- Calculation of the sum of perfect numbers between 10 and N.
15- Calculation of the sum of the digits of a positive integer A.
16- Calculation of the occurrence number of the digit C (0 ≤ C < 10) inside a positive integer
A.
17- Display the Mirror of a positive integer A. (Example : Mirror of 26538 = 83562)

Exercise 6:
Write an algorithm which determines the GCD (Greater Common Divisor) of two integer
numbers using the following methods:

Méthod 1 : successive substractions. Méthod 2 : euclidian division.


GCD ( 3465 , 1575 ) GCD ( 7038 , 5474 )

A B Reste A B Reste
3465 - 1575 = 1890 7038 / 5474 1564
1890 - 1575 = 315 5474 / 1564 782
1575 - 315 = 1260 1564 / 782 0
1260 - 315 = 945
945 - 315 = 630
630 - 315 = 315 3465 / 1575 315
315 - 315 = 0 1575 / 315 0

Exercise 7:
Write an algorithm to calculate the product of two positive integers using the Russian
multiplication method:

For A=9 and B=25 we have:


A B PRODUCT
9 25 0
8 25 25
4 50 25
2 100 25
1 200 25
0 200 225

Exercise 8:
Write an algorithm to calculate the cube root of a given number A using Newton's
approximate calculation method defined by the recurrence formula below:

𝑋𝑛+1 =
1
3 ( 𝐴
𝑋𝑛
2 − 2 * 𝑋𝑛 )
Where 𝑋0 = 1
The calculation is stopped when the absolute value: |𝑋𝑛+1 − 𝑋𝑛| is lower than
−3
𝐸𝑃𝑆𝐼𝐿𝑂𝑁 = 10 .

Exercise 9:
Write an algorithm to calculate the cosine cos(x) of order n of a given real number x defined
as follows, using only the basic operators (without the use of the power operator):
2 4 6 2𝑛
𝑥 𝑥 𝑥 𝑥
cos(𝑥) = 1 − 2!
+ 4!
− 6!
+ …± (2𝑛)!
With 𝑛≥5

Exercise 10:
Write an algorithm to calculate the sum S(n) of order n defined as follows:
𝑆(𝑛) = 1! + 4! + 7! + 10! + … + (3𝑛 + 1)!

Write an algorithm to calculate the difference between S(n1) and S(n2) with 2 < 𝑛1 < 𝑛2

Exercise 11:
Write an algorithm to calculate the sum S(n) of order n defined as follows, using only the
basic operators (without the use of the power operator):
𝑖=𝑛 𝑖+1
(−1)
𝑆(𝑛) = ∑ 𝑖
𝑖=0 𝑥

Exercise 12:
Write an algorithm to calculate the sum S(n) of order n defined as follows:
𝑖=𝑛
𝑖 4 5 𝑛
𝑆(𝑛) = ∑ [(𝑖 − 3) * (𝑖 + 4)] = (1 * 8) + (2 * 9) + … + [(𝑛 − 3) * (𝑛 + 4)]
𝑖=4
With 𝑛 > 3
Exercise 13:
Write an algorithm allowing you to calculate the Nth term of the sequence (UN) defined as
follows:
𝑈𝑁 = 𝑈𝑁−1 + 𝑈𝑁−2

Where 𝑈 = 1, 𝑈 = 1, 𝑁 > 2
1 2

Exercise 14:
Write an algorithm to calculate the Nth term of the sequences (UN) and (VN) defined as
follows:
𝑈𝑁 = 2𝑈𝑁−1 − 𝑈𝑁−2
𝑉𝑁 = 𝑉𝑁−1 − 𝑈𝑁

Where 𝑈 = 1, 𝑈 = 2, 𝑉 = 3, 𝑁 > 2 .
1 2 2

Definitions:
- Two integers are considered friends if the sum of the divisors of the first number (other
than itself) is equal to the second number, and the sum of the divisors of the second
number (other than itself) is equal to the first number.
- A positive integer X is automorphic if its square X2 ends in X.
- A semi-prime number is equal to the product of two different prime numbers.
- A positive integer X is perfect if it is equal to the sum of its divisors < X.

You might also like