I'm working on a problem that asks to generate a sequence using prime numbers 2, 3, and 5, and then displaying then nth number in the sequence. So, if I ask the program to display the 1000th number, it should display it.
I can't be using arrays or anything like that, just basic decisions and loops.
I started working on it and hit a wall... here's what I got:
#include <iostream>
using namespace std;
int main() {
unsigned int n=23;
for(int i=2; i<n; i++){
if(i%2==0){
cout<<i<<", ";
}else if(i%3==0){
cout<<i<<", ";
}else if(i%5==0){
cout<<i<<", ";
}
}
return 0;
}
Unfortunately, that code doesn't do what's required. It displays numbers such as 14, which includes a prime number 7.... The numbers can only be divided by the 3 specified primes (2,3,5).
I found some information that I'm trying to understand, and so far not sure how to implement it... maybe using lots of for() loops? So, it appears I have to use the concept of 2^n * 3^m * 5^k where n+m+k>0.
I guess I have to run a number through a test where it checks to see first if it's fully divisible by 2^1 * 3^0 * 5^0, then 2^0 * 3^1 * 5^0, then 2^0 * 3^0 * 5^1, and so on... Just not sure where to begin.
Check this.