This blog-post is about UVa 136: Ugly Number, a trivial, but interesting UVa problem. The crux involves computing 1500^{th} Ugly number, where a Ugly number is defined as a number whose prime factors are only 2, 3 or 5. Following illustrates a sequence of Ugly numbers:

1,2,3,4,5,6,8,9,10,12,15...

Using F#, we can derive 1500^{th} Ugly Number in F#’s REPL as follows.

In this context, the primary function the determines whether a number is a Ugly number or not–`isUglyNumber`

–is outlined as follows. As we can see, it is a naive algorithm that can be further optimized using memoization (as listed here).

After computing the 1500^{th} Ugly number in this manner, we submit the Java source code listed below. For complete source code. please visit this gist. Alternatively, this script is also available at tryfsharp.org for further introspection.

Please leave a comment in case of any question or improvement of this implementation. Thanks.