从here开始,我试图开发自己的逻辑来生成一系列丑陋的数字。但每次打印所有数字。C中的丑陋数字的逻辑
我正在确定数字的前3个素数因子是2,3和5,并将它们放置在一个计数变量中,以确定数字x的素数因子总数。
如果计数大于3,数字并不难看。
下面是代码:
/* To generate a sequence of Ugly numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers. By convention, 1 is included.
*/
#include<stdio.h>
#include<math.h>
int isprime(int x)
{
int i;
for(i=2;i<=sqrt(x);i++)
if(x%i==0)
return 0;
return 1;
}
int isUgly(int x)
{
int count=0; //To maintain the count of the prime factors. If count > 3, then the number is not ugly
int i;
for(i=2;i<=sqrt(x);i++)
{
if(isprime(i) && x%i==0)
{
count++;
if(count > 3)
return 0; // Not ugly
}
}
return 1;
}
int main(void)
{
int i,n=10;
printf("\n The ugly numbers upto %d are : 1 ",n);
for(i=2;i<=n;i++)
{
if(isUgly(i))
printf(" %d ",i);
}
return 0;
}
步骤虽然与调试器的代码。 – 2014-11-23 04:56:21
谷歌搜索'丑陋的数字'出现了[定义](http://www.geeksforgeeks.org/ugly-numbers/):_丑陋的数字是其唯一的主要因素是2,3或5的数字。按照惯例,所以包括1。因此,通过这个定义,如果可以重复(正好)2,然后3,然后5,并且值的序列结束于1,那么数字会变得很难看。如果它以其他方式结束数字,那么它不是丑陋的,并且序列中的最后一个值是除2,3或5以外的一个或多个素数的乘积。(谷歌搜索还揭示了关于该主题的一些SO问题。) – 2014-11-23 05:50:42