We are provided with a number N. Our task is to generate all the Hailstone Numbers from N and find the number of steps taken by N to reduce to
Collatz Conjecture: A problem posed by L. Collatz in 1937, also called the 3x+1 mapping, 3n+1 problem. Let N be a integer. According to Collatz conjecture, if we keep iterating N as following
N = N / 2 // For Even N
and N = 3 * N + 1 // For Odd N
Our number will eventually converge to 1 irrespective of the choice of N.
Hailstone Numbers: The sequence of integers generated by Collatz conjecture are called Hailstone Numbers.
Examples:
Input : N = 7
Output :
Hailstone Numbers: 7, 22, 11, 34, 17,
52, 26, 13, 40, 20,
10, 5, 16, 8, 4, 2,
1
No. of steps Required: 17
Input : N = 9
Output :
Hailstone Numbers: 9, 28, 14, 7, 22, 11,
34, 17, 52, 26, 13,
40, 20, 10, 5, 16, 8,
4, 2, 1
No. of steps Required: 20
In the first example, N = 7.
The numbers will be calculated as follows:
7
3 * 7 + 1 = 22 // Since 7 is odd.
22 / 2 = 11 // 22 is even.
3 * 11 + 1 = 34 // 11 is odd.
.... and so on upto 1.
The idea is simple, we recursively print numbers until we reach base case.
C++
#include <bits/stdc++.h>
using namespace std;
int HailstoneNumbers( int N)
{
static int c;
cout << N << " " ;
if (N == 1 && c == 0) {
return c;
}
else if (N == 1 && c != 0) {
c++;
return c;
}
else if (N % 2 == 0) {
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {
c++;
HailstoneNumbers(3 * N + 1);
}
}
int main()
{
int N = 7;
int x;
x = HailstoneNumbers(N);
cout << endl;
cout << "Number of Steps: " << x;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int c;
static int HailstoneNumbers( int N)
{
System.out.print(N + " " );
if (N == 1 && c == 0 ) {
return c;
}
else if (N == 1 && c != 0 ) {
c++;
return c;
}
else if (N % 2 == 0 ) {
c++;
HailstoneNumbers(N / 2 );
}
else if (N % 2 != 0 ) {
c++;
HailstoneNumbers( 3 * N + 1 );
}
return c;
}
public static void main(String[] args)
{
int N = 7 ;
int x;
x = HailstoneNumbers(N);
System.out.println();
System.out.println( "Number of Steps: " + x);
}
}
|
Python
def HailstoneNumbers(N, c):
print (N, end = " " )
if (N = = 1 and c = = 0 ):
return c
elif (N = = 1 and c ! = 0 ):
c = c + 1
elif (N % 2 = = 0 ):
c = c + 1
c = HailstoneNumbers( int (N / 2 ), c)
elif (N % 2 ! = 0 ):
c = c + 1
c = HailstoneNumbers( 3 * N + 1 , c)
return c
N = 7
x = HailstoneNumbers(N, 0 )
print ( "\nNumber of Steps: " , x)
|
C#
using System;
class GFG {
static int c;
static int HailstoneNumbers( int N)
{
Console.Write(N + " " );
if (N == 1 && c == 0) {
return c;
}
else if (N == 1 && c != 0) {
c++;
return c;
}
else if (N % 2 == 0) {
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {
c++;
HailstoneNumbers(3 * N + 1);
}
return c;
}
public static void Main()
{
int N = 7;
int x;
x = HailstoneNumbers(N);
Console.WriteLine();
Console.WriteLine( "Number of Steps: " + x);
}
}
|
PHP
<?php
function HailstoneNumbers( $N )
{
static $c ;
echo $N . " " ;
if ( $N == 1 && $c == 0)
{
return $c ;
}
else if ( $N == 1 && $c != 0)
{
$c ++;
return $c ;
}
else if ( $N % 2 == 0)
{
$c ++;
HailstoneNumbers((int)( $N / 2));
}
else if ( $N % 2 != 0)
{
$c ++;
HailstoneNumbers(3 * $N + 1);
}
return $c ;
}
$N = 7;
$x = HailstoneNumbers( $N );
echo "\nNumber of Steps: " . $x ;
?>
|
Javascript
<script>
let c = 0;
function HailstoneNumbers(N)
{
document.write(N + " " );
if (N == 1 && c == 0) {
return c;
}
else if (N == 1 && c != 0) {
c++;
return c;
}
else if (N % 2 == 0) {
c++;
HailstoneNumbers(N / 2);
}
else if (N % 2 != 0) {
c++;
HailstoneNumbers(3 * N + 1);
}
return c;
}
let N = 7;
let x;
x = HailstoneNumbers(N);
document.write( "<br/>" );
document.write( "Number of Steps: " + x);
</script>
|
Output
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Number of Steps: 17
Last Updated :
29 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...