From a62b2c2e39b83fa78f741233e820ccc53b2da96b Mon Sep 17 00:00:00 2001 From: KUchy Date: Wed, 11 Aug 2021 01:56:05 +0200 Subject: [PATCH] Adding collatz conjecture program --- .../threenplusone/threenplusone.cpp | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 CPP/miscelanious/threenplusone/threenplusone.cpp diff --git a/CPP/miscelanious/threenplusone/threenplusone.cpp b/CPP/miscelanious/threenplusone/threenplusone.cpp new file mode 100644 index 0000000..26b053b --- /dev/null +++ b/CPP/miscelanious/threenplusone/threenplusone.cpp @@ -0,0 +1,41 @@ +#include +#include + +int threeNLoop(int n) +{ + int steps = 0; + do{ + if(n % 2 == 1) + { + n = (3*n + 1) / 2; + steps += 2; + }else + { + n /= 2; + steps++; + } + }while(n != 1); + return steps; +} + +int main() +{ + int n; + std::cout << "Enter n (where the numbers checked wil be at most 2 ^ n) value:" << std::endl; + std::cin >> n; + int maxSteps = 0; + int maxN = 1; + for(int i = 1; i <= pow(2, n); i++) + { + int currentSteps = threeNLoop(i); + if(currentSteps > maxSteps) + { + maxSteps = currentSteps; + maxN = i; + } + + } + std::cout << "max steps: " << maxSteps << std::endl; + std::cout << "For n = " << maxN << std::endl; + return 0; +}