Lucky numbers

Given an input 'n' from the user, you need to find all lucky numbers from 1 to n. Lucky numbers are those which have either 4 or 7 or both as their digits ex:
4 7 447 477 44 47 etc..



I thought of this in many ways I just can't get it right AT ALL!! I tried doing it recursively using the formula of "Next lucky number is: X*10 +4 & X*10 +7" But I just can't do it e.e I can't seem to solve it in any way :/ anyone has any ideas?
Maybe like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;
// returns true if num contains only '4' and '7' digits, otherwise false
bool isLuckyNumber(int num)
{
  // convert num into  a string and check if it conly contains '4' and '7'
  //return either true or false
}

int main()
{
  cout << "Enter max. number: ";
  int num;
  cin >> num;

  // check that i is an int and > 1
  
  for (int i = 1; i < num; i++)
  {
    if (isLuckyNumber(i))
    {
      cout << i << " is a lucky number" << "\n";
    }
  }

  system("pause");
  return 0;
}

Maybe like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;
// returns true if num contains only '4' and '7' digits, otherwise false
bool isLuckyNumber(int num)
{
  // convert num into  a string and check if it conly contains '4' and '7'
  //return either true or false
}

int main()
{
  cout << "Enter max. number: ";
  int num;
  cin >> num;

  // check that i is an int and > 1
  
  for (int i = 1; i < num; i++)
  {
    if (isLuckyNumber(i))
    {
      cout << i << " is a lucky number" << "\n";
    }
  }

  system("pause");
  return 0;
}




Yes but like, how exactly to check that it ONLY contains 4's and 7's ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;

typedef unsigned long long INT;

vector<INT> lucky( INT n )
{
   vector<INT> luckyNumbers;
   vector<int> luckyDigits = { 4, 7 };

   // Single-digit lucky numbers
   for ( int i : luckyDigits )
   {
      if ( n < i ) return luckyNumbers;
      luckyNumbers.push_back( i );
   }

   // Successively higher numbers of digits constructed by preceding previous ones by lucky digits
   // NOTE: only add to numbers added in last pass, or there will be 0's in the sequence
   vector<INT> addedLastPass = luckyNumbers;
   vector<INT> added;
   INT powerOfTen = 1;
   while ( true )
   {
      powerOfTen *= 10;

      for ( int i : luckyDigits )
      {
         for ( INT p : addedLastPass )
         {
            INT newNum = i * powerOfTen + p;
            if ( newNum > n ) return luckyNumbers;
            luckyNumbers.push_back( newNum );
            added.push_back( newNum );
         }
      }
      addedLastPass = added;
      added.clear();
   }
}

//======================================================================

int main()
{
   INT n;

   cout << "Input n: ";
   cin >> n;

   vector<INT> luckyNumbers = lucky( n );

   cout << "Lucky numbers are:\n";
   for ( INT p : luckyNumbers ) cout << p << endl;
}


Input n: 45678
Lucky numbers are:
4
7
44
47
74
77
444
447
474
477
744
747
774
777
4444
4447
4474
4477
4744
4747
4774
4777
7444
7447
7474
7477
7744
7747
7774
7777
44444
44447
44474
44477
44744
44747
44774
44777

I managed to reach it in another solution using recursion, I hope it has no logical errors compared to yours @lastchance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>


#define fl(n) for(int i = 0; i < n; i++)


#define ll long long
#define nl endl
#define init -9999
#define INF 1e9
#define u unsigned


using namespace std;


u ll lucky[2048];
u ll Slucky[2048];

size_t sz = sizeof(lucky)/sizeof(lucky[0]);
size_t sz2 = sizeof(Slucky)/sizeof(Slucky[0]);

u ll Lucky_Sum(u ll x, u ll m, u ll n)
{
    static int c = 0, s = 0, z = 0;
    m = x;
    if(n < m) return x;


    if(x != 0)
    {
        lucky[c] = x;
        c++;
        s += x;
        Slucky[z] = s;
    }

    Lucky_Sum( (10*x) + 4, m, n);
    Lucky_Sum( (10*x) + 7, m, n);

return 1; //blah blah
}

int main()
{


    u ll LN;
    cout << "Number of lucky numbers to sum: " ;
    cin >> LN;

    Lucky_Sum(0,LN,LN);

    sort(lucky,lucky+sz);
    sort(Slucky,Slucky+sz2);

    cout << nl << "Lucky numbers found in the range 1 > " << LN << " were: " ;
    for(auto i : lucky)
        if(i != 0)
        cout << i << " ";
        cout << nl
             << nl;

    cout << "The sum of all the lucky numbers above is: ";
    for(auto i : Slucky) //useless loop , should subscript [0] & print cuz its always overwritten but yolo
        if(i != 0)
        cout << i;
        cout << nl;

   return 0;
}
Last edited on
Topic archived. No new replies allowed.