Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Print first N Mosaic numbers

  • Last Updated : 05 Oct, 2021

Given an integer N, the task is to print first N Mosaic numbers. A Mosaic number can be expressed as follows:

If N = p1a1p2a2…pkak in the prime factorization of N 
where p1 ,p2 … pk are prime numbers. 
Then the Nth Mosaic number is equal to ((p1)*(a1))*((p2)*(a2))*…*((pk)*(ak))
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:  



Input : N=10 
Output : 1 2 3 4 5 6 7 6 6 10 
For N = 4, N = 22
4th Mosaic number = 2*2 = 4 
For N=8 , N= 2 3 
8th Mosaic number = 2*3 = 6 
Similarly print first N Mosaic numbers

Input : N=5 
Output : 1 2 3 4 5
 

Approach
Run a loop from 1 to N and for every i 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 ith 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 <bits/stdc++.h>
using namespace std;
 
// Function to return the nth mosaic number
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;
}
 
// Function to print first N Mosaic numbers
void nMosaicNumbers(int n)
{
    for (int i = 1; i <= n; i++)
        cout << mosaic(i) << " ";
}
 
// Driver code
int main()
{
    int n = 10;
    nMosaicNumbers(n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
 
    // Function to return the nth mosaic number
    static 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;
    }
 
    // Function to print first N Mosaic numbers
    static void nMosaicNumbers(int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.print( mosaic(i)+ " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int n = 10;
        nMosaicNumbers(n);
    }
}
 
// This code contributed by Rajput-Ji

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the nth mosaic number
    static 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;
    }
 
    // Function to print first N Mosaic numbers
    static void nMosaicNumbers(int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write( mosaic(i)+ " ");
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 10;
        nMosaicNumbers(n);
    }
}
 
// This code is contributed by AnkitRai01

Python




# Python implementation of the approach
 
# Function to return the nth mosaic number
def mosaic( n):
    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 = n// i
 
                # Increase the count
                count+=1;
             
 
            # Multiply the answer with
            # count and i
            ans *= count * i;
     
 
    # Return the answer
    return ans;
 
 
# Function to print first N Mosaic numbers
def nMosaicNumbers(n):
    for i in range(1,n+1):
        print mosaic(i),
 
 
# Driver code
n = 10;
nMosaicNumbers(n);
 
# This code is contributed by CrazyPro

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the nth mosaic number
function mosaic(n)
{
    var 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) {
            var 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;
}
 
// Function to print first N Mosaic numbers
function nMosaicNumbers(n)
{
    for (var i = 1; i <= n; i++)
        document.write( mosaic(i) + " ");
}
 
// Driver code
var n = 10;
nMosaicNumbers(n);
 
</script>
Output: 
1 2 3 4 5 6 7 6 6 10

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!