title: Logic of Ugly Numbers in C
body: <p>Following from <a href="http://www.geeksforgeeks.org/ugly-numbers/" rel="nofollow">here</a>, I am trying to develop my own logic to generate a sequence of ugly numbers. But every time all the numbers are printed.</p>
  
  <p>I am determining if the first 3 prime factors of the number are 2, 3 and 5 and placing them in a count variable determining the total count of prime factors of a number x.</p>
  
  <p>If the count is greater than 3, the number is not ugly. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>/* 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&lt;stdio.h&gt;
  #include&lt;math.h&gt;
  
  int isprime(int x)
  {
      int i;
      for(i=2;i&lt;=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 &gt; 3, then the number is not ugly
      int i;
      for(i=2;i&lt;=sqrt(x);i++)
      {
          if(isprime(i) &amp;&amp; x%i==0)
          {
              count++;
              if(count &gt; 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&lt;=n;i++)
      {
          if(isUgly(i))
              printf(" %d ",i);
      }
      return 0;
  }
  </code></pre>