-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz.cpp
More file actions
39 lines (36 loc) · 913 Bytes
/
Copy pathcollatz.cpp
File metadata and controls
39 lines (36 loc) · 913 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* TODO: write a program to test the collatz conjecture. This conjecture
* considers the following process:
* Start with any (positive) integer n and then transform it as follows:
* -- if n == 1, stop
* -- if n is even, divide it by 2 (n --> n/2)
* -- if n is odd, multiply b 3 and add 1 (n --> 3n+1)
* Conjecture: this process always stops after a finite number of steps,
* no matter what value of n you start with.
*
* Your program should work as follows:
* 1. read an integer
* 2. see how many steps the above process takes before you hit 1
* 3. print # of steps
* 4. go back to (1) until stdin has no more integers to read.
* */
#include <iostream>
using std::cin;
using std::cout;
int main()
{
int n;
while (cin>>n) {
int steps =0;
while (n!=1) {
if (n%2==0) {
n=n/2;
}else {
n=3*n+1;;
}
steps++;
}
cout<< steps <<std::endl;
}
return 0;
}
// vim:foldlevel=2