Contenus

Toggle## Exercises corrected using LARP software

The following corrected exercises concern the creation of algorithms according to the flowchart structure of the LARP software.

## To get started

Write the flowchart allowing the following operation:

- A gate opens using a remote control.
- The system uses an infrared sensor to know if the car has passed.
- The barrier closes when the car has passed
- A rotating beacon turns on when the barrier is open and turns off when the barrier is closed.

Uses only the actions and events listed below:

## Exercise 1

**Challenge 1:** find the center of a circle. Write the flowchart of operations for finding the center of a circle. It is not forbidden to give a name to your algorithm.

**Challenge 2:** Divide a circle into 6 equal parts. Write the flowchart of operations allowing you to divide a circle into 6 equal parts. It is not forbidden to use an existing algorithm if it has a name; We can then call it by its name: Execute “myAlgo”.

Challenge 1:

Challenge 2:

## Exercise 2

An insurance company offers its customers four rates identifiable by color, from least to most expensive: white, green, orange, red.

The price depends on the driver's situation:

- A driver under 25 years of age and holding a license for less than two years is assigned the red rate, provided he has never been responsible for an accident. Otherwise, the company refuses to insure it.
- A driver under 25 years old and holding the license for more than two years, or over 25 years old but holding the license for less than two years is entitled to the orange rate if he has never caused an accident, at the red rate for an accident, otherwise it is refused.
- A driver over 25 years old who has held a license for more than two years benefits from the green rate if he is not the cause of any accident and the orange rate for one accident, the red rate for two accidents, and refused beyond.

## Exercise 3

Establish the pseudo code of the algorithm meeting the following need:

The algorithm starts by asking you for the amount of the initial loan (at zero interest).

Then it records the amount of your successive repayments.

With each repayment, the algorithm sends you information: the amount of capital remaining to be repaid.

BEGINNING

QUERY “borrow amount?” " , E

WHILE E>0 DO

REQUEST “reimbursement amount?” » ,R

IF (ER >=0) THEN

E = ER

WRITE “amount remaining to be repaid => “, E

OTHERWISE

WRITE “your refund is too high”

WRITE “maximum value => “, E

END IF

FINTANTQUE

WRITE “end of reimbursement”

END

## Exercise 4

**Challenge 1:** Write the flowchart of an algorithm that asks the user for a number and then displays its absolute value. Try it with positive and negative numbers (handle the case of value 0)

**Challenge 2:** Write pseudo-code for an algorithm that asks the user for two different numbers and then displays the larger one. Try it with pairs of numbers covering all possible cases.

**Challenge 3:** Write the flowchart of an algorithm which asks the user for 4 integers a, b, c, d. The algorithm must give the solution to the equation ax + b = cx + d. Or inform the user of special situations for solving the equation. Test the algorithm in the various possible cases.

**Challenge 4:** Write the pseudo-code of an algorithm that asks the user for 3 integers a, b, c. The algorithm must give the real solution(s) of the equation ax^{2} + bx + c = 0. Test in various possible situations.

**Challenge 5:** Write pseudo-code for an algorithm that displays the list of squares of numbers as long as the square is less than 100

**Challenge 6: **Write the pseudo-code of an algorithm that hasdisplays the list of integers multiples of 3

**Challenge 7: **Write the pseudo-code of an algorithm that hasdisplays the list of integers multiples of 3 and 5

Challenge 1:

BEGINNING

QUERY “enter a number?” " , NOT

IF N>0 THEN

val_abs = N

OTHERWISE

val_abs = -N

END IF

WRITE “the absolute value of “,N,” is “,val_abs

END

Challenge 2:

BEGINNING

QUERY “a first number?” » , N1

QUERY “a second number?” » , N2

IF N1>N2 THEN

WRITE N1,” is greater than “,N2

OTHERWISE

WRITE N2,” is greater than “,N1

END IF

END

Challenge 4:

BEGINNING

QUERY “enter the coefficient a > “, a

QUERY “enter the coefficient b > “, b

QUERY “enter the coefficient c > “, c

d = b*b – 4*a*c

IF d >0 THEN

R1 = (-b + ROOT(d)) / (2*a)

R2 = (-b + ROOT(d)) / (2*a)

WRITE” there are two roots: “,R1,” and “,R2

ELSE IF d=0 THEN

R = (-b) / (2*a)

WRITE “there is only one root: “, R

ELSE IF d <0 THEN

WRITE "there is no real root"

END IF

END

Challenge 5:

BEGINNING

number =1

WHILE (number * number <100) DO

WRITE number, » «, number*number

number = number + 1

FINTANTQUE

END

## Exercise 5

The computer chooses a mystery number between 1 and 100. And you will have to find it.

To allow the computer to have a random number between two integer values and thus make a suggested answer, you can use the following function:

number = RANDOM (p, q) to obtain a random number in the interval [p, q]

Establish the flowchart or the pseudo code (or both…) of an algorithm which allows the computer to respond to your number proposals.

“Too small” message if your number is smaller than the mystery number

“Too big” message if your number is larger than the mystery number

“Won” message if your number is the mystery number

Test your algorithms.

## Exercise 6

**Challenge 1:** Write the organization chart of a program that

- Request notes (number of notes unknown)
- Store them in a container

The program stops asking for ratings when it receives a negative rating.

Afterwards

- It calculates the average of the marks
- It displays the number of notes entered and the average of these notes

Write the flowchart, try it, then translate it into pseudo code

**Challenge 2:** To obtain random integer values use the RANDOM (p) function which provides a random integer in the interval ]0, p].

Value = RANDOM (p)

Once the container is filled, the program will display the contents of the container: position and value

**Challenge 3:** Write a program in pseudo-code that

- Fills a container with random values (see challenge 2)
- Find the smallest value in the container
- Shows the message “the smallest value is” and the value found

**Challenge 4:** Write the algorithm for calculating the factorial n! Choose the representation of your choice and try your algorithm with varied values (1, 3.6, 12, 0,-4)

**Challenge 5:** GCD of a and b by Euclid's algorithm

- Ask for the two numbers a and b
- Perform the Euclidean division of a by b; we note r the result of this division
- a takes the value of b and b takes the value of r
- we start again in 2
- when the decision gives a remainder equal to 0 the PGCD is the last non-zero remainder
- Then display the PGCD

Write your algorithm and test it for various pairs of values (a,b)

Question: what happens if the remainder of a division is 1?

Correct and adapt your algorithm to take this case into account

**Challenge 6:** Verifying the Syracuse conjecture

- Take a whole number
- If this number is even, divide it by 2
- If this number is odd, take the triple and add 1
- We get a new integer and we start again at 2.

At the end we always get 1.

Write this algorithm and get that at the end (when we get 1) it displays the number of iterations performed.

**Challenge 7:** Calculate by framing a value approximated to 10^{-6} of the golden ratio. Using the formula: The value between two successive values of the golden ratio calculation must be less than 10

**Challenge 9: ** Check if an integer requested by the algorithm is prime or not. The algorithm must answer: the number is prime or the number is not prime. A hint: a number n is prime if it has exactly 2 divisors in the interval [1, n]

**Challenge 10: ** Check if an integer requested by the algorithm is prime or not. The algorithm must answer: the number is prime or the number is not prime. Same question but find another approach for another algorithm. A clue: a number n is prime if there exists a divisor for this number in the interval ]1, n[

**Challenge 11: **reflection on prime number algorithms

Which of these two algorithms do you think is more efficient and why?

Challenge 4:

ENTER n

fact = 1

IF (n!=0) THEN

FOR counter = 1 UNTIL n DO

facto = facto*counter

FINFOR

OTHERWISE

fact = 1

END IF

RETURN facto

Challenge 5:

Challenge 6:

Challenge 9:

Challenge 10:

## Exercise 7

A study carried out by a polling institute showed that since 2001.

5% members of party A leave that party for party B.

8% members of party B leave this party for party A.

In 2001, party A had 40,000 members and party B had 70,000.

Establish the flowchart of an algorithm which will make it possible to determine in which year the number of members of party B will exceed that of party A.

## Exercise 8

**Challenge 1:** Establish the flowchart of an algorithm that requires polar coordinates and converts to Cartesian coordinates. We will limit ourselves to plane coordinates, but if that tempts you, ... why not in 3D.**Challenge 2:** Establish the flowchart of an algorithm that requires Cartesian coordinates and converts to polar coordinates. Same for 3D.**Challenge 3:** Write the organization chart of a program that

Ask what type of conversion you want to perform (P to C or C to P)

Run the flowchart for challenge 1 or u2 depending on the answer to the initial question.

**Challenge 4:** Complete the previous flowchart so that the execution of the algorithm asks after the conversion has been carried out, if you want to start again and starts again as long as the answer is yes.**Challenge 5:** In arithmetic, a perfect number is a natural number *not* such that σ(*not*) = 2*not* where σ(*not*) is the sum of the positive divisors of *not*. This amounts to saying that a natural number is perfect if it is equal to half the sum of its divisors or even to the sum of its strict divisors. So 6 is a perfect number because 2 × 6 = 12 = 1 + 2 + 3 + 6, or 6 = 1 + 2 + 3

Write the organization chart of a program that

Finds and displays prime numbers starting from 1 inclusive.

For information :

There are 4 perfect numbers between (1 and 10,000) known since antiquity (Euclid)

Then: 33,550,336, then…. 2 305 843 008 139 952 128 (discovered by Leonhard Euler), then others. In total today 49 including the last discovered in January 2016.**Challenge 6:** Write pseudo code, a program that

Performs the sum of integers: ∑ n for n equal to 1 up to + ∞

Displays for each value of n the value of the sum obtained

Test your program for as large a value of n as possible. Your computer's processing capacity should limit you.

Evaluate the limit of this sum for n tending towards + ∞ and verify Ramanujan’s summation:

1 + 2 + 3 + 4 + 5 +…..to infinity = -1/12.**Challenge 7:** Write an algorithm in pseudo-code that

Calculation of an approximate value of √150 at 5

^{th}decimal (x_{ n + 1}-x_{ not}<10^{-5})Use the x formula

_{n + 1}= (x_{ not}+a/x_{ not})/2

**Challenge 8:** Write an algorithm in pseudo-code that

Requests the value of an initial investment then calculates and displays the change in capital year after year at a rate of 3% per year

The initial investment is made at the beginning of the year (on 1

^{er}January)The capital assessment and interest payment is done at the end of the year (December 31)

**Challenge 9:** Write an algorithm in pseudo-code that

Determine the last value n of powers of 2

^{not}such as 2^{not}<10,000

Challenge 5:

Which gives the following algorithm:

Challenge 7:

BEGINNING

QUERY “value of a = “, number

One = number

precision = 0.00001

counter = 0

REPEAT

U = One

One = (U+number/U)/2.0

deviation = ABSOLUTE (Un-U)

counter = counter +1

UNTIL (deviation < precision)

WRITE “the root of “,number,” is “,ROUND(One)

WRITE “result obtained in “, counter, “loop turns”

END

Challenge 8:

BEGINNING

T = 3/100

YEAR = 0

QUERY “investment amount > “, P

Pini = P

REPEAT

P = P + (P * 3/100)

YEAR = YEAR+1

WRITE “Year: “,YEAR,” placement = “,P

UNTIL p >= 2*Pini

WRITE “Goal achieved in “, YEAR,” “years”

END

## Exercise 9

A set of matches is spread out (for example 17), each player removes 1 to 3 matches in turn. The winner is the one who removes the last match (Nim game).

The winning strategy is to leave your opponent with a number of matches equal to 1modulo 4.

Write the flowchart of the game algorithm in which

- The computer asks for the number of matches in the game
- Makes the human player play
- Takes a turn by removing
- The number of matches needed to leave 1 modulo 4 if possible
- A random number between 1 and 3 otherwise
- And so on until there is only one match left
- The program then chooses the winner

To allow the computer to have a random number between two integer values and thus make a suggested answer, you can use the following function:

number = RANDOM (p,q) to obtain a random number in the interval [p, q]

IN CONSTRUCTION