Producto pandigital

Un número pandigital es un número que utiliza todos los dígitos del 1 al 9 exactamente una vez. Nos dan un número, tenemos que averiguar si hay dos números cuya multiplicación es número dado y dados tres números juntos son pandigitales.

Ejemplos: 

Input : 7254
Output : Yes
39 * 186 = 7254. We can notice that
the three numbers 39, 186 and 7254
together have all digits from 1 to 9.

Input : 6952
Output : Yes

La idea es considerar todos los pares que se multiplican a un número dado. Para cada par, cree una string que contenga tres números (número dado y par actual). Ordenamos la string creada y verificamos si la string ordenada es igual a «123456789». 

C++

// C++ code to check the number
// is Pandigital Product or not
#include <bits/stdc++.h>
using namespace std;
 
// To check the string formed
// from multiplicand, multiplier
// and product is pandigital
bool isPandigital(string str)
{
    if (str.length() != 9)
        return false;
     
    char ch[str.length()];
    strcpy(ch, str.c_str());
    sort(ch, ch + str.length());
    string s = ch;
     
    if(s.compare("123456789") == 0)
        return true;
    else
        return true;
}
 
// calculate the multiplicand,
// multiplier, and product
// eligible for pandigital
bool PandigitalProduct_1_9(int n)
{
    for (int i = 1; i * i <= n; i++)
        if (n % i == 0 && isPandigital(to_string(n) +
                                       to_string(i) +
                                   to_string(n / i)))
            return true;
    return false;
}
 
// Driver Code
int main()
{
    int n = 6952;
    if (PandigitalProduct_1_9(n) == true)
        cout << "yes";
    else
        cout << "no";
    return 0;
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)

Java

// Java code to check the number
// is Pandigital Product or not
import java.io.*;
import java.util.*;
class GFG {
 
    // calculate the multiplicand, multiplier, and product
    // eligible for pandigital
    public static boolean PandigitalProduct_1_9(int n)
    {
        for (int i = 1; i*i <= n; i++)
            if (n % i == 0 && isPandigital("" + n + i + n / i))
                return true;
        return false;
    }
 
    // To check the string formed from multiplicand
    // multiplier and product is pandigital
    public static boolean isPandigital(String str)
    {
        if (str.length() != 9)
            return false;
        char ch[] = str.toCharArray();
        Arrays.sort(ch);
        return new String(ch).equals("123456789");
    }
 
    // Driver function
    public static void main(String[] args)
    {
        int n = 6952;
        if (PandigitalProduct_1_9(n) == true)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}

Python3

# Python3 code to check the number
# is Pandigital Product or not
 
# Calculate the multiplicand,
# multiplier, and product
# eligible for pandigital
def PandigitalProduct_1_9(n):
     
    i = 1
    while i * i <= n:
         
        if ((n % i == 0) and
             bool(isPandigital(str(n) +
                               str(i) +
                               str(n // i)))):
            return bool(True)
             
        i += 1
     
    return bool(False)
 
# To check the string formed from
# multiplicand multiplier and
# product is pandigital
def isPandigital(Str):
 
    if (len(Str) != 9):
        return bool(False)
         
    ch = "".join(sorted(Str))
     
    if (ch == "123456789"):
        return bool(True)
    else:
        return bool(False)
 
# Driver code
n = 6952
if (bool(PandigitalProduct_1_9(n))):
    print("yes")
else:
    print("no")
 
# This code is contributed by divyeshrabadiya07

C#

// C# code to check the number
// is Pandigital Product or not.
using System;
 
class GFG {
 
    // calculate the multiplicand,
    // multiplier, and product
    // eligible for pandigital
    public static bool PandigitalProduct_1_9(int n)
    {
        for (int i = 1; i*i <= n; i++)
            if (n % i == 0 && isPandigital("" + n
                                      + i + n / i))
                return true;
                 
        return false;
    }
 
    // To check the string formed from multiplicand
    // multiplier and product is pandigital
    public static bool isPandigital(String str)
    {
        if (str.Length != 9)
            return false;
             
        char []ch = str.ToCharArray();
        Array.Sort(ch);
         
        return new String(ch).Equals("123456789");
    }
 
    // Driver function
    public static void Main()
    {
        int n = 6952;
         
        if (PandigitalProduct_1_9(n) == true)
            Console.Write("yes");
        else
            Console.Write("no");
    }
}
 
// This code is contributed by nitin mittal.

PHP

<?php
// PHP code to check the number
// is Pandigital Product or not
 
// To check the string formed
// from multiplicand, multiplier
// and product is pandigital
function isPandigital($str)
{
    if (strlen($str) != 9)
        return false;
    $x = str_split($str);
    sort($x);
    $x = implode($x);
    return strcmp($x, "123456789");
}
 
// calculate the multiplicand,
// multiplier, and product
// eligible for pandigital
function PandigitalProduct_1_9($n)
{
    for ($i = 1;
         $i * $i <= $n; $i++)
        if ($n % $i == 0 &&
            isPandigital(strval($n) .
                         strval($i) .
                         strval((int)($n / $i))))
            return true;
    return false;
}
 
// Driver Code
$n = 6050;
if (PandigitalProduct_1_9($n))
    echo "yes";
else
    echo "no";
 
// This code is contributed
// by mits
?>

Javascript

// JavaScript code to check the number
// is Pandigital Product or not
 
// To check the string formed
// from multiplicand, multiplier
// and product is pandigital
function isPandigital(str)
{
    if (str.length != 9)
        return false;
     
    let ch = Array.from(str);
    ch.sort();
 
    let s = ch;
     
    if(s == "123456789")
        return true;
    else
        return false;
}
 
// calculate the multiplicand,
// multiplier, and product
// eligible for pandigital
function PandigitalProduct_1_9(n)
{
    for (let i = 1; i * i <= n; i++)
        if (n % i == 0 && isPandigital(n.toString() + i.toString() + (n / i).toString()));
            return true;
    return false;
}
 
// Driver Code
let n = 6952;
if (PandigitalProduct_1_9(n) == true)
    console.log("yes");
else
    console.log("no");
 
// This code is contributed by Nidhi goel

Producción: 

yes

Publicación traducida automáticamente

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