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
The only numbers that are only divisible by 2, 3 or 5 are the powers 2i × 3j × 5k for i, j, k = 0, 1, ....
Those numbers are easily generated.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With