forked from AllAlgorithms/cpp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz.cpp
More file actions
Latest commit
25 lines (23 loc) · 463 Bytes
/
Copy pathcollatz.cpp
File metadata and controls
25 lines (23 loc) · 463 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* Collatz sequenge
Author: Pablo Trinidad <github.com/pablotrinidad>
*/
#include<bits/stdc++.h>
usingnamespacestd;
voidcollatz(int n) {
while (n > 1) {
cout << n << "";
if (n % 2 == 0) {
n = n / 2;
} else {
n = (3 * n) + 1;
}
}
cout << n << endl;
}
intmain() {
int n;
cout << "Enter an integer n to compute the Collatz sequence: ";
cin >> n;
collatz(n);
return0;
}