Programa para imprimir números de pentátopo hasta el término N

requisitos previos: 
 

Dado un valor n, la tarea es imprimir series de números pentatópicos hasta el término n .
Ejemplos: 
 

Input: 5
Output: 1 5 15 35 70

Input: 10
Output: 1 5 15 35 70 126 210 330 495 715 

Método 1: Uso de series de números tetraédricos: 
este problema se puede resolver fácilmente con el hecho de que el N número del pentátopo es igual a la suma de los primeros N números tetraédricos.
Echemos un vistazo a la serie de números pentatópicos y tetraédricos. 
 

Para n = 5 
Números tetraédricos = 1, 4, 10, 20, 35 
Prefijo Suma de números tetraédricos para cada término: (1), (1 + 4), (1 + 4 + 10), (1 + 4 + 10 + 20), (1 + 4 + 10 + 20 + 35) 
Entonces, los números del pentátopo son 1, 5, 15, 35, 70 
 

Calcule el N número tetraédrico usando la fórmula:  \frac{N\times (N+1)\times (N+2)}{6}
Por lo tanto, imprima la serie de Números pentatópicos generando números tetraédricos y sumándolos con la suma de todos los Números tetraédricos generados previamente.
A continuación se muestra la implementación del enfoque anterior:
 

CPP

// C++ program to generate Pentatope
// Number series
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate nth tetrahedral number
int findTetrahedralNumber(int n)
{
    return ((n * (n + 1) * (n + 2)) / 6);
}
 
// Function to print pentatope number
// series upto nth term.
void printSeries(int n)
{
    // Initialize prev as 0. It store the
    // sum of all previously generated
    // pentatope numbers
    int prev = 0;
    int curr;
 
    // Loop to print pentatope series
    for (int i = 1; i <= n; i++)
    {
        // Find ith tetrahedral number
        curr = findTetrahedralNumber(i);
 
        // Add ith tetrahedral number to
        // sum of all previously generated
        // tetrahedral number to get ith
        // pentatope number
        curr = curr + prev;
        cout << curr << " ";
 
        // Update sum of all previously
        // generated tetrahedral number
        prev = curr;
    }
}
 
// Driver code
int main()
{
    int n = 10;
     
    // Function call to print pentatope
    // number series
    printSeries(n);
     
    return 0;
}

Java

// Java program to generate Pentatope
// Number series
import java.io.*;
 
class GFG {
     
    // Function to generate nth tetrahedral number
    static int findTetrahedralNumber(int n)
    {
        return ((n * (n + 1) * (n + 2)) / 6);
    }
     
    // Function to print pentatope number
    // series upto nth term.
    static void printSeries(int n)
    {
        // Initialize prev as 0. It store the
        // sum of all previously generated
        // pentatope numbers
        int prev = 0;
        int curr;
     
        // Loop to print pentatope series
        for (int i = 1; i <= n; i++)
        {
            // Find ith tetrahedral number
            curr = findTetrahedralNumber(i);
     
            // Add ith tetrahedral number to
            // sum of all previously generated
            // tetrahedral number to get ith
            // pentatope number
            curr = curr + prev;
            System.out.print(curr + " ");
     
            // Update sum of all previously
            // generated tetrahedral number
            prev = curr;
        }
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 10;
     
        // Function call to print pentatope
        // number series
        printSeries(n);
    }
}

python3

# Python program to generate Pentatope
# Number series
 
# Function to generate nth tetrahedral number
def findTetrahedralNumber(n) :
    return (int((n * (n + 1) * (n + 2)) / 6))
 
# Function to print pentatope number
# series upto nth term.
def printSeries(n) :
     
    # Initialize prev as 0. It store the
    # sum of all previously generated
    # pentatope numbers
    prev = 0
 
    # Loop to print pentatope series
    for i in range(1, n + 1) :
     
        # Find ith tetrahedral number
        curr = findTetrahedralNumber(i)
 
        # Add ith tetrahedral number to
        # sum of all previously generated
        # tetrahedral number to get ith
        # pentatope number
        curr = curr + prev;
        print(curr, end=' ')
 
        # Update sum of all previously
        # generated tetrahedral number
        prev = curr
 
# Driver code
n = 10
     
# Function call to print pentatope
# number series
printSeries(n)

C#

// C# program to generate Pentatope
// Number series
using System;
 
public class GFG {
     
    // Function to generate nth tetrahedral number
    static int findTetrahedralNumber(int n)
    {
        return ((n * (n + 1) * (n + 2)) / 6);
    }
     
    // Function to print pentatope number
    // series upto nth term.
    static void printSeries(int n)
    {
        // Initialize prev as 0. It store the
        // sum of all previously generated
        // pentatope numbers
        int prev = 0;
        int curr;
     
        // Loop to print pentatope series
        for (int i = 1; i <= n; i++)
        {
            // Find ith tetrahedral number
            curr = findTetrahedralNumber(i);
     
            // Add ith tetrahedral number to
            // sum of all previously generated
            // tetrahedral number to get ith
            // pentatope number
            curr = curr + prev;
            Console.Write(curr + " ");
     
            // Update sum of all previously
            // generated tetrahedral number
            prev = curr;
        }
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 10;
     
        // Function call to print pentatope
        // number series
        printSeries(n);
    }
}

PHP

<?php
// PHP program to generate Pentatope
// Number series
 
// Function to generate nth tetrahedral number
function findTetrahedralNumber($n)
{
    return (($n * ($n + 1) * ($n + 2)) / 6);
}
 
// Function to print pentatope number
// series upto nth term.
function printSeries($n)
{
    // Initialize prev as 0. It store the
    // sum of all previously generated
    // pentatope numbers
    $prev = 0;
    $curr;
 
    // Loop to print pentatope series
    for ($i = 1; $i <= $n; $i++)
    {
        // Find ith tetrahedral number
        $curr = findTetrahedralNumber($i);
 
        // Add ith tetrahedral number to
        // sum of all previously generated
        // tetrahedral number to get ith
        // pentatope number
        $curr = $curr + $prev;
        echo($curr . " ");
 
        // Update sum of all previously
        // generated tetrahedral number
        $prev = $curr;
    }
}
 
// Driver code
$n = 10;
     
// Function call to print pentatope
// number series
printSeries($n);
?>

Javascript

<script>
 
// JavaScript program to generate Pentatope
// Number series
 
// Function to generate nth tetrahedral number
function findTetrahedralNumber(n)
{
    return ((n * (n + 1) * (n + 2)) / 6);
}
 
// Function to print pentatope number
// series upto nth term.
function printSeries(n)
{
    // Initialize prev as 0. It store the
    // sum of all previously generated
    // pentatope numbers
    let prev = 0;
    let curr;
 
    // Loop to print pentatope series
    for (let i = 1; i <= n; i++)
    {
        // Find ith tetrahedral number
        curr = findTetrahedralNumber(i);
 
        // Add ith tetrahedral number to
        // sum of all previously generated
        // tetrahedral number to get ith
        // pentatope number
        curr = curr + prev;
        document.write(curr+" ");
 
        // Update sum of all previously
        // generated tetrahedral number
        prev = curr;
    }
}
 
// Driver code
let n = 10;
     
// Function call to print pentatope
// number series
printSeries(n);
 
// This code is contributed by sravan kumar
 
</script>
Producción: 

1 5 15 35 70 126 210 330 495 715

 

Complejidad de tiempo: O(n), donde n representa el entero dado.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Método 2: Uso de la fórmula del número de pentátopo: 
La fórmula para encontrar el número N de pentátopo  A\frac{N\times (N+1)\times (N+2)\times (N+3)}{24}
continuación se muestra la implementación requerida:
 

CPP

// C++ program to print Pentatope number series.
#include <bits/stdc++.h>
using namespace std;
 
// Function to print pentatope series up to nth term
void printSeries(int n)
{
 
    // Loop to print pentatope number series
    for (int i = 1; i <= n; i++)
    {
        // calculate and print ith pentatope number
        int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);
         
        cout << num << " ";
    }
}
 
// Driver code
int main()
{
    int n = 10;
     
    // Function call to print pentatope number series
    printSeries(n);
    return 0;
}

Java

// Java program to print Pentatope number series.
import java.io.*;
 
class GFG {
     
    // Function to print pentatope series up to nth term
    static void printSeries(int n)
    {
     
        // Loop to print pentatope number series
        for (int i = 1; i <= n; i++)
        {
            // calculate and print ith pentatope number
            int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);
             
            System.out.print(num + " ");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 10;
     
        // Function call to print pentatope number series
        printSeries(n);
    }
}

python3

# Python program to print Pentatope number series.
 
# Function to print pentatope series up to nth term
def printSeries(n) :
 
    # Loop to print pentatope number series
    for i in range(1, n + 1) :
 
        # calculate and print ith pentatope number
        num = int(i * (i + 1) * (i + 2) * (i + 3) // 24)
         
        print(num, end=' ');
 
# Driver code
n = 10
     
# Function call to print pentatope number series
printSeries(n)

C#

// C# program to print Pentatope number series.
using System;
 
public class GFG {
     
    // Function to print pentatope series up to nth term
    static void printSeries(int n)
    {
     
        // Loop to print pentatope number series
        for (int i = 1; i <= n; i++)
        {
            // calculate and print ith pentatope number
            int num = (i * (i + 1) * (i + 2) * (i + 3) / 24);
             
            Console.Write(num + " ");
        }
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 10;
     
        // Function call to print pentatope number series
        printSeries(n);
    }
}

PHP

<?php
// PHP program to print Pentatope number series.
 
// Function to print pentatope series up to nth term
function printSeries($n)
{
 
    // Loop to print pentatope number series
    for ($i = 1; $i <= $n; $i++)
    {
        // calculate and print ith pentatope number
        $num = ($i * ($i + 1) * ($i + 2) * ($i + 3) / 24);
         
        echo($num . " ");
    }
}
 
// Driver code
$n = 10;
     
// Function call to print pentatope number series
printSeries($n);
?>

Javascript

<script>
 
// Javascript program to print Pentatope number series.
 
// Function to print pentatope series up to nth term
function printSeries(n)
{
 
    // Loop to print pentatope number series
    for (let i = 1; i <= n; i++)
    {
        // calculate and print ith pentatope number
        num = (i * (i + 1) * (i + 2) * (i + 3) / 24);
         
        document.write(num+" ");
    }
}
 
// Driver code
let n = 10;
     
// Function call to print pentatope number series
printSeries(n);
 
// This code is contributed by sravan kumar
 
</script>
Producción: 

1 5 15 35 70 126 210 330 495 715

 

Complejidad de tiempo: O(n), donde n representa el entero dado.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por ihritik 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 *