P0005. Hamming numbers

A natural number greater than zero is a Hamming number if its divisors
are only 2, 3 or 5. The twelve first Hamming numbers are 1, 2, 3, 4, 5,
8, 9, 10, 12, 15 and 16. However, neither 42 nor 97 are not Hamming
numbers: 42 is divisible by 7, and 97 is a prime number greater than 5.

Your task is to write a program that prints the n first Hamming numbers
for different values of n.

Your program must include and use the function

        bool is_hamming(int x);

that indicates if a natural number |x| greater than zero is a Hamming
number or is not.

Input

The input is a sequence of natural numbers.

Output

For each natural number n of the input print, in a line and separated by
commas, the first n Hamming numbers in increasing order.

Observation

There are astute ways to generate the n first Hamming numbers sorted. We
do not ask you to discover them: simply, implement a reasonable
algorithm.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T11:23:14.515Z

© Jutge.org, 2006–2026.
https://jutge.org
