Find the Nth Mosaic number

• Difficulty Level : Easy
• Last Updated : 05 May, 2021

Given an integer N, the task is to find the Nth Mosaic number. A Mosaic number can be expressed as follows:
If N = Aa * Bb * Cc where A, B, C.. are the prime factors of N then the Nth Mosaic number will be A * a * B * b * C * c ….

Examples:

Input: N = 8
Output:
8 can be expressed as 23
So, the 8th Mosaic number will be 2 * 3 = 6

Input: N = 36
Output: 24
36 can be expressed as 22 * 32
2 * 2 * 3 * 2 = 24

Approach: We have to find all the prime factors and also the powers of the factors in the number by dividing the number by the factor until the factor divides the number. The Nth Mosaic number will then be the product of the found prime factors and their powers.

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach#include using namespace std; // Function to return the nth mosaic numberint mosaic(int n){    int i, ans = 1;     // Iterate from 2 to the number    for (i = 2; i <= n; i++) {         // If i is the factor of n        if (n % i == 0 && n > 0) {            int count = 0;             // Find the count where i^count            // is a factor of n            while (n % i == 0) {                 // Divide the number by i                n /= i;                 // Increase the count                count++;            }             // Multiply the answer with            // count and i            ans *= count * i;        }    }     // Return the answer    return ans;} // Driver codeint main(){    int n = 36;    cout << mosaic(n);     return 0;}

Java

 // Java implementation of the approachimport java.io.*; class GFG{     // Function to return the nth mosaic numberstatic int mosaic(int n){    int i, ans = 1;     // Iterate from 2 to the number    for (i = 2; i <= n; i++)    {         // If i is the factor of n        if (n % i == 0 && n > 0)        {            int count = 0;             // Find the count where i^count            // is a factor of n            while (n % i == 0)            {                 // Divide the number by i                n /= i;                 // Increase the count                count++;            }             // Multiply the answer with            // count and i            ans *= count * i;        }    }     // Return the answer    return ans;} // Driver codepublic static void main (String[] args){         int n = 36;    System.out.println (mosaic(n));}} // This code is contributed by jit_t.

Python3

 # Python3 implementation of the approach # Function to return the nth mosaic numberdef mosaic(n):     i=0    ans = 1     # Iterate from 2 to the number    for i in range(2,n+1):         # If i is the factor of n        if (n % i == 0 and n > 0):            count = 0             # Find the count where i^count            # is a factor of n            while (n % i == 0):                 # Divide the number by i                n //= i                 # Increase the count                count+=1                          # Multiply the answer with            # count and i            ans *= count * i              # Return the answer    return ans # Driver code n = 36print(mosaic(n)) # This code is contributed by mohit kumar 29

C#

 // C# implementation of the approachusing System; class GFG{     // Function to return the nth mosaic numberstatic int mosaic(int n){    int i, ans = 1;     // Iterate from 2 to the number    for (i = 2; i <= n; i++)    {         // If i is the factor of n        if (n % i == 0 && n > 0)        {            int count = 0;             // Find the count where i^count            // is a factor of n            while (n % i == 0)            {                 // Divide the number by i                n /= i;                 // Increase the count                count++;            }             // Multiply the answer with            // count and i            ans *= count * i;        }    }     // Return the answer    return ans;} // Driver codestatic public void Main (){    int n = 36;    Console.WriteLine(mosaic(n));}} // This code is contributed by ajit..

Javascript


Output:
24

My Personal Notes arrow_drop_up