Given a positive integer n, the task is to find whether this number reaches 1 after performing the following two operations:-
- If n is even, then n = n/2.
- If n is odd, then n = 3*n + 1.
- Repeat the above steps, until it becomes 1.
For example, for n = 12, we get the sequence 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
Examples:
Input : n = 4
Output : Yes
Input : n = 5
Output : Yes
The idea is to simply follow given rules and recursively call the function with reduced values until it reaches 1. If a value is seen again during recursion, then there is a cycle and we can’t reach 1. In this case, we return false.
C++
#include<bits/stdc++.h>
using namespace std;
bool isToOneRec( int n, unordered_set< int > &s)
{
if (n == 1)
return true ;
if (s.find(n) != s.end())
return false ;
s.insert(n);
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;
}
|
Java
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" );
}
}
}
|
Python3
def isToOneRec(n: int , s: set ) - > bool :
if n = = 1 :
return True
if n in s:
return False
if n % 2 :
return isToOneRec( 3 * n + 1 , s)
else :
return isToOneRec(n / / 2 , s)
def isToOne(n: int ) - > bool :
s = set ()
return isToOneRec(n, s)
if __name__ = = "__main__" :
n = 5
if isToOne(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Boolean isToOneRec( int n, HashSet< int > 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< int > s = new HashSet< int >();
return isToOneRec(n, s);
}
public static void Main(String[] args)
{
int n = 5;
if (isToOne(n))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function isToOneRec(n, s)
{
if (n == 1)
{
return true ;
}
if (s.has(n))
{
return false ;
}
return (n % 2 == 1) ? isToOneRec(3 * n + 1, s)
: isToOneRec(n / 2, s);
}
function isToOne(n)
{
let s = new Set();
return isToOneRec(n, s);
}
let n = 5;
if (isToOne(n))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
The above program is inefficient. The idea is to use Collatz Conjecture. It states that if n is a positive then somehow it will reach 1 after a certain amount of time. So, by using this fact it can be done in O(1) i.e. just check if n is a positive integer or not.
Note that the answer would be false for negative numbers. For negative numbers, the above operations would keep number negative and it would never reach 1.
C++
#include<bits/stdc++.h>
using namespace std;
bool isToOne( int n)
{
return (n > 0);
}
int main()
{
int n = 5;
isToOne(n) ? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean isToOne( int n)
{
return (n > 0 );
}
public static void main(String[] args)
{
int n = 5 ;
if (isToOne(n) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python 3
def isToOne(n):
return (n > 0 )
n = 5
if isToOne(n) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isToOne( int n)
{
return (n > 0);
}
public static void Main()
{
int n = 5;
if (isToOne(n) == true )
Console.Write( "Yes" ) ;
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isToOne(n)
{
return (n > 0);
}
let n = 5;
if (isToOne(n) == true )
document.write( "Yes" ) ;
else
document.write( "No" );
</script>
|
PHP
<?php
function isToOne( $n )
{
if ( $n > 0)
return true;
return false;
}
$n = 5;
isToOne( $n )? print ( "Yes" ) : print ( "No" );
?>
|
Time complexity: O(1)
Auxiliary space: O(1)
We strongly recommend to refer below problem as an exercise:
Maximum Collatz sequence length
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!