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.
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.
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.
EDIT:
#include <iostream>
#include <vector>
bool is_lucky(int num)
{
while(num!=0)
{
if((num%10!=4)&&(num%10!=7))
{
return false;
}
num/=10;
}
return true;
}
int main()
{
std::vector <long long> lucky;
for(int in_num=1;in_num<1000;in_num++)
{
if(is_lucky(in_num))
{
lucky.push_back(in_num);
}
}
}
Even though I am still using the for-if anti-pattern as Deduplicator pointed out, is the is_lucky
function comparable in efficiency as the ones below?
EDIT:
#include <iostream>
#include <vector>
bool is_lucky(int num)
{
while(num!=0)
{
if((num%10!=4)&&(num%10!=7))
{
return false;
}
num/=10;
}
return true;
}
int main()
{
std::vector <long long> lucky;
for(int in_num=1;in_num<1000;in_num++)
{
if(is_lucky(in_num))
{
lucky.push_back(in_num);
}
}
}
Even though I am still using the for-if anti-pattern as Deduplicator pointed out, is the is_lucky
function comparable in efficiency as the ones below?
Even though I am still using the for-if anti-pattern as Deduplicator pointed out, is the above function (almost) as efficientis_lucky
function comparable in efficiency as the ones below?
Even though I am still using the for-if anti-pattern as Deduplicator pointed out, is the above function (almost) as efficient as the ones below?
Even though I am still using the for-if anti-pattern as Deduplicator pointed out, is the is_lucky
function comparable in efficiency as the ones below?