-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreeNOne.cpp
executable file
·51 lines (43 loc) · 913 Bytes
/
ThreeNOne.cpp
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
40
41
42
43
44
45
46
47
48
49
50
/**
* Time a function to compute the 2n+1 function.
* Created: 15:41, 09/07/03
* Modified: 15:44, 09/07/03
*
*/
#include <iostream>
#include <iomanip>
#include <sys/timeb.h>
// #include <time.h>
using namespace std;
// returns the time in milliseconds.
long millis()
{
struct timeb t;
ftime(&t);
return 1000*t.time + t.millitm;
}
long twoNone(long n)
{
long rtval = 0;
while( n != 1 ) {
if( n % 2 == 0 )
n = n / 2;
else
n = 3 * n + 1;
rtval++;
}
return rtval;
}
int main()
{
const long step = 1000;
const long start = step;
const long limit = 100000;
for( long n = start; n < limit; n += step ) {
long t0 = millis();
long f = twoNone(n);
long t1 = millis();
cout << "[" << setw(7) << (t1-t0) << "] "
<< setw(6) << n << " " << setw(15) << f << endl;
}
}