Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1587821861714395138
Commonmark migration
Source Link

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.

Rollback to Revision 5
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

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?

Included a new source code.
Source Link

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?

Included a new source code.
Source Link
Loading
added 80 characters in body
Source Link
Loading
deleted 334 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Improved Formatting and added the whole source code.
Source Link
Loading
added 50 characters in body
Source Link
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /