Números de granizo

Se nos proporciona un número N. Nuestra tarea es generar todos los números de granizo a partir de N y encontrar el número de pasos tomados por N para reducir a

Conjetura de Collatz: un problema planteado por L. Collatz en 1937, también llamado mapeo 3x+1, problema 3n+1. Sea N un número entero. De acuerdo con la conjetura de Collatz, si seguimos iterando N de la siguiente manera 
: N = N / 2 // Para N pares 
y N = 3 * N + 1 // Para N impares 
Nuestro número eventualmente convergerá a 1 independientemente de la elección de N.  Piedra de
granizo Números: La secuencia de números enteros generados por la conjetura de Collatz se denominan números de granizo.

Ejemplos: 

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.

La idea es simple, imprimimos números recursivamente hasta llegar al caso base. 

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("\nNumber 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 "\nNumber 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>
Producción

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

Este artículo es una contribución de Vineet Joshi . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a contribuir@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *