Program to implement Collatz Conjecture | C ++ program to implement Collatz Conjecture ; Function to find if n reaches to 1 or not . ; If there is a cycle formed , we can 't r  reach 1. ; If n is odd then pass n = 3 n + 1 else n = n / 2 ; Wrapper over isToOneRec ( ) ; To store numbers visited using recursive calls . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isToOneRec ( int n , unordered_set < int > & s ) { if ( n == 1 ) return true ; if ( s . find ( n ) != s . end ( ) ) return false ; return ( n % 2 ) ? isToOneRec ( 3 * n + 1 , s ) : isToOneRec ( n / 2 , s ) ; } bool isToOne ( int n ) { unordered_set < int > s ; return isToOneRec ( n , s ) ; } int main ( ) { int n = 5 ; isToOne ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }