Friday, January 13, 2012

Humble Number Genarator A number whose only prime factors are 2,3,5 or 7 is called a humble number.

#include<stdio.h>


long humble[5842], min;


void humble_generator()
{
int index, p2, p3, p5, p7;
p2 = p3 = p5 = p7 = index =0;
humble[index++] = 1;


for(;index<5842;)
{
min = humble[p2]*2;


if(humble[p3]*3<min)  min = humble[p3]*3;
if(humble[p5]*5<min)  min = humble[p5]*5;
if(humble[p7]*7<min)  min = humble[p7]*7;


humble[index++] = min;


if(humble[p2]*2== min) p2++;
if(humble[p3]*3== min) p3++;
if(humble[p5]*5== min) p5++;
if(humble[p7]*7== min) p7++;
}
}




int main()
{
humble_generator();
int n,m;


while(scanf("%d",&n)==1 && n)
{
m=n%10;
if((n%100)/10==1) printf("The %dth humble number is %ld.\n",n,humble[n-1]);
else if(m==1)   printf("The %dst humble number is %ld.\n",n,humble[n-1]);
else if(m==2)   printf("The %dnd humble number is %ld.\n",n,humble[n-1]);
else if(m==3)   printf("The %drd humble number is %ld.\n",n,humble[n-1]);
else printf("The %dth humble number is %ld.\n",n,humble[n-1]);
}
return 0;
}
/*
Input:
1
2
3
4
11
12
13
21
22
23
100
1000
5842
Output:
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
*/

No comments:

Post a Comment