Open In App

Brilliant Numbers

Last Updated : 16 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ implementation for the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate all prime
// numbers less than n
bool SieveOfEratosthenes(int n,
                bool isPrime[])
{
    // Initialize all entries of
    // boolean array as true.
    // A value in isPrime[i]
    // will finally be false
    // if i is Not a prime
    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] is not changed,
        // then it is a prime
        if (isPrime[p] == true) {
 
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                isPrime[i] = false;
        }
    }
}
 
// Function to return the number
// of digits in a number
int countDigit(long long n)
{
    return floor(log10(n) + 1);
}
 
// Function to check if N is a
// Brilliant number
bool isBrilliant(int n)
{
    int flag = 0;
 
    // Generating primes using Sieve
    bool isPrime[n + 1];
    SieveOfEratosthenes(n, isPrime);
 
    // Traversing all numbers
    // to find first pair
    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;
}
 
// Driver Code
int main()
{
    // Given Number
    int n = 1711;
 
    // Function Call
    if (isBrilliant(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java




// Java implementation for the
// above approach
import java.util.*;
class GFG{
 
// Function to generate all prime
// numbers less than n
static void SieveOfEratosthenes(int n,
                    boolean isPrime[])
{
    // Initialize all entries of
    // boolean array as true.
    // A value in isPrime[i]
    // will finally be false
    // if i is Not a prime
    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] is not changed,
        // then it is a prime
        if (isPrime[p] == true)
        {
 
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                isPrime[i] = false;
        }
    }
}
 
// Function to return the number
// of digits in a number
static int countDigit(int n)
{
    int count = 0;
        while (n != 0)
        {
            n = n / 10;
            ++count;
        }
        return count;
}
 
// Function to check if N is a
// Brilliant number
static boolean isBrilliant(int n)
{
    int flag = 0;
 
    // Generating primes using Sieve
    boolean isPrime[] = new boolean[n + 1];
    SieveOfEratosthenes(n, isPrime);
 
    // Traversing all numbers
    // to find first pair
    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;
}
 
// Driver Code
public static void main (String[] args)
{
    // Given Number
    int n = 1711;
 
    // Function Call
    if (isBrilliant(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Ritik Bansal


Python3




# Python3 program for the
# above approach
import math
 
# Function to generate all prime
# numbers less than n
def SieveOfEratosthenes(n, isPrime):
     
    # Initialize all entries of 
    # boolean array as true. 
    # A value in isPrime[i] 
    # will finally be false 
    # if i is Not a prime
    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] is not changed, 
        # then it is a prime
        if (isPrime[p] == True):
   
            # Update all multiples of p
            for i in range(p * 2, n + 1, p):
                isPrime[i] = False
         
        p += 1
   
# Function to return the number
# of digits in a number
def countDigit(n):
     
    return math.floor(math.log10(n) + 1)
   
# Function to check if N is a
# Brilliant number
def isBrilliant(n):
     
    flag = 0
   
    # Generating primes using Sieve
    isPrime = [0] * (n + 1)
    SieveOfEratosthenes(n, isPrime)
   
    # Traversing all numbers
    # to find first pair
    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 
   
# Driver Code
 
# Given Number
n = 1711
   
# Function Call
if (isBrilliant(n)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by sanjoy_62


C#




// C# implementation for the
// above approach
using System;
class GFG{
 
// Function to generate all prime
// numbers less than n
static void SieveOfEratosthenes(int n,
                       bool []isPrime)
{
     
    // Initialize all entries of
    // boolean array as true.
    // A value in isPrime[i]
    // will finally be false
    // if i is Not a prime
    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] is not changed,
       // then it is a prime
       if (isPrime[p] == true)
       {
            
           // Update all multiples of p
           for(int i = p * 2; i <= n; i += p)
              isPrime[i] = false;
       }
    }
}
 
// Function to return the number
// of digits in a number
static int countDigit(int n)
{
    int count = 0;
    while (n != 0)
    {
        n = n / 10;
        ++count;
    }
    return count;
}
 
// Function to check if N is a
// Brilliant number
static bool isBrilliant(int n)
{
    //int flag = 0;
 
    // Generating primes using Sieve
    bool []isPrime = new bool[n + 1];
    SieveOfEratosthenes(n, isPrime);
 
    // Traversing all numbers
    // to find first pair
    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;
}
 
// Driver Code
public static void Main()
{
    // Given Number
    int n = 1711;
 
    // Function Call
    if (isBrilliant(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
// Javascript implementation for the
// above approach
 
 
    // Function to generate all prime
    // numbers less than n
    function SieveOfEratosthenes( n, isPrime) {
        // Initialize all entries of
        // let array as true.
        // A value in isPrime[i]
        // will finally be false
        // if i is Not a prime
        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] is not changed,
            // then it is a prime
            if (isPrime[p] == true) {
 
                // Update all multiples of p
                for (let  i = p * 2; i <= n; i += p)
                    isPrime[i] = false;
            }
        }
    }
 
    // Function to return the number
    // of digits in a number
    function countDigit( n) {
        let count = 0;
        while (n != 0) {
            n = parseInt(n / 10);
            ++count;
        }
        return count;
    }
 
    // Function to check if N is a
    // Brilliant number
    function isBrilliant( n) {
        let flag = 0;
 
        // Generating primes using Sieve
        let isPrime = Array(n + 1).fill(true);
        SieveOfEratosthenes(n, isPrime);
 
        // Traversing all numbers
        // to find first pair
        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;
    }
 
    // Driver Code
      
        // Given Number
        let n = 1711;
 
        // Function Call
        if (isBrilliant(n))
            document.write("Yes");
        else
            document.write("No");
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

Yes

 

Time Complexity: O(n)

Auxiliary Space: O(n)

Reference: http://oeis.org/A078972
 



Similar Reads

Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
22 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Permutation of numbers such that sum of two consecutive numbers is a perfect square
Prerequisite: Hamiltonian Cycle Given an integer n(&gt;=2), find a permutation of numbers from 1 to n such that the sum of two consecutive numbers of that permutation is a perfect square. If that kind of permutation is not possible to print "No Solution". Examples: Input : 17 Output : [16, 9, 7, 2, 14, 11, 5, 4, 12, 13, 3, 6, 10, 15, 1, 8, 17] Expl
16 min read
Numbers within a range that can be expressed as power of two numbers
Given two integers L and R. Find the number of perfect powers in the given range [L, R]. A number x is said to be perfect power if there exists some integers a &gt; 0, p &gt; 1 such that x = ap. Examples : Input : 1 4 Output : 2 Explanation : Suitable numbers are 1 and 4 where 1 can be expressed as 1 = 12 and 4 can be expressed as 4 = 22 Input : 12
12 min read
Print all distinct integers that can be formed by K numbers from a given array of N numbers
Given an array of N elements and an integer K, print all the distinct integers which can be formed by choosing K numbers from the given N numbers. A number from an array can be chosen any number of times. Examples: Input: k = 2, a[] = {3, 8, 17, 5} Output: The 10 distinct integers are: 6 8 10 11 13 16 20 22 25 34 The 2 elements chosen are: 3+3 = 6,
7 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbers cannot be a perfect square.Examples: Input : N
8 min read
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
Given N and K, the task is to print N lines where each line contains 4 numbers such that every among those 4 numbers has a GCD K and the maximum number used in N*4 should be minimized.Note: In case of multiple outputs, print any one. Examples: Input: N = 1, K = 1 Output: 1 2 3 5 Every pair among 1, 2, 3 and 5 gives a GCD K and the largest number am
5 min read
Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between sum of non-prime numbers and prime numbers. Note: 1 is neither prime nor non-prime. Examples: Input : 1 3 5 10 15 7 Output : 10 Explanation: Sum of non-primes = 25 Sum of primes = 15 Input : 3 4 6 7 Output : 0 Naive Approach: A simple solution is to travers
16 min read
Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between product of non-prime numbers and prime numbers.Note: 1 is neither prime nor non-prime.Examples: Input : arr[] = {1, 3, 5, 10, 15, 7} Output : 45 Explanation : Product of non-primes = 150 Product of primes = 105 Input : arr[] = {3, 4, 6, 7} Output : 3 Naive
15 min read