📜  查找第N个镶嵌号

📅  最后修改于: 2021-05-04 16:41:01             🧑  作者: Mango

给定整数N ,任务是找到第N镶嵌数。镶嵌数可以表示为:
如果N = A a * B b * C c ,其中ABC ..是N的素数,则第N个镶嵌数将为A * a * B * b * C * c…

例子:

方法:通过将数字除以因子,直到因子除以数字,我们必须找到数字中的所有素因子以及因子的幂。然后,第N镶嵌数将是找到的素数及其功效的乘积。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}
  
// Driver code
int main()
{
    int n = 36;
    cout << mosaic(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
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;
}
  
// Driver code
public static void main (String[] args)
{
      
    int n = 36;
    System.out.println (mosaic(n));
}
}
  
// This code is contributed by jit_t.


Python
# Python3 implementation of the approach
  
# Function to return the nth mosaic number
def 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 = 36
print(mosaic(n))
  
# This code is contributed by mohit kumar 29


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;
}
  
// Driver code
static public void Main ()
{
    int n = 36;
    Console.WriteLine(mosaic(n));
}
}
  
// This code is contributed by ajit..


输出:
24