Home > Back-end >  Write a C program that finds and lists all possible correct credit card numbers according to the L
Write a C program that finds and lists all possible correct credit card numbers according to the L

Time:01-07

I need to print all possible correct credit card numbers according to the Luhn algorithm, and I did like a 16 for loops nested inside each other, I was wondering if there is a way to make my code shorter? Here is my code:

this is for index 0,2,4,6,8,10,12,14,16 It takes the number and multiply it by 2. and after that it takes the sum of the individual numbers; lets say index 0 is 6 it takes 6*2=12 and sums 1 2

int dbl(int x) {
    int sum = 0;
    while (x !=0)
    {
        sum  = x % 10;
        x /= 10;
    }
    return sum;
}

void bruh(int x, int y) {
    
    x  = (dbl(2 * y));
}

This is my loops:

int main()
{
    for (int i = 0; i <= 9; i  )
    {
        for (int j = 0; j <= 9; j  )
        {
            for (int k = 0; k <= 9; k  )
            {
                for (int l = 0; l <= 9; l  )
                {
                    for (int m = 0; m <= 9; m  )
                    {
                        for (int n = 0; n <= 9; n  )
                        {
                            for (int o = 0; o <= 9; o  )
                            {
                                for (int p = 0; p <= 9; p  )
                                {
                                    for (int q = 0; q <= 9; q  )
                                    {
                                        for (int r = 0; r <=9; r  )
                                        {
                                            for (int s = 0; s <= 9; s  )
                                            {
                                                for (int u = 0; u <= 9; u  )
                                                {
                                                    for (int v = 0; v <= 9; v  )
                                                    {
                                                        for (int x = 0; x <= 9; x  )
                                                        {
                                                            for (int w = 0; w <= 9; w  )
                                                            {
                                                                for (int y = 0; y <= 9; y  )
                                                                {
                                                                    int dbles = 0, sngls =0;
                                                                    bruh(dbles, i);
                                                                    sngls  = j;
                                                                    bruh(dbles, k);
                                                                    sngls  = l;

                                                                    bruh(dbles, m);
                                                                    sngls  = n;
                                                                    bruh(dbles, o);
                                                                    sngls  = p;

                                                                    bruh(dbles, q);
                                                                    sngls  = r;
                                                                    bruh(dbles, s);
                                                                    sngls  = u;

                                                                    bruh(dbles, v);
                                                                    sngls  = x;
                                                                    bruh(dbles, w);
                                                                    sngls  = y;
                                                                    if (dbles sngls==0)
                                                                    {
                                                                        cout << "Valid Number: " << i << j << k << l << m << n << o << p << q << r << s << u << v << x << w << y << endl;
                                                                        cout << "---------------" << endl;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

The program works fine(added a break statement to stop after 100th cc to check if it runs, And it dose), I just want to know if there is away to make my code shorter

CodePudding user response:

Something like this will be much shorter and easier to read:

#include <string>

void increment(std::string& s) {
    for (int i = s.length() - 1; i >= 0; --i) {
        if (s[i] != '9') {
            s[i]  ;
            return;
        }
        s[i] = '0';
    }
}

int main()
{
    std::string s = "0000000000000000";
    for (uint64_t i = 0; i < 1'000'000'000'000'000;   i) {
        // do your check with s[0]..s[15]
        increment(s);
    }
}

NOTE: this does pretty much what you tried to do in your code.

You can take n0rd's suggestion and loop through 15 digit numbers, calculating the checksum and appending it to the end. In that case you'd only need 3,000 years instead of 30,000 :)

  •  Tags:  
  • Related