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 1.

**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) + 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++

`// C++ program to generate hailstone ` `// numbers and calculate steps required ` `// to reduce them to 1 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to print hailstone numbers ` `// and to calculate the number of steps ` `// required ` `int` `HailstoneNumbers(` `int` `N) ` `{ ` ` ` `static` `int` `c; ` ` ` ` ` `cout << N << ` `" "` `; ` ` ` ` ` `if` `(N == 1 && c == 0) { ` ` ` ` ` `// N is initially 1. ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N == 1 && c != 0) { ` ` ` ` ` `// N is reduced to 1. ` ` ` `c++; ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N % 2 == 0) { ` ` ` ` ` `// If N is Even. ` ` ` `c++; ` ` ` `HailstoneNumbers(N / 2); ` ` ` `} ` ` ` `else` `if` `(N % 2 != 0) { ` ` ` ` ` `// N is Odd. ` ` ` `c++; ` ` ` `HailstoneNumbers(3 * N + 1); ` ` ` `} ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `N = 7; ` ` ` `int` `x; ` ` ` ` ` `// Function to generate Hailstone ` ` ` `// Numbers ` ` ` `x = HailstoneNumbers(N); ` ` ` ` ` `// Output: Number of Steps ` ` ` `cout << endl; ` ` ` `cout << ` `"Number of Steps: "` `<< x; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to generate hailstone ` `// numbers and calculate steps required ` `// to reduce them to 1 ` `import` `java.util.*; ` `class` `GFG ` `{ ` `static` `int` `c; ` ` ` `// function to print hailstone numbers ` `// and to calculate the number of steps ` `// required ` `static` `int` `HailstoneNumbers(` `int` `N) ` `{ ` ` ` `System.out.print(N + ` `" "` `); ` ` ` ` ` `if` `(N == ` `1` `&& c == ` `0` `) { ` ` ` ` ` `// N is initially 1. ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N == ` `1` `&& c != ` `0` `) { ` ` ` ` ` `// N is reduced to 1. ` ` ` `c++; ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N % ` `2` `== ` `0` `) { ` ` ` ` ` `// If N is Even. ` ` ` `c++; ` ` ` `HailstoneNumbers(N / ` `2` `); ` ` ` `} ` ` ` `else` `if` `(N % ` `2` `!= ` `0` `) { ` ` ` ` ` `// N is Odd. ` ` ` `c++; ` ` ` `HailstoneNumbers(` `3` `* N + ` `1` `); ` ` ` `} ` ` ` `return` `c; ` `} ` ` ` `// Driver function ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `7` `; ` ` ` `int` `x; ` ` ` ` ` `// Function to generate Hailstone ` ` ` `// Numbers ` ` ` `x = HailstoneNumbers(N); ` ` ` ` ` `// Output: Number of Steps ` ` ` `System.out.println(); ` ` ` `System.out.println(` `"Number of Steps: "` `+x); ` `} ` `} ` `/* This code is contributed by Kriti Shukla */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to generate ` `# hailstone numbers and ` `# calculate steps required ` `# to reduce them to 1 ` ` ` `# function to print hailstone ` `# numbers and to calculate ` `# the number of steps required ` `def` `HailstoneNumbers(N, c): ` ` ` `print` `(N, end ` `=` `" "` `); ` ` ` `if` `(N ` `=` `=` `1` `and` `c ` `=` `=` `0` `): ` ` ` ` ` `# N is initially 1. ` ` ` `return` `c; ` ` ` `elif` `(N ` `=` `=` `1` `and` `c !` `=` `0` `): ` ` ` ` ` `# N is reduced to 1. ` ` ` `c ` `=` `c ` `+` `1` `; ` ` ` `elif` `(N ` `%` `2` `=` `=` `0` `): ` ` ` ` ` `# If N is Even. ` ` ` `c ` `=` `c ` `+` `1` `; ` ` ` `c ` `=` `HailstoneNumbers(` `int` `(N ` `/` `2` `), c); ` ` ` `elif` `(N ` `%` `2` `!` `=` `0` `): ` ` ` ` ` `# N is Odd. ` ` ` `c ` `=` `c ` `+` `1` `; ` ` ` `c ` `=` `HailstoneNumbers(` `3` `*` `N ` `+` `1` `, c); ` ` ` `return` `c; ` ` ` `# Driver Code ` `N ` `=` `7` `; ` ` ` `# Function to generate ` `# Hailstone Numbers ` `x ` `=` `HailstoneNumbers(N, ` `0` `); ` ` ` `# Output: Number of Steps ` `print` `(` `"\nNumber of Steps: "` `, x); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to generate hailstone ` `// numbers and calculate steps required ` `// to reduce them to 1 ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `c; ` ` ` ` ` `// function to print hailstone numbers ` ` ` `// and to calculate the number of steps ` ` ` `// required ` ` ` `static` `int` `HailstoneNumbers(` `int` `N) ` ` ` `{ ` ` ` `Console.Write(N + ` `" "` `); ` ` ` ` ` `if` `(N == 1 && c == 0) { ` ` ` ` ` `// N is initially 1. ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N == 1 && c != 0) { ` ` ` ` ` `// N is reduced to 1. ` ` ` `c++; ` ` ` `return` `c; ` ` ` `} ` ` ` `else` `if` `(N % 2 == 0) { ` ` ` ` ` `// If N is Even. ` ` ` `c++; ` ` ` `HailstoneNumbers(N / 2); ` ` ` `} ` ` ` `else` `if` `(N % 2 != 0) ` ` ` `{ ` ` ` ` ` `// N is Odd. ` ` ` `c++; ` ` ` `HailstoneNumbers(3 * N + 1); ` ` ` `} ` ` ` `return` `c; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 7; ` ` ` `int` `x; ` ` ` ` ` `// Function to generate Hailstone ` ` ` `// Numbers ` ` ` `x = HailstoneNumbers(N); ` ` ` ` ` `// Output: Number of Steps ` ` ` `Console.WriteLine(); ` ` ` `Console.WriteLine(` `"Number of Steps: "` `+x); ` ` ` `} ` `} ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to generate ` `// hailstone numbers and ` `// calculate steps required ` `// to reduce them to 1 ` ` ` `// function to print hailstone ` `// numbers and to calculate the ` `// number of steps required ` `function` `HailstoneNumbers(` `$N` `) ` `{ ` ` ` `static` `$c` `; ` ` ` ` ` `echo` `$N` `.` `" "` `; ` ` ` ` ` `if` `(` `$N` `== 1 && ` `$c` `== 0) ` ` ` `{ ` ` ` ` ` `// N is initially 1. ` ` ` `return` `$c` `; ` ` ` `} ` ` ` `else` `if` `(` `$N` `== 1 && ` `$c` `!= 0) ` ` ` `{ ` ` ` ` ` `// N is reduced to 1. ` ` ` `$c` `++; ` ` ` `return` `$c` `; ` ` ` `} ` ` ` `else` `if` `(` `$N` `% 2 == 0) ` ` ` `{ ` ` ` ` ` `// If N is Even. ` ` ` `$c` `++; ` ` ` `HailstoneNumbers((int)(` `$N` `/ 2)); ` ` ` `} ` ` ` `else` `if` `(` `$N` `% 2 != 0) ` ` ` `{ ` ` ` ` ` `// N is Odd. ` ` ` `$c` `++; ` ` ` `HailstoneNumbers(3 * ` `$N` `+ 1); ` ` ` `} ` ` ` `return` `$c` `; ` `} ` ` ` `// Driver Code ` `$N` `= 7; ` ` ` `// Function to generate ` `// Hailstone Numbers ` `$x` `= HailstoneNumbers(` `$N` `); ` ` ` `// Output: Number of Steps ` `echo` `"\nNumber of Steps: "` `. ` `$x` `; ` ` ` `// This code is contributed ` `// by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Number of Steps: 17

This article is contributed by **Vineet Joshi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of numbers upto M divisible by given Prime Numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Check if a given pair of Numbers are Betrothed numbers or not
- Numbers less than N which are product of exactly two distinct prime numbers
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- D Numbers
- Add two numbers using ++ and/or --
- Self Numbers
- Sum of first n even numbers
- Represent K^N as the sum of exactly N numbers
- Decagonal Numbers
- Sum of first K numbers which are not divisible by N