From f3236fe5c2d5161a6c6581a44485ee77f17f829c Mon Sep 17 00:00:00 2001 From: lostybtw <58177990+lostybtw@users.noreply.github.com> Date: Sun, 2 Oct 2022 04:45:46 +0000 Subject: [PATCH] Added Collatz Conjecture --- cpp/3x+1.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cpp/3x+1.cpp diff --git a/cpp/3x+1.cpp b/cpp/3x+1.cpp new file mode 100644 index 0000000..40e0ba9 --- /dev/null +++ b/cpp/3x+1.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +string collatz_conjecture(int number){ + string out; + + out += to_string(number); + out += " "; + + while(number != 1){ + if (number % 2 != 0){ + number = 3 * number + 1; + out += to_string(number); + out += " "; + } + + + if (number % 2 == 0){ + number = number/2; + out += to_string(number); + out += " "; + } + } + + return out; +} + +int main(){ + cout << collatz_conjecture(7); + return 0; +}