Brilliant Numbers
Last Updated :
16 Jul, 2021
Brilliant Number is a number N which is the product of two primes with the same number of digits.
Few Brilliant numbers are:
4, 6, 9, 10, 14, 15, 21, 25, 35, 49….
Check if N is a Brilliant number
Given a number N, the task is to check if N is a Brilliant Number or not. If N is a Brilliant Number then print “Yes” else print “No”.
Examples:
Input: N = 1711
Output: Yes
Explanation:
1711 = 29*59 and both 29 and 59 have two digits.
Input: N = 16
Output: No
Approach: The idea is to find all the primes less than or equal to the given number N using Sieve of Eratosthenes. Once we have an array that tells all primes, we can traverse through this array to find a pair with a given product. We will find Two Prime Numbers with given product using the sieve of Eratosthenes and check if the pair has the same number of digits or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool SieveOfEratosthenes( int n,
bool isPrime[])
{
isPrime[0] = isPrime[1] = false ;
for ( int i = 2; i <= n; i++)
isPrime[i] = true ;
for ( int p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for ( int i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
int countDigit( long long n)
{
return floor ( log10 (n) + 1);
}
bool isBrilliant( int n)
{
int flag = 0;
bool isPrime[n + 1];
SieveOfEratosthenes(n, isPrime);
for ( int i = 2; i < n; i++) {
int x = n / i;
if (isPrime[i] &&
isPrime[x] and x * i == n) {
if (countDigit(i) == countDigit(x))
return true ;
}
}
return false ;
}
int main()
{
int n = 1711;
if (isBrilliant(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static void SieveOfEratosthenes( int n,
boolean isPrime[])
{
isPrime[ 0 ] = isPrime[ 1 ] = false ;
for ( int i = 2 ; i <= n; i++)
isPrime[i] = true ;
for ( int p = 2 ; p * p <= n; p++)
{
if (isPrime[p] == true )
{
for ( int i = p * 2 ; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static int countDigit( int n)
{
int count = 0 ;
while (n != 0 )
{
n = n / 10 ;
++count;
}
return count;
}
static boolean isBrilliant( int n)
{
int flag = 0 ;
boolean isPrime[] = new boolean [n + 1 ];
SieveOfEratosthenes(n, isPrime);
for ( int i = 2 ; i < n; i++)
{
int x = n / i;
if (isPrime[i] &&
isPrime[x] && (x * i) == n)
{
if (countDigit(i) == countDigit(x))
return true ;
}
}
return false ;
}
public static void main (String[] args)
{
int n = 1711 ;
if (isBrilliant(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def SieveOfEratosthenes(n, isPrime):
isPrime[ 0 ] = isPrime[ 1 ] = False
for i in range ( 2 , n + 1 , 1 ):
isPrime[i] = True
p = 2
while (p * p < = n ):
if (isPrime[p] = = True ):
for i in range (p * 2 , n + 1 , p):
isPrime[i] = False
p + = 1
def countDigit(n):
return math.floor(math.log10(n) + 1 )
def isBrilliant(n):
flag = 0
isPrime = [ 0 ] * (n + 1 )
SieveOfEratosthenes(n, isPrime)
for i in range ( 2 , n, 1 ):
x = n / / i
if (isPrime[i] and
isPrime[x] and x * i = = n):
if (countDigit(i) = = countDigit(x)):
return True
return False
n = 1711
if (isBrilliant(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static void SieveOfEratosthenes( int n,
bool []isPrime)
{
isPrime[0] = isPrime[1] = false ;
for ( int i = 2; i <= n; i++)
isPrime[i] = true ;
for ( int p = 2; p * p <= n; p++)
{
if (isPrime[p] == true )
{
for ( int i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static int countDigit( int n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
static bool isBrilliant( int n)
{
bool []isPrime = new bool [n + 1];
SieveOfEratosthenes(n, isPrime);
for ( int i = 2; i < n; i++)
{
int x = n / i;
if (isPrime[i] &&
isPrime[x] && (x * i) == n)
{
if (countDigit(i) == countDigit(x))
return true ;
}
}
return false ;
}
public static void Main()
{
int n = 1711;
if (isBrilliant(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function SieveOfEratosthenes( n, isPrime) {
isPrime[0] = isPrime[1] = false ;
for ( let i = 2; i <= n; i++)
isPrime[i] = true ;
for (let p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for (let i = p * 2; i <= n; i += p)
isPrime[i] = false ;
}
}
}
function countDigit( n) {
let count = 0;
while (n != 0) {
n = parseInt(n / 10);
++count;
}
return count;
}
function isBrilliant( n) {
let flag = 0;
let isPrime = Array(n + 1).fill( true );
SieveOfEratosthenes(n, isPrime);
for ( let i = 2; i < n; i++) {
let x = n / i;
if (isPrime[i] && isPrime[x] && (x * i) == n) {
if (countDigit(i) == countDigit(x))
return true ;
}
}
return false ;
}
let n = 1711;
if (isBrilliant(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Reference: http://oeis.org/A078972
Share your thoughts in the comments
Please Login to comment...