-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollatz.txt
103 lines (89 loc) · 1.96 KB
/
Collatz.txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// ----------------------------
// projects/collatz/Collatz.c++
// Copyright (C) 2013
// Glenn P. Downing
// ----------------------------
// --------
// includes
// --------
#include <cassert> // assert
#include <iostream> // endl, istream, ostream
#include <string> //for testing purposes
#include <stdio.h>
using namespace std;
#include "Collatz.h"
// ------------
// collatz_read
// ------------
int cache [50];
bool collatz_read (std::istream& r, int& i, int& j) {
r >> i;
if (!r)
return false;
r >> j;
assert(i > 0);
assert(j > 0);
return true;}
// ------------
// collatz_eval
// ------------
int collatz_eval (int i, int j) {
assert(i > 0);
assert(j > 0);
printf("pooop");
int max = 1;
int temp;
if(i>j)
{
temp = i;
i = j;
j = temp;
}
for(int a = i; a <= j; a++)
{
int count = 1;
//string q("hello");
int loca = a; //I am so fiching high right now I just don't even know what to do twith myselfl. I might pass out soon frim all of these crazy drugs Taylor just gave me.
//cout << "cache[" << a << "]: " << cache[a] << endl;
if(cache[a] != 0) //use cache if available
count = cache[a];
else //otherwise, find cycle length
{
while (loca!=1)
{
if(loca%2 == 1)
{
loca = loca + (loca>>1) + 1; //odd: (3n+1)/2, 2 steps
count += 2;
}
else
{
loca = loca/2; //even: n/2, 1 step
count++;
}
}
cache[a] = count;
}
if (count > max)
max = count;
assert(max > 0);
}
return max;
}
// -------------
// collatz_print
// -------------
void collatz_print (std::ostream& w, int i, int j, int v) {
assert(i > 0);
assert(j > 0);
assert(v > 0);
w << i << " " << j << " " << v << std::endl;}
// -------------
// collatz_solve
// -------------
void collatz_solve (std::istream& r, std::ostream& w) {
int i;
int j;
while (collatz_read(r, i, j)) {
const int v = collatz_eval(i, j);
collatz_print(w, i, j, v);}}