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
Stuck?
Use the Get Hint button in the action bar to reveal a guided hint.
💬 Discussion
Loading...💬
No comments yet. Be the first to start the discussion!
💻 No output yet.
Click Run in the editor to execute your code with the custom input.
Sample cases (from the problem)
Sample Case 1
Input
3 24 36 60
Expected output
12
Solve it your way first
Community solutions unlock after you submit a passing solution. Don't peek.
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