Program to implement Collatz Conjecture | Jav 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
import java . util . * ; class GFG { static boolean isToOneRec ( int n , HashSet < Integer > s ) { if ( n == 1 ) { return true ; } if ( s . contains ( n ) ) { return false ; } return ( n % 2 == 1 ) ? isToOneRec ( 3 * n + 1 , s ) : isToOneRec ( n / 2 , s ) ; } static boolean isToOne ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; return isToOneRec ( n , s ) ; } public static void main ( String [ ] args ) { int n = 5 ; if ( isToOne ( n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }