原文:
给定一个整数 n,任务是打印前 n 个马赛克数字。马赛克数可以表示如下:
如果 n = p1t2【at41t7【p2t10【at122t15】…pkt18】at20kt23】在 n 的素因式分解中其中 p 1,p 2 … p 那么第 n 个镶嵌数等于((p1)(a1)((p2)(a2)……((pk)(ak)
示例:
输入:n = 10 t3】输出:1 2 3 4 5 6 7 6 10 对于 n = 4,n = 2 2 。 4 第镶嵌编号= 22 = 4 对于 n=8,n = 23t14】8第镶嵌编号= 23 = 6 同样打印前 n 个镶嵌编号
输入:n = 5 t3】输出: 1 2 3 4 5
逼近 : 运行一个从 1 到 n 的循环,对于每一个 i,我们必须通过将数除以因子直到因子除以数来找到所有的质因数以及数中因子的幂。然后,第 i 个马赛克数将是已发现的质因数及其幂的乘积。
下面是上述方法的实现:
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;
}
// 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 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
java 描述语言
output:
1 2 3 4 5 6 7 6 6 10
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处