Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numbers that only divide by 2,3 and/or 5, but not by any other prime number

Tags:

c++

math

I am given an integer N and I have to find the first N elements that are divisable only by 2,3 and/or 5, and not by any other prime number.

For example:

N = 3
Results: 2,3,4
N = 5
Results: 2,3,4,5,6

Mistake number = 55..55/5 = 11..11 which is a prime number. As 55..55 is divisable by a prime different from 2,3 and 5, it doesn't count.

I guess I need a recursive function, but I cant imagine what the algorithm would look like

like image 648
waplet Avatar asked Dec 19 '25 03:12

waplet


2 Answers

The only numbers that are only divisible by 2, 3 or 5 are the powers 2i × 3j × 5k for ijk = 0, 1, ....

Those numbers are easily generated.

like image 175
Kerrek SB Avatar answered Dec 20 '25 17:12

Kerrek SB


The numbers you're seeking are of the form 2^n * 3^m * 5^k, with n, m and k positive integers, with n+m+k > 0.

I'd pre-generate a sorted array and just print out the first N.

like image 27
Luchian Grigore Avatar answered Dec 20 '25 17:12

Luchian Grigore