-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (64 loc) · 1.48 KB
/
main.cpp
File metadata and controls
81 lines (64 loc) · 1.48 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
// Copyright (c) 2021 Futureblur. All rights reserved.
#include <iostream>
#define LOG(x) std::cout << x << std::endl
#define ADD_INPUT std::cout << "> "
static void clear();
static void start();
static void run(long base);
int main()
{
LOG("Welcome! This is a tiny program which simulates the collatz-conjecture. ");
LOG("For more information, please visit: https://simple.wikipedia.org/wiki/Collatz_conjecture\n\n");
LOG("Please enter any integer greater than 0 to start.\n");
start();
}
static void clear()
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
static void start()
{
ADD_INPUT;
long base;
std::cin >> base;
if (!std::cin)
{
LOG("This is not a valid integer. Please try again.");
clear();
start();
return;
}
if (base <= 0.0)
{
LOG("The number must be greater than 0.");
clear();
start();
return;
}
run(base);
}
static void run(long base)
{
LOG("Running simulation with integer " << base << ".");
uint32_t iterations = 0;
while (base != 1)
{
iterations++;
if (base % 2 == 0)
{
//Even
base /= 2;
}
else
{
//Odd
base = base * 3 + 1;
}
LOG(base);
}
LOG("Simulation finished.");
LOG("Total iterations: " << iterations);
LOG("Enter another number to retry.");
start();
}