冰雹数量

我们得到了一个数字N。我们的任务是从N生成所有冰雹数字,并找出N采取的步骤数,以减少到

null

科拉茨猜想: L.Collatz在1937年提出的一个问题,也称为3x+1映射,即3n+1问题。设N为整数。根据Collatz猜想,如果我们继续如下迭代N N=N/2//表示偶数N 对于奇数N,N=3*N+1// 不管选择N,我们的数字最终都会收敛到1。 冰雹数量: 科拉兹猜想产生的整数序列称为冰雹数。

例如:

Input : N = 7Output : Hailstone Numbers: 7, 22, 11, 34, 17,                   52, 26, 13, 40, 20,                   10, 5, 16, 8, 4, 2,                    1No. of steps Required: 17Input : N = 9Output : Hailstone Numbers: 9, 28, 14, 7, 22, 11,                   34, 17, 52, 26, 13,                    40, 20, 10, 5, 16, 8,                   4, 2, 1No. of steps Required: 20In the first example, N = 7. The numbers will be calculated as follows:73 * 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.

这个想法很简单,我们递归地打印数字,直到达到基本情况。

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 code
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;
}


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 code
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 */


python

# 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 ( "Number of Steps: " , x)
# This code is contributed
# by mits


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 code
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


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 "Number of Steps: " . $x ;
// This code is contributed
// by mits
?>


Javascript

<script>
// JavaScript program to generate hailstone
// numbers and calculate steps required
// to reduce them to 1
let c = 0;
// function to print hailstone numbers
// and to calculate the number of steps
// required
function HailstoneNumbers(N)
{
document.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 Code
let N = 7;
let x;
// Function to generate Hailstone
// Numbers
x = HailstoneNumbers(N);
// Output: Number of Steps
document.write( "<br/>" );
document.write( "Number of Steps: " + x);
// This code is contributed by susmitakundugoaldanga.
</script>


输出

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

本文由 维尼特·乔希 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享