c++numbershamming-numbers# Generating a sequence using prime numbers 2, 3, and 5 only, and then displaying an nth term (C++)

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.

Solution

Check this.

```
#include <iostream>
using namespace std;
int IsPrime(int var);
int CheckifPrimeGreaterThaFive(int Num);
int GetFactors(int Num)
{
int i =0,j=0;
for (i =2,j=0; i <= Num; i++)
{
if (Num%i == 0)
{
if (1 == CheckifPrimeGreaterThaFive(i))
{
return 1;
}
}
}
return 0;
}
int CheckifPrimeGreaterThaFive(int Num)
{
if ((Num != 2 && Num != 3 && Num != 5) && IsPrime(Num))
{
return 1;
}
return 0;
}
int IsPrime(int var)
{
for (int i = 2; i <= var/2; i++)
{
if (var % i == 0)
return 0;
}
return 1;
}
int main() {
int n=98;
int i, FactorsCount=0;
for(i=2; i<n; i++)
{
if (0 == GetFactors(i))
{
cout<<" "<<i;
}
}
return 0;
}
```

- C language server on Zed for SDL
- C Passing array of unknown size to a function in a single variable
- Return Functions Don't work. Using pythagorean Theorem
- While loop with nested if statements - C
- How to create 24 bit unsigned integer in C
- Are multiple variable-length arrays in a structure in C possible?
- Making recvfrom() function non-blocking
- Implications of explicitly casting a value to the type it gets returned as from a function?
- cs50 runoff infinite loop
- Can the unsigned keyword be used in non-obvious ways?
- In C/C++ what's the simplest way to reverse the order of bits in a byte?
- Dot Product function in C language
- How to read two lines with a random numbers count from input into two arrays
- RobotC - Programming an Elevator
- Strange behavior when adding longs in RobotC
- Do unsigned functions have to return something?
- Duplicate symbols in Microsoft C library
- Using ssize_t vs int
- undefined reference to `sctp_get_no_strms'
- Passing pointer of an array to a function in C
- C Program to find day of week given date
- Embedding Python in C, linking fails with undefined reference to `Py_Initialize'
- Converting python string object to c char* using ctypes
- State-of-the-art for embedding scriptable, interactive SVG in Gtk+ applications?
- Does the stack get freed after scope block?
- VS Code's C/C++ extension says the C23 true and false keywords are undefined
- How to use Docker compose in c program?
- How to declare a pointer to a character array in C?
- How to get one character at a time
- How to get a timeout to work when connecting to a socket