mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 14:43:01 +02:00
Adding collatz conjecture program
This commit is contained in:
parent
a1f18f5f00
commit
a62b2c2e39
41
CPP/miscelanious/threenplusone/threenplusone.cpp
Normal file
41
CPP/miscelanious/threenplusone/threenplusone.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user