Sunday , December 11 2016
Home / C / Ugly Numbers Algorithm – C and Java Program

Ugly Numbers Algorithm – C and Java Program

C Program / Algorithm for Ugly Numbers:  What is an Ugly Number SeriesUgly Numbers are the ones whose prime factors are 2,3 or 5 Only. We have also given a Java Source code in Method #4

So Ugly Number Series is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, and so on…

The reason why 1 is added is because of the old convention and mostly 1 is added in many series may it be a Fibonacci series.

Ugly Numbers Source Code in C

  • Program #1
  • This approach has a time complexity of Big-O-Notation O(n).

    Splitting the Ugly number sequence of 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …into three groups –

    1. 1×2, 2×2, 3×2, 4×2, 5×2 . . .
    2. 1×3, 2×3, 3×3, 4×3, 5×3 . . .
    3. 1×5, 2×5, 3×5, 4×5, 5×5 . . .

    since each number can be divided by 2, 3, 5 only

    We can find that every sub sequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.

    Explanation:

     

    .

  • This is a trivial program.

  • Java Program: We have also listed a Ugly Number java Source code. This is a linear Solution of the Ugly Number series.

     

Also see:

Ugly Number C Program

Ugly Number Java Program

Ugly Numbers Algo

Also See:   c program for Snooker Animation in C language

Check Also

Hide Mouse Pointer C Program

How to Hide a Mouse Pointer Program in C : This program will show and hide ...

One comment

  1. 1 billionth ugly number:

    62565096724471903888424537973014890491686968126921250076541212862080934425144389
    76692222667734743108165348546009548371249535465997230641841310549077830079108427
    08520497989078343041081429889246063472775181069303596625038985214292236784430583
    66046734494015674435358781857279355148950650629382822451696203426871312216858487
    7816068576714140173718

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.