Pandigital Product
Last Updated :
03 Aug, 2023
A Pandigital Number is a number that makes the use of all digits 1 to 9 exactly once. We are given a number, we need to find if there are two numbers whose multiplication is given number and given three numbers together are pandigital.
Examples:
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
The idea is to consider all pairs that multiply to given number. For every pair, create a string containing three numbers (given number and current pair). We sort the created string and check if sorted string is equal to “123456789”.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPandigital(string str)
{
if (str.length() != 9)
return false ;
sort(str.begin(), str.end());
return str== "123456789" ;
}
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 ;
}
int main()
{
int n = 6952;
if (PandigitalProduct_1_9(n) == true )
cout << "yes" ;
else
cout << "no" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 ;
}
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" );
}
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
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 )
def isPandigital( Str ):
if ( len ( Str ) ! = 9 ):
return bool ( False )
ch = "".join( sorted ( Str ))
if (ch = = "123456789" ):
return bool ( True )
else :
return bool ( False )
n = 6952
if ( bool (PandigitalProduct_1_9(n))):
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
class GFG {
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 ;
}
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" );
}
public static void Main()
{
int n = 6952;
if (PandigitalProduct_1_9(n) == true )
Console.Write( "yes" );
else
Console.Write( "no" );
}
}
|
Javascript
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 ;
}
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 ;
}
let n = 6952;
if (PandigitalProduct_1_9(n) == true )
console.log( "yes" );
else
console.log( "no" );
|
PHP
<?php
function isPandigital( $str )
{
if ( strlen ( $str ) != 9)
return false;
$x = str_split ( $str );
sort( $x );
$x = implode( $x );
return strcmp ( $x , "123456789" );
}
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;
}
$n = 6050;
if (PandigitalProduct_1_9( $n ))
echo "yes" ;
else
echo "no" ;
?>
|
Time Complexity: O(n^1/2)
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...