Repository navigation
Expand file tree
/
Copy pathCollatz_PartB.cpp
More file actions
152 lines (135 loc) · 3.57 KB
/
Copy pathCollatz_PartB.cpp
File metadata and controls
152 lines (135 loc) · 3.57 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
class Collatz_Number {
public:
long long num;
Collatz_Number(long long entered_number) {
num = entered_number;
}
int seq_length() {
int sequence_length = 0;
while (num != 1) {
sequence_length += 1;
if (num % 2 == 0) {
num = num / 2;
}
else {
num = (num * 3) + 1;
}
}
return sequence_length;
}
int seq_max() {
long long temp = num;
long long max = temp;
while (temp != 1) {
if (temp % 2 == 0) {
temp = temp / 2;
}
else {
temp = (temp * 3) + 1;
if (temp > max) {
max = temp;
}
}
}
return max;
}
//nearly obselete and could be combined with seq_length() however having seperate functions for seperate operations is nicer
};
void display_seq(int max_num) {
int count = 2;
int temp = 2;
while (count < max_num) {
count += 1;
cout << "\n\nNumber: " << temp << "\n";
while (temp != 1) {
if (temp % 2 == 0) {
temp = temp / 2;
cout << temp << " ";
}
else {
temp = (temp * 3) + 1;
cout << temp << " ";
}
}
temp = count;
}
}
bool is_valid_number(long long number_input) {
if (number_input == abs(number_input) && (number_input != 0) && number_input < 9223372036854775807) {
return true;
}
else {
cout << "Enter Positive Numbers Only Letters Will Be Ignored" << endl;
return false;
}
}
int main()
{
string menu_input;
long long number_input;
int max_num;
cout << "========== Collatz Number Calculator ==========\n\n";
while (true) {
menu_input = "";
cout << " MENU == MENU == MENU == MENU\n\nSelect Operation to Complete\n1. Compute Sequence Length\n2. Compute Highest Value Reached in Sequence\n3. Display Full Sequence (Limited Range)\n4. Quit\n";
cin >> menu_input;
if (menu_input == "1") {
cout << "\nEnter Number To Perform Operation On: ";
cin >> number_input;
if (is_valid_number(number_input)) {
Collatz_Number number(number_input);
cout << "Sequence Length: " << number.seq_length() << endl;
}
else {
cin.clear();
}
}
else if (menu_input == "2") {
cout << "\nEnter Number To Perform Operation On: ";
cin >> number_input;
if (is_valid_number(number_input)){
Collatz_Number number(number_input);
cout << "\nHighest Value Reached: " << number.seq_max() << endl;
cout << "Difference of Entered Number and Max Value: " << abs(number.seq_max() - number_input) << endl;
}
else {
cin.clear();
}
}
else if (menu_input == "3") {
while (true) {
cout << "Enter Number to Generate Sequence Up to: ";
cin >> max_num;
if (cin.fail() || max_num != abs(max_num)) {
cout << "Enter Positive Numbers Only\n";
cin.clear();
cin.ignore(INT_MAX, '\n');
}
else {
display_seq(max_num);
break;
}
}
}
else if (menu_input == "4") {
exit(0);
}
system("pause");
system("cls");
}
}
/*
Bibliography:
https://www.w3schools.com/cpp/cpp_classes.asp Classes
https://www.w3schools.com/cpp/cpp_class_methods.asp Methods
https://www.w3schools.com/cpp/cpp_constructors.asp Constructors
https://pvs-studio.com/en/blog/terms/0079/#:~:text=Use%20of%20uninitialized%20memory%20means,a%20so%20called%20%22heisenbug%22. Error with uninitialised memory
https://www.dcode.fr/collatz-conjecture Testing if progam matches with actual values
https://www.w3schools.blog/clear-screen-in-c-2 Clearing screen in console
https://www.youtube.com/watch?v=XRLLk7HyR3s cin.clear(), cin.ignore()
https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected cin.ignore()
*/