Luhn Algorithm Generator

0

Good I would like to ask for help with a project that I have to do is generate a numerical range of 10 digits and save it in a file, try to do something in c ++ but the vectors do not allow me to process such large numbers. What could I implement ?, I would have to do it in another language if someone has a suggestion I would appreciate it

#include <iostream>
#include <fstream>

using namespace std;

int main() {
int cedu=123266;    
int cedula[cedu], ced[cedu], 
pares[cedu], impares[cedu], total[cedu], 
dec[cedu], dectotal[cedu];
int a[cedu] ,b[cedu] ,c[cedu] ,d[cedu] ,e[cedu] ,f[cedu] ,g[cedu] ,h[cedu] ,i[cedu] ,j[cedu];
int n, m=0;
for (n=0; n<123266; n++){   
cout<<"Ingrese su cedula n: "<<n<<endl;
cedula[n]=n;
ced[n]=cedula[n];
a[n]=cedula[n]/1000000000;
cedula[n]=cedula[n]-(a[n]*1000000000);
b[n]=cedula[n]/100000000;
cedula[n]=cedula[n]-(b[n]*100000000);
c[n]=cedula[n]/10000000;
cedula[n]=cedula[n]-(c[n]*10000000);
d[n]=cedula[n]/1000000;
cedula[n]=cedula[n]-(d[n]*1000000);
e[n]=cedula[n]/100000;
cedula[n]=cedula[n]-(e[n]*100000);
f[n]=cedula[n]/10000;
cedula[n]=cedula[n]-(f[n]*10000);
g[n]=cedula[n]/1000;
cedula[n]=cedula[n]-(g[n]*1000);
h[n]=cedula[n]/100;
cedula[n]=cedula[n]-(h[n]*100);
i[n]=cedula[n]/10;
cedula[n]=cedula[n]-(i[n]*10);
j[n]=cedula[n]/1;
cedula[n]=cedula[n]-(j[n]*1);

//cout<<cedula<<endl<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl<<f<<endl<<g<<endl<<h<<endl<<i<<endl<<j<<endl;
if (cedula[n]>2400000000){
    cout<<"Numero de cedula invalido."<<endl;
}
else{       
    pares[n]=b[n]+d[n]+f[n]+h[n];
    a[n]=a[n]*2;
    if (a[n]>9){
        a[n]=a[n]%10+a[n]/10;
        }       
    c[n]=c[n]*2;
    if (c[n]>9){
        c[n]=c[n]%10+c[n]/10;
        }
    e[n]=e[n]*2;
    if (e[n]>9){
        e[n]=e[n]%10+e[n]/10;
        }
    g[n]=g[n]*2;
    if (g[n]>9){
        g[n]=g[n]%10+g[n]/10;
        }
    i[n]=i[n]*2;
    if (i[n]>9){
        i[n]=i[n]%10+i[n]/10;
        }
    impares[n]=a[n]+c[n]+e[n]+g[n]+i[n];
    total[n]=pares[n]+impares[n];   
    } 
    dec[n]=0;   
    while (dec[n]-total[n]!=j[n] && dec[n]<total[n]+10){
        dec[n]=dec[n]+10;
        dectotal[n]=dec[n]-total[n];
    }       
}
ofstream fs("Cedula.txt");
cout<<endl;
for (n=0; n<123266; n++){       
    m++;
    if (dectotal[n]==j[n]){
        ced[m]=ced[n];          
        if(a[n]==0){
        cout<<"0"<<ced[m]<<endl;
        fs<<"0"<<ced[m]<<endl;
        }
        else{
        cout<<ced[m]<<endl;
        fs<<ced[m]<<endl;
        }
    }
    else{
    }       
}
fs.close();     
return 0;
}

Until here, it only processes 123266 numbers

    
asked by Arifactory 10.12.2018 в 07:32
source

1 answer

1

int is a 32-bit signed type, then the maximum value that you can store in a variable of this type is 2^31 - 1 = 2.147.483.647 . If you intend to store 10-digit numbers in this type, we're wrong, since the vast majority of those numbers do not enter .

To solve this you have at your disposal the type long long , which are 64 bits with sign, and unsigned long long to work without sign. The range of these types increases appreciably with respect to the 32-bit types:

long long          -> 2^63 - 1 =  9.223.372.036.854.775.807
unsigned long long -> 2^64 - 1 = 18.446.744.073.709.551.615

As you can see, these types support numbers of up to 19/20 digits, then you should not have problems working with 10-digit numbers

  

try to change the data type unsigned long long and enlarge the vector to 10 digits but when compiling I get the error violation of segment

A classic in the C ++ world:

int cedu=10;    
int cedula[cedu], ced[cedu], 

If cedula , ced , and company have 10 elements, the iteration range must be 0 to 9 and not% 1 to 10 :

// BIEN
for (n=0; n<10; n++){  

// MAL
for (n=1; n<=10; n++){  

On the other hand, keep in mind that cedu is a variable , so cedula , cedu and so on become VLA (Variable Length Array). This type of elements is not valid according to the C ++ standard, then your code will not be portable and may give problems in some compilers. I suggest changing the declaration of cedu :

int const cedu = 10;

In this way you make it clear to the compiler that cedu is a constant and that its value will not change, which removes the problem of portability.

    
answered by 10.12.2018 в 08:06