Algorithm to add all lucky numbers under [N] to a Vector A lucky number is defined as a positive integer whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. I need to check if a given number is evenly divisible by any lucky number or not.
Now, suppose I want to add all Lucky Numbers under a given integer [N] to a vector, without using recursion. For the sake of simplicity, let N = 1000.

I came up with an approach to just check each digit of all the numbers under [N] by making separate loops for 1 digit numbers, 2 digit numbers etc.
for(int number=0;number&lt;10;number++) {if(((number%10==4)||(number%10==7))) {lucky.push_back(number);}} //1 Digit Lucky Numbers
for(int number=10;number&lt;100;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))) {lucky.push_back(number);}} //2 Digit Lucky Numbers
for(int number=100;number&lt;1000;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))&amp;&amp;(((number/100)%10==7)||((number/100)%10==4))) {lucky.push_back(number);}} //3 Digit Lucky Numbers
for(int number=10;number&lt;100;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))&amp;&amp;(((number/100)%10==7)||((number/100)%10==4))&amp;&amp;(((number/1000)%10==7)||((number/1000)%10==4))) {lucky.push_back(number);}} //4 Digit Lucky Numbers

I was thinking that this could roughly be converted to something along these lines but I am not quite able to come up with what exactly to do.
for(number;number&lt;10*itr_counter;number++)
    {
    if(((number%10*itr_counter==4)||(number%10*itr_counter==7))) {lucky.push_back(number);}
    itr_counter*=10;
    }

I basically want to check each digit of all 1 digit numbers by taking modulo 10 and checking if the digits are 4 or 7. Similarly for a number consisting of X digits, I am taking modulo and dividing the number by 10, 100 and so on to check against 4 or 7.
Can someone help me optimise the first block of code into something smaller and more efficient? Something along the lines of the second block of code would work.
The Program
#include &lt;bits/stdc++.h&gt;
using namespace std;

int main()
{
int in_num;
cin&gt;&gt;in_num;

//This Part Needs to be Optimised
vector&lt;int&gt;lucky;
for(int number=0;number&lt;10;number++) {if(((number%10==4)||(number%10==7))) {lucky.push_back(number);}} //1 Digit Lucky Numbers
for(int number=10;number&lt;100;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))) {lucky.push_back(number);}} //2 Digit Lucky Numbers
for(int number=100;number&lt;1000;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))&amp;&amp;(((number/100)%10==7)||((number/100)%10==4))) {lucky.push_back(number);}} //3 Digit Lucky Numbers
for(int number=10;number&lt;100;number++) {if(((number%10==4)||(number%10==7))&amp;&amp;(((number/10)%10==7)||((number/10)%10==4))&amp;&amp;(((number/100)%10==7)||((number/100)%10==4))&amp;&amp;(((number/1000)%10==7)||((number/1000)%10==4))) {lucky.push_back(number);}} //4 Digit Lucky Numbers
//This Part Needs to be Optimised

int flag=0;
for(unsigned int index=0;index&lt;lucky.size();index++)
    {
    if(in_num%lucky[index]==0) {flag=1; break;}
    }
if(flag==1) {cout&lt;&lt;&quot;YES&quot;;}
else {cout&lt;&lt;&quot;NO&quot;;}
return 0;
}
It is fairly straightforward to generate a vector with all the values.  First, you should treat all the same length numbers as a group.  The values 4 and 7 would be in a singledigit vector.  To get the twodigit vector, simply go through the previous vector, multiplying each by 10 and then adding 4 to push back a value and add 7 to push back another value.

vector&lt;int&gt; lucky;
vector&lt;int&gt; current;
vector&lt;int&gt; next;
current.push_back(4);
current.push_back(7);
lucky.push_back(4);
lucky.push_back(7);
int value=0;
while(value &lt; N)
{
   for(unsigned int i=0;i&lt;current.size();++i)
   {
       value=current[i]*10+4;
       next.push_back(value);
       lucky.push_back(value);
       value=current[i]*10+7;
       next.push_back(value);
       lucky.push_back(value);
   }

   current=next;
   next.clear();
}