patterncppMinor
Algorithm to add all lucky numbers under [N] to a Vector
Viewed 0 times
allnumbersluckyalgorithmundervectoradd
Problem
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.
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.
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 s
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<10;number++) {if(((number%10==4)||(number%10==7))) {lucky.push_back(number);}} //1 Digit Lucky Numbers
for(int number=10;number<100;number++) {if(((number%10==4)||(number%10==7))&&(((number/10)%10==7)||((number/10)%10==4))) {lucky.push_back(number);}} //2 Digit Lucky Numbers
for(int number=100;number<1000;number++) {if(((number%10==4)||(number%10==7))&&(((number/10)%10==7)||((number/10)%10==4))&&(((number/100)%10==7)||((number/100)%10==4))) {lucky.push_back(number);}} //3 Digit Lucky Numbers
for(int number=10;number<100;number++) {if(((number%10==4)||(number%10==7))&&(((number/10)%10==7)||((number/10)%10==4))&&(((number/100)%10==7)||((number/100)%10==4))&&(((number/1000)%10==7)||((number/1000)%10==4))) {lucky.push_back(number);}} //4 Digit Lucky NumbersI 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<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 s
Solution
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 lucky;
vector current;
vector next;
current.push_back(4);
current.push_back(7);
lucky.push_back(4);
lucky.push_back(7);
int value=0;
while(value < N)
{
for(unsigned int i=0;i<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();
}Code Snippets
vector<int> lucky;
vector<int> current;
vector<int> next;
current.push_back(4);
current.push_back(7);
lucky.push_back(4);
lucky.push_back(7);
int value=0;
while(value < N)
{
for(unsigned int i=0;i<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();
}Context
StackExchange Code Review Q#110582, answer score: 4
Revisions (0)
No revisions yet.