Weird Algorithm
Next: missing-number
You are given a positive integer .
If is even, set ;
if is odd, set .
Repeat this process until .
For example, the sequence for is
Your task is to print every value of that appears during the algorithm.
Input
The only input line contains an integer .
Output
Print a single line containing all values of produced by the algorithm, separated by spaces.
Constraints
Answer
This is the collatz conjecture.
#include <iostream>
int main() {
long long n;
std::cin >> n;
std::cout << n << ' ';
while (n > 1) {
n = n % 2 == 0 ? n / 2 : (n * 3) + 1;
std::cout << n << ' ';
}
}
Next: missing-number