Program to implement Collatz Conjecture
Last Updated :
07 Jan, 2024
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 : YesInput : 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++
// C++ program to implement Collatz Conjecture
#include<bits/stdc++.h>
using namespace std;
// Function to find if n reaches to 1 or not.
bool isToOneRec(int n, unordered_set<int> &s)
{
if (n == 1)
return true;
// If there is a cycle formed, we can't r
// reach 1.
if (s.find(n) != s.end())
return false;
s.insert(n);//inserting elements to the s
// If n is odd then pass n = 3n+1 else n = n/2
return (n % 2)? isToOneRec(3*n + 1, s) :
isToOneRec(n/2, s);
}
// Wrapper over isToOneRec()
bool isToOne(int n)
{
// To store numbers visited using recursive calls.
unordered_set<int> s;
return isToOneRec(n, s);
}
// Drivers code
int main()
{
int n = 5;
isToOne(n) ? cout << "Yes" : cout <<"No";
return 0;
}
Java
// Java program to implement Collatz Conjecture
import java.util.*;
class GFG
{
// Function to find if n reaches to 1 or not.
static boolean isToOneRec(int n, HashSet<Integer> s)
{
if (n == 1)
{
return true;
}
// If there is a cycle formed, we can't r
// reach 1.
if (s.contains(n))
{
return false;
}
// If n is odd then pass n = 3n+1 else n = n/2
return (n % 2 == 1) ? isToOneRec(3 * n + 1, s)
: isToOneRec(n / 2, s);
}
// Wrapper over isToOneRec()
static boolean isToOne(int n)
{
// To store numbers visited using recursive calls.
HashSet<Integer> s = new HashSet<Integer>();
return isToOneRec(n, s);
}
// Drivers code
public static void main(String[] args)
{
int n = 5;
if (isToOne(n))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 program to implement Collatz Conjecture
# Function to find if n reaches to 1 or not.
def isToOneRec(n: int, s: set) -> bool:
if n == 1:
return True
# If there is a cycle formed,
# we can't reach 1.
if n in s:
return False
# If n is odd then pass n = 3n+1 else n = n/2
if n % 2:
return isToOneRec(3 * n + 1, s)
else:
return isToOneRec(n // 2, s)
# Wrapper over isToOneRec()
def isToOne(n: int) -> bool:
# To store numbers visited
# using recursive calls.
s = set()
return isToOneRec(n, s)
# Driver Code
if __name__ == "__main__":
n = 5
if isToOne(n):
print("Yes")
else:
print("No")
# This code is contributed by
# sanjeev2552
C#
// C# program to implement
// Collatz Conjecture
using System;
using System.Collections.Generic;
class GFG
{
// Function to find if n reaches to 1 or not.
static Boolean isToOneRec(int n, HashSet<int> s)
{
if (n == 1)
{
return true;
}
// If there is a cycle formed,
// we can't reach 1.
if (s.Contains(n))
{
return false;
}
// If n is odd then pass n = 3n+1 else n = n/2
return (n % 2 == 1) ? isToOneRec(3 * n + 1, s)
: isToOneRec(n / 2, s);
}
// Wrapper over isToOneRec()
static Boolean isToOne(int n)
{
// To store numbers visited using
// recursive calls.
HashSet<int> s = new HashSet<int>();
return isToOneRec(n, s);
}
// Driver code
public static void Main(String[] args)
{
int n = 5;
if (isToOne(n))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to implement Collatz Conjecture
// Function to find if n reaches to 1 or not.
function isToOneRec(n, s)
{
if (n == 1)
{
return true;
}
// If there is a cycle formed,
// we can't reach 1.
if (s.has(n))
{
return false;
}
// If n is odd then pass n = 3n+1 else n = n/2
return (n % 2 == 1) ? isToOneRec(3 * n + 1, s)
: isToOneRec(n / 2, s);
}
// Wrapper over isToOneRec()
function isToOne(n)
{
// To store numbers visited using
// recursive calls.
let s = new Set();
return isToOneRec(n, s);
}
let n = 5;
if (isToOne(n))
{
document.write("Yes");
}
else
{
document.write("No");
}
// This code is contributed by divyeshrabadiya07.
</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++
// C++ program to implement Collatz Conjecture
#include<bits/stdc++.h>
using namespace std;
// Function to find if n reaches to 1 or not.
bool isToOne(int n)
{
// Return true if n is positive
return (n > 0);
}
// Drivers code
int main()
{
int n = 5;
isToOne(n) ? cout << "Yes" : cout <<"No";
return 0;
}
Java
// Java program to implement Collatz
// Conjecture
class GFG {
// Function to find if n reaches
// to 1 or not.
static boolean isToOne(int n)
{
// Return true if n is positive
return (n > 0);
}
// Drivers code
public static void main(String[] args)
{
int n = 5;
if(isToOne(n) == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Smitha.
Python 3
# Python 3 program to implement
# Collatz Conjecture
# Function to find if n
# reaches to 1 or not.
def isToOne(n):
# Return true if n
# is positive
return (n > 0)
# Drivers code
n = 5
if isToOne(n) == True:
print("Yes")
else:
print("No")
# This code is contributed
# by Smitha.
C#
// C# program to implement
// Collatz Conjecture
using System;
class GFG {
// Function to find if n
// reaches to 1 or not.
static bool isToOne(int n)
{
// Return true if n
// is positive
return (n > 0);
}
// Drivers code
public static void Main()
{
int n = 5;
if(isToOne(n) == true)
Console.Write("Yes") ;
else
Console.Write("No");
}
}
// This code is contributed
// by Smitha.
JavaScript
<script>
// Javascript program to implement Collatz Conjecture
// Function to find if n
// reaches to 1 or not.
function isToOne(n)
{
// Return true if n
// is positive
return (n > 0);
}
let n = 5;
if(isToOne(n) == true)
document.write("Yes") ;
else
document.write("No");
// This code is contributed by mukesh07.
</script>
PHP
<?php
// PHP program to implement Collatz Conjecture
// Function to find if n reaches
// to 1 or not.
function isToOne($n)
{
// Return true if n is positive
if($n > 0)
return true;
return false;
}
// Driver code
$n = 5;
isToOne($n)? print("Yes") : print("No");
// This code is contributed by princiraj1992
?>
Time complexity: O(1)
Auxiliary space: O(1)
We strongly recommend to refer below problem as an exercise:
Maximum Collatz sequence length
Similar Reads
Maximum Sequence Length | Collatz Conjecture Given an integer N. The task is to find the number in the range from 1 to N-1 which is having the maximum number of terms in its Collatz Sequence and the number of terms in the sequence.The collatz sequence of a number N is defined as: If N is Odd then change N to 3*N + 1.If N is Even then change N
6 min read
Program to print Collatz Sequence Starting with any positive integer N, Collatz sequence is defined corresponding to n as the numbers formed by the following operations : If n is even, then n = n / 2.If n is odd, then n = 3*n + 1.Repeat above steps, until it becomes 1. Examples : Input : 3 Output : 3, 10, 5, 16, 8, 4, 2, 1 Input : 6
4 min read
Legendre's Conjecture It says that there is always one prime number between any two consecutive natural number's(n = 1, 2, 3, 4, 5, ...) square. This is called Legendre's Conjecture. Conjecture: A conjecture is a proposition or conclusion based upon incomplete information to which no proof has been found i.e it has not b
5 min read
Program to find Cullen Number A Cullen Number is a number of the form is 2n * n + 1 where n is an integer. The first few Cullen numbers are 1, 3, 9, 25, 65, 161, 385, 897, 2049, 4609 . . . . . . Examples: Input : n = 4 Output :65 Input : n = 0 Output : 1 Input : n = 6 Output : 161 Below is implementation of formula. We use bitwi
3 min read
Lemoine's Conjecture Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine's conjecture. Examples : 7 = 3 + (2 Ã 2), where 3 is a prime number (other than 2) and 4 (= 2
8 min read