Encuentre todos los números hasta N que sean pentagonales y hexagonales

Dado un número entero N , la tarea es encontrar todos los números hasta N , que son tanto pentagonales como hexagonales .
Ejemplo: 
 

Entrada: N = 1000 
Salida: 1
Entrada: N = 100000 
Salida: 1, 40755 
 

Acercarse: 
 

  • Para resolver el problema, generamos todos los números pentagonales hasta N y verificamos si son números hexagonales o no.
  • Fórmula para calcular el i -ésimo número pentagonal
     

yo * ( 3 * yo – 1 ) / 2

  • Para verificar si un número pentagonal, digamos pn , es un número hexagonal o no: 
     

( 1 + sqrt(8 * pn + 1 ) ) / 4 debe ser un número natural

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

C++

// C++ Program of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print numbers upto N
// which are both pentagonal as well
// as hexagonal numbers
void pen_hex(long long n)
{
    long long pn = 1;
    for (long long int i = 1;; i++) {
 
        // Calculate i-th pentagonal number
        pn = i * (3 * i - 1) / 2;
 
        if (pn > n)
            break;
 
        // Check if the pentagonal number
        // pn is hexagonal or not
        long double seqNum
            = (1 + sqrt(8 * pn + 1)) / 4;
 
        if (seqNum == long(seqNum))
            cout << pn << ", ";
    }
}
 
// Driver Program
int main()
{
    long long int N = 1000000;
    pen_hex(N);
    return 0;
}

Java

// Java program of the above approach
import java.util.*;
 
class GFG{
 
// Function to print numbers upto N
// which are both pentagonal as well
// as hexagonal numbers
static void pen_hex(long n)
{
    long pn = 1;
    for(long i = 1; i < n; i++)
    {
         
        // Calculate i-th pentagonal number
        pn = i * (3 * i - 1) / 2;
 
        if (pn > n)
            break;
 
        // Check if the pentagonal number
        // pn is hexagonal or not
        double seqNum = (1 + Math.sqrt(
                        8 * pn + 1)) / 4;
 
        if (seqNum == (long)seqNum)
            System.out.print(pn + ", ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    long N = 1000000;
    pen_hex(N);
}
}
 
// This code is contributed by offbeat

Python3

# Python3 program of the above approach
import math
 
# Function to print numbers upto N
# which are both pentagonal as well
# as hexagonal numbers
def pen_hex(n):
 
    pn = 1
    for i in range(1, N):
 
        # Calculate i-th pentagonal number
        pn = (int)(i * (3 * i - 1) / 2)
 
        if (pn > n):
            break
 
        # Check if the pentagonal number
        # pn is hexagonal or not
        seqNum = (1 + math.sqrt(8 * pn + 1)) / 4
 
        if (seqNum == (int)(seqNum)):
            print(pn, end = ", ")
 
# Driver Code
N = 1000000
 
pen_hex(N)
 
# This code is contributed by divyeshrabadiya07

C#

// C# program of the above approach 
using System;
using System.Collections.Generic;
 
class GFG{        
             
// Function to print numbers upto N
// which are both pentagonal as well
// as hexagonal numbers
static void pen_hex(long n)
{
    long pn = 1;
    for(long i = 1;; i++)
    {
         
        // Calculate i-th pentagonal number
        pn = i * (3 * i - 1) / 2;
 
        if (pn > n)
            break;
 
        // Check if the pentagonal number
        // pn is hexagonal or not
        double seqNum = (1 + Math.Sqrt(
                         8 * pn + 1)) / 4;
 
        if (seqNum == (long)(seqNum))
        {
            Console.Write(pn + ", ");
        }
    }
}
         
// Driver Code        
public static void Main (string[] args)
{        
    long N = 1000000;
     
    pen_hex(N);
}        
}
 
// This code is contributed by rutvik_56

Javascript

<script>
// javascript program of the above approach
  
// Function to print numbers upto N
// which are both pentagonal as well
// as hexagonal numbers
function pen_hex(n)
{
    var pn = 1;
    for(i = 1; i < n; i++)
    {
         
        // Calculate i-th pentagonal number
        pn = parseInt(i * (3 * i - 1) / 2);
 
        if (pn > n)
            break;
 
        // Check if the pentagonal number
        // pn is hexagonal or not
        var seqNum = (1 + Math.sqrt(
                        8 * pn + 1)) / 4;
 
        if (seqNum == parseInt(seqNum))
            document.write(pn + ", ");
    }
}
 
// Driver code
var N = 1000000;
pen_hex(N);
 
// This code is contributed by Amit Katiyar
</script>
Producción: 

1, 40755,

 

Complejidad temporal: O(N) 
Espacio auxiliar: O(1)
 

Publicación traducida automáticamente

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