Programa para hallar el término N de la serie 1, 3, 12, 60, 360…

Dado un número N. La tarea es escribir un programa para encontrar el N-ésimo término en la siguiente serie: 
 

1, 3, 12, 60, 360…

Ejemplos: 
 

Input: 2
Output: 3

Input: 4
Output: 60

Enfoque: ¡ La idea es encontrar primero el factorial del número (N+1), es decir (N+1)!
Ahora, el N-ésimo término de la serie anterior será: 
 

N-th term = (N+1)!/2

A continuación se muestra la implementación del enfoque anterior:
 

C++

// CPP program to find N-th term of the series:
// 1, 3, 12, 60, 360…
 
#include <iostream>
using namespace std;
 
// Function to find factorial of N
int factorial(int N)
{
    int fact = 1;
 
    for (int i = 1; i <= N; i++)
        fact = fact * i;
 
    // return factorial of N+1
    return fact;
}
 
// calculate Nth term of series
int nthTerm(int N)
{
    return (factorial(N + 1) / 2);
}
 
// Driver Function
int main()
{
 
    // Taking n as 6
    int N = 6;
 
    // Printing the nth term
    cout << nthTerm(N);
 
    return 0;
}

C

// C program to find N-th term of the series:
// 1, 3, 12, 60, 360…
#include <stdio.h>
 
// Function to find factorial of N
int factorial(int N)
{
    int fact = 1;
 
    for (int i = 1; i <= N; i++)
        fact = fact * i;
 
    // return factorial of N+1
    return fact;
}
 
// calculate Nth term of series
int nthTerm(int N)
{
    return (factorial(N + 1) / 2);
}
 
// Driver Function
int main()
{
 
    // Taking n as 6
    int N = 6;
 
    // Printing the nth term
    printf("%d",nthTerm(N));
 
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java program to find N-th
// term of the series:
// 1, 3, 12, 60, 360
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Function to find factorial of N
    static int factorial(int N)
    {
        int fact = 1;
 
        for (int i = 1; i <= N; i++)
            fact = fact * i;
 
        // return factorial of N
        return fact;
    }
 
    // calculate Nth term of series
    static int nthTerm(int N)
    {
        return (factorial(N + 1) / 2);
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        // Taking  n as 6
        int N = 6;
 
        // Printing the nth term
        System.out.println(nthTerm(N));
    }
}

Python3

# Python 3 program to find
# N-th term of the series:
# 1, 3, 12, 60, 360…
 
# Function for finding
# factorial of N
def factorial(N) :
    fact = 1
    for i in range(1, N + 1) :
        fact = fact * i
 
    # return factorial of N
    return fact
 
# Function for calculating
# Nth term of series
def nthTerm(N) :
 
    # return nth term
    return (factorial(N + 1) // 2)
 
# Driver code
if __name__ == "__main__" :
     
    N = 6
 
    # Function Calling
    print(nthTerm(N))

C#

// C# program to find N-th
// term of the series:
// 1, 3, 12, 60, 360
using System;
 
class GFG
{
     
// Function to find factorial of N
static int factorial(int N)
{
    int fact = 1;
 
    for (int i = 1; i <= N; i++)
        fact = fact * i;
 
    // return factorial of N
    return fact;
}
 
// calculate Nth term of series
static int nthTerm(int N)
{
    return (factorial(N + 1) / 2);
}
 
// Driver Code
static void Main()
{
    int N = 6 ;
     
    // Printing the nth term
    Console.WriteLine(nthTerm(N));
}
}
 
// This code is contributed
// by ANKITRAI1

PHP

<?php
// PHP program to find N-th term
// of the series: 1, 3, 12, 60, 360…
 
// Function to find factorial of N
function factorial($N)
{
    $fact = 1;
 
    for ($i = 1; $i <= $N; $i++)
        $fact = $fact * $i;
 
    // return factorial of N+1
    return $fact;
}
 
// calculate Nth term of series
function nthTerm($N)
{
    return (factorial($N + 1) / 2);
}
 
// Driver Code
 
// Taking n as 6
$N = 6;
 
// Printing the nth term
echo nthTerm($N);
 
// This code is contributed
// by chandan_jnu..
?>

Javascript

<script>
 
// JavaScript program to find N-th term of the series:
// 1, 3, 12, 60, 360…
    
// Function to find factorial of N
function factorial(N)
{
    let fact = 1;
   
    for (let i = 1; i <= N; i++)
        fact = fact * i;
   
    // return factorial of N+1
    return fact;
}
   
// calculate Nth term of series
function nthTerm(N)
{
    return (Math.floor(factorial(N + 1) / 2));
}
   
// Driver Function
 
    // Taking n as 6
    let N = 6;
   
    // Printing the nth term
    document.write(nthTerm(N));
   
   
// This code is contributed by Surbhi Tyagi
 
</script>
Producción: 

2520

 

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

Publicación traducida automáticamente

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