Recursive GCD of n numbers MEDIUM Recursivitate (BAC) Subprograme (BAC) 25 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Read a natural number `n` followed by `n` positive natural numbers. Print the greatest common divisor (GCD) of all `n` numbers, computed using **a recursive function** based on Euclid's algorithm. Define a recursive function `cmmdc(a, b)` with the relation: ``` cmmdc(a, 0) = a cmmdc(a, b) = cmmdc(b, a % b), for b > 0 ``` Then compute the GCD of all `n` numbers by successive reduction: start with the first number and combine it one by one with each of the remaining numbers using `cmmdc`.

Input format

Input input.txt

The first line contains the natural number `n` (the count of values). The second line contains `n` positive natural numbers separated by spaces. - `1 <= n <= 50` - `1 <= valori <= 1.000.000` - The solution must use recursion.

Output format

Output output.txt

Print a single natural number: the GCD of all `n` numbers, followed by a newline.

Example

input
3

24 36 60
output
12

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more