954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help in finding 1500`th ugly number.

Hello friends..Ugly number is a number whose prime factors are only 1,2,3 or 5.
I had written a program for finding n`th ugly number in the series.Now the series is 1,2,3,4,5,6,8,9,10,12,15...these are first 11 ugly no`s.
Now In my program if we put m=11 then output will be 15.
It will work only up to m=239,Onwords it doesn`t work.
plz help me for finding 1500`th ugly number.

<strong>/*PROGRAM TO FIND AND PRINT 1500`th UGLY NUMBER*/</strong>
//UGLY NUMBER IS A NUMBER WHOSE PRIME FACTORS ARE 2,3 OR 5
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
 long int i;
 int j,k,l,p,n,m=240,c,f=0,x[20];
 int flag=1;
 clrscr();
 for(i=1;i<=500000;i++)
 {
 n=i;
 k=0;
 for(j=1;j<=i;j++)
 {
  if(n%j==0)
  {
   k++;
   x[k]=j;
   n=n/j;
   j=1;
   }
   if(x[k]!=1 && x[k]!=2 && x[k]!=3 && x[k]!=5)
   {
    flag=0;
    break;
    }
   }
   if(flag==1)
   {
  c=0;
  for(p=1;p<=k;p++)
  {
   if(x[p]==1 || x[p]==2 || x[p]==3 || x[p]==5)
    c++;
   }
   if(c==k)
   {
    f++;
    if(f==m)
    {
     clrscr();
     printf("\n\nThe 1500`th ugly number is <%ld>.",i);
     getch();
     exit(0);
     }
    }
   }
   flag=1;
  }
  printf("error");
  getch();
 }
nnhamane
Newbie Poster
15 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

void main()
{int n=1,i=0;
clrscr();
while(i<1500)
{ n++;
if(n%2==0||n%3==0||n%5==0)
i++;

}
printf("%d",n);


getch();
}

muraliaa
Newbie Poster
2 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

muraliaa, that has got to be one of the worst written codes ever, the indentation is all messed up, you havent used code tags, and you have your void main. Also you havent added any files to include, so the functions printf, getch and clrscr are all undefined. I wouldent consider this any help at all.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

Not to mention - 2 YEARS TOO LATE!

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You