IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

Crash et dépassement de capacité.


Sujet :

Langage C++

  1. #21
    Invité
    Invité(e)
    Par défaut
    Bon, 128 secondes pour trouver un nombre premier de 128 bits avec l'algo de miller-rabbin.

    Au sinon j'ai essayé d'utiliser gmp mais mon compilo m'indique à chaque fois qu'il ne trouve pas les fonctions de gmp (mpz_urandomb et companie), et pourtant j'ai bien inclus gmp.h et gmpxx.h.

    Donc là je sèche totalement! :/

    PS : au passage si quelqu'un peut m'aide à optimiser l'algo pour que ça ailler plus vite :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
     
    #include "../../../include/odfaeg/Network/bigInt.hpp"
     
    using namespace std;
    namespace odfaeg {
        BigInt::BigInt(const std::string& number) {
            base = 10;
            positif = (number.at(number.length()-1) != '-') ? true : false;
            nbChiffres = (positif) ? number.length() : number.length() - 1;
            for (unsigned int i = 0; i < nbChiffres; i++) {
                unsigned int c = number.at(i) - 48;
                if (c >= 0 && c < 10)
                    chiffres.push_back(c);
            }
        }
        BigInt::BigInt(unsigned long long integer, bool positif, unsigned int b) {
            this->positif = positif;
            this->base = b;
            nbChiffres = 0;
            if (integer > 0) {
                if (base < std::numeric_limits<unsigned int>::max()) {
                    unsigned long long int max = 1LL;
                    while (integer >= max)
                        max *= base;
                    max /= base;
                    while (max > 0) {
                        unsigned long long int c;
                        c = integer / max;
                        computeNbC(c);
                        chiffres.push_back(c);
                        integer %= max;
                        max /= b;
                    }
                } else {
                    unsigned long long int c = integer / base;
                    computeNbC(c);
                    chiffres.push_back(c);
                    integer %= base;
                    computeNbC(integer);
                    chiffres.push_back(integer);
                }
            }
        }
        BigInt BigInt::sqrt() {
            BigInt z = *this;
            BigInt rst;
            int max = 8;     // to define maximum digit
            int i;
            BigInt j(1, true, base);
            BigInt dix(10, true, base);
            BigInt bi (max, true, base);
            BigInt two(2, true, base);
            for(i = max ; i >= 0 ; i--){
                // value must be bigger then 0
                if(z - ((two * rst ) + ( j * dix.pow(i))*( j * dix.pow(i))) >= 0)
                {
                    while( z - (( two * rst ) + ( j * dix.pow(i)))*( j * dix.pow(i)) >= 0
                          && j >= 1)
                    {
                        j++;
                    }
                    j--; //correct the extra value by minus one to j
                    z -= (( two * rst ) + ( j * dix.pow(i)))*( j * dix.pow(i)); //find value of z25:
                    rst += j * dix.pow(i);     // find sum of a
                    j = 1;
                }
            }
            return rst;
        }
        void BigInt::computeNbC (unsigned long long int c) {
            unsigned int nb = base;
            unsigned int i = 1;
            while (c >= nb) {
                nb *= base;
                i++;
            }
            nbChiffres += i;
        }
        unsigned int BigInt::getNbChiffres() {
            return nbChiffres;
        }
        BigInt BigInt::genRandom (unsigned int nbBits, unsigned int base) {
           unsigned int nearestPowerOfTwo = math::Math::logn(base, 2);
           base = math::Math::power(2, nearestPowerOfTwo);
           BigInt n;
           n.base = base;
           n.nbChiffres = (nearestPowerOfTwo == 1) ? nbBits : nbBits / nearestPowerOfTwo;
           do {
                for (unsigned long long int i = 0; i < n.nbChiffres; ++i) {
                    unsigned int c = math::Math::random(base);
                    n.chiffres.push_back(c);
                }
     
            } while (n.isNull());
            n.shorten();
            return n;
        }
        BigInt BigInt::genPrime(unsigned int nbBits, unsigned int base) {
            BigInt p;
            do {
                p = genRandom(nbBits, base);
                std::cout<<"p : "<<p<<std::endl;
            } while(!miller(nbBits, p));
            return p;
        }
        bool BigInt::isPrime(unsigned int nbBits, BigInt& p) {
            // on effectue NB_ITER fois la boucle.
            if (p <= 1) return false;
            if (p % 2 == 0) return false;
            BigInt i (3, true, p.base);
            for (; i*i <= p; i+= 2) {
                //std::cout<<"i : "<<i<<std::endl;
                if (p % i == 0)
                    return false;
            }
            // le nombre est considéré comme premier
            return true;
        }
        int BigInt::jacobien(BigInt& a, BigInt& b)
        {
            if(a == 0)
                return 0;
            int J = 1;
            while(a != 1)
            {
                if(a % 2 == 0)
                {
                    if( ((b * b - 1) >> 3) % 2 != 0)
                        J *= -1;
     
                    a >>= 1;
                }
                else
                {
                    if( ((a - 1) * (b - 1) >> 2) % 2 != 0)
                        J *= -1;
     
                    BigInt temp = b % a;
                    b = a;
                    a = temp;
                }
            }
     
            return J;
        }
        BigInt BigInt::pgcd(BigInt& a, BigInt& b)
        {
            BigInt q = 0;
            while(b != 0)
            {
                q = a;
                a = b;
                b = q / b;
            }
            return a;
        }
        bool BigInt::isPositif () {
            return positif;
        }
        void BigInt::shorten () {
     
            vector<unsigned int>::iterator it;
            for (it = chiffres.begin(); it != chiffres.end(); ) {
                if (*it == 0) {
                    it = chiffres.erase(it);
                    nbChiffres--;
                } else
                    break;
            }
        }
        void BigInt::clear()
        {
            chiffres.clear();
        }
        bool BigInt::isNull() const {
            return chiffres.empty();
        }
        unsigned int BigInt::size() const {
            return chiffres.size();
        }
        unsigned int BigInt::operator[] ( unsigned int i ) const {
            return ( i<0 || i>=chiffres.size() ? 0 : chiffres[i] );
        }
        BigInt BigInt::operator++ (int i) {
            BigInt un(1, true, base);
            *this += un;
            return *this;
        }
        void BigInt::insert(unsigned int c, int pos) {
     
            if (pos <= size()) {
                BigInt result(0, true, base);
                result.chiffres.resize(size() + 1, 0);
                for (unsigned int i = 0, j = 0; i <= size(); i++, j++) {
                    if (i != pos) {
                        result.chiffres[i] = chiffres[j];
                    } else {
                        j--;
                    }
                }
                result.chiffres[pos] = c;
                computeNbC(c);
                *this = result;
            }
        }
        BigInt BigInt::operator-- (int i) {
            BigInt un(1, true, base);
            *this -= un;
            return *this;
        }
        int BigInt::comparaison (const BigInt &b) const {
            /*std::cout<<"sizes : "<<chiffres.size()<<" "<<b.chiffres.size()<<std::endl;
            std::string s;
            std::cin>>s;*/
            if (positif && !b.positif)
                return +1;
            if (!positif && b.positif)
                return -1;
            if ((chiffres.size() > b.chiffres.size() && positif && b.positif)
                || (chiffres.size() < b.chiffres.size() && !positif && !b.positif))
                return +1;
            if ((chiffres.size() < b.chiffres.size() && positif && b.positif)
                || (chiffres.size() > b.chiffres.size() && !positif && !b.positif))
                return -1;
     
            for(unsigned int i= 0; i < chiffres.size(); i++) {
     
                if ((chiffres[i] > b.chiffres[i] && positif && b.positif)
                    || (chiffres[i] < b.chiffres[i] && !positif && !b.positif))
                    return +1;
                if ((chiffres[i] < b.chiffres[i] && positif && b.positif)
                    || (chiffres[i] > b.chiffres[i] && !positif && !b.positif))
                    return -1;
            }
            return 0;
        }
        bool BigInt::operator== ( const BigInt& a ) const {
            return ( comparaison(a) == 0 );
        }
        bool BigInt::operator!= ( const BigInt& a) const {
            return ( comparaison(a) != 0 );
        }
        bool BigInt::operator< ( const BigInt& a ) const {
            return ( comparaison(a) < 0 );
        }
        bool BigInt::operator<= ( const BigInt& a ) const {
            return ( comparaison(a) <= 0 );
        }
        bool BigInt::operator> ( const BigInt& a) const {
            return ( comparaison(a) > 0 );
        }
        bool BigInt::operator>= ( const BigInt& a) const {
            return ( comparaison(a) >= 0 );
        }
        BigInt BigInt::operator-() const {
            BigInt result = *this;
            result.positif = !positif;
            return result;
        }
        BigInt BigInt::add (const BigInt& bi) const {
            if (isNull())
                return bi;
            if (bi.isNull())
                return *this;
            BigInt somme(0, true, base);
            somme.clear();
            unsigned int taille = max(this->size(), bi.size());
            somme.chiffres.resize( taille, 0);
     
            unsigned int retenue= 0;
            int i, j;
            for(i=size() - 1, j = bi.size() - 1; i >= 0; i--, j--)  {
     
                unsigned long long int temp = (unsigned long long int) (*this)[i] + (unsigned long long int) bi[j] + (unsigned long long int) retenue;
     
                if ( temp < base) {
                    somme.chiffres[i] = (unsigned int) temp;
                    retenue = 0;
                }
                else {
                    somme.chiffres[i]= (unsigned int) temp - base;
                    retenue = 1;
                }
            }
     
            while (retenue != 0) {
                if (i >= 0) {
                    unsigned long long int temp = (unsigned long long int) (*this)[i] + (unsigned long long int) bi[i] + (unsigned long long int) retenue;
     
                    if ( temp < base) {
                        somme.chiffres[i] = (unsigned int) temp;
                        retenue = 0;
                    } else {
                        somme.chiffres[i]= (unsigned int) temp - base;
     
                    }
                    i--;
                } else {
                    somme.insert(retenue, 0);
                    retenue = 0;
                }
            }
            somme.shorten();
            return somme;
        }
     
        BigInt BigInt::operator+ (const BigInt &bi) const {
            BigInt a = *this;
            BigInt b = bi;
            BigInt result(0, true, base);
            if (positif && bi.positif) {
                if (bi.size() > size()) {
                    a = bi;
                    b = *this;
                }
                result = a.add(b);
            } else if (!positif && bi.positif) {
                if (*this >= bi) {
                    result = a.sub(b);
                    result.positif = false;
                } else {
                    result = b.sub(a);
                }
            } else if (positif && !bi.positif) {
                if (*this >= bi) {
                    result = a.sub(b);
                } else {
                    result = b.sub(a);
                    result.positif = false;
                }
            } else {
               if (bi.size() > size()) {
                    a = bi;
                    b = *this;
               }
               result = a.add(b);
               result.positif = false;
            }
            return result;
        }
        BigInt& BigInt::operator+= (const BigInt &bi) {
            *this = *this + bi;
            return *this;
        }
        BigInt BigInt::sub (const BigInt &bi) const {
            BigInt diff(0, true, this->base);
            diff.clear();
     
            if (bi.isNull())
                return *this;
     
            unsigned int taille= max( this->size(), bi.size() );
            diff.chiffres.resize( taille, 0);
            // Calculer la somme chiffre par chiffre en tenant compte des retenues
            unsigned int retenue= 0;
            int i, j;
            for(i=size() - 1, j = bi.size() - 1; i>=0; i--, j--)  {
                long long temp = (long long) ((unsigned long long int) (*this)[i] - (unsigned long long int) bi[j] - (unsigned long long int) retenue);
     
                if ( temp >= 0 ) {
                    diff.chiffres[i]= (unsigned int) temp;
                    retenue = 0;
                }
                else {
                    diff.chiffres[i]= (unsigned int) (temp + base);
                    retenue = 1;
                }
            }
            while (retenue > 0) {
                long long int temp = (long long int) ((unsigned long long int) (*this)[i] - (unsigned long long int) retenue);
     
                if ( temp >= 0 ) {
                    diff.chiffres[i]= (unsigned int) temp;
                    retenue = 0;
                }
                else {
                    //std::cout<<*this<<" "<<bi<<" "<<diff.size()<<" "<<i<<std::endl;
                    diff.chiffres[i]= (unsigned int) (temp + base);
                }
                i--;
            }
            diff.shorten();
            return diff;
        }
        BigInt BigInt::operator- (const BigInt &bi) const {
            BigInt result(0, true, base);
            BigInt a = *this;
            BigInt b = bi;
            if (positif && bi.positif) {
                if (*this >= bi) {
                    result = a.sub(b);
                } else {
                    result = b.sub(a);
                    result.positif = false;
                }
            } else if (!positif && bi.positif) {
                if (bi.size() > size()) {
                    a = bi;
                    b = *this;
                }
                result = a.add(b);
                result.positif = false;
            } else if (positif && !bi.positif) {
                if (bi.size() > size()) {
                    a = bi;
                    b = *this;
                }
                result = a.add(b);
            } else {
                if (*this >= bi) {
                    result = a.sub(b);
                    result.positif = false;
                } else {
                    result.positif = true;
                    result = b.sub(a);
                }
            }
            return result;
        }
        BigInt& BigInt::operator-= (const BigInt &bi) {
            *this = *this - bi;
            return *this;
        }
        BigInt BigInt::addZeros (unsigned int n) {
     
            if (n < 0)
                return *this;
            if (isNull())
                return *this;
            BigInt result(0, positif, base);
     
            result.chiffres.resize(size() + n, 0);
            nbChiffres += n;
            for (int i = 0; i < size(); i++) {
                 result.chiffres[i] = chiffres[i];
            }
            return result;
        }
        BigInt BigInt::multiply (const BigInt &bi) const {
     
            BigInt produit(0, true, this->base);
            produit.clear();
            if ( this->size() == 0 || bi.size() == 0 ) {
                return produit;
            }
     
            int i = 0, j = bi.size() - 1;
            produit = scaleUp(bi.chiffres[i]).addZeros(j);
            while ( j > 0)
            {
               i++; j--;
               produit += scaleUp (bi.chiffres[i]).addZeros(j);
     
            }
            produit.shorten();
            return produit;
        }
        BigInt BigInt::karatsuba (const BigInt &bi) const {
            if (size() >= bi.size()) {
                unsigned int n = size() / 2;
                BigInt a = arraySub(0, size() - n);
                BigInt b = arraySub(size() - n, n);
                if (bi.size() > n) {
                    BigInt c = bi.arraySub(0, bi.size() - n);
                    BigInt d = bi.arraySub(bi.size() - n, n);
                    BigInt ac = a * c;
                    BigInt bd = b * d;
                    BigInt ad_bc = (a + b) * (c + d) - ac - bd;
                    ac = ac.addZeros(2*n);
                    ad_bc = ad_bc.addZeros(n);
                    return ac + ad_bc + bd;
                } else {
                    BigInt aq = a * bi;
                    BigInt bq = b * bi;
                    aq = aq.addZeros(n);
                    return aq + bq;
                }
            }
            return *this;
        }
        BigInt BigInt::operator* (const BigInt &bi) const {
            // D´el´eguer les petites multiplications `a la m´ethode scolaire
     
            BigInt result(0, true, base);
            if (size() < bi.size())
                result = bi * *this;
            else if (bi.size() < karatsuba_treshold)
                result = multiply(bi);
            else
                result = karatsuba(bi);
            if (!positif && bi.positif || positif && !bi.positif)
                result.positif = false;
            else
                result.positif = true;
            return result;
        }
        BigInt& BigInt::operator*= (const BigInt &bi) {
            *this = *this * bi;
            return *this;
        }
     
     
        BigInt BigInt::operator/ (const BigInt &bi) const {
            if (bi == 0) {
                cerr<<"Error : b is null!";
                return 0;
            }
            if (bi > *this) {
                return 0;
            }
            BigInt result(0, true, base);
            BigInt reste(0, true, base);
            result = burnikel_ziegler(bi, reste);
            if (!positif && bi.positif || positif && !bi.positif)
                result.positif = false;
            else
                result.positif = true;
            return result;
     
        }
        BigInt& BigInt::operator/= (const BigInt &bi) {
            *this = *this / bi;
            return *this;
        }
     
        BigInt BigInt::operator% (const BigInt& bi) const {
     
            if (bi == BigInt(0)) {
                cerr<<"Error : b is null!";
                return 0;
            }
            if (bi > *this)
                return *this;
            BigInt reste(0, true, base);
            burnikel_ziegler(bi, reste);
            reste.shorten();
            if (!positif && bi.positif || positif && !bi.positif)
                reste.positif = false;
            else
                reste.positif = true;
            return reste;
        }
        BigInt BigInt::pow (const BigInt& exp) {
            BigInt un(1, true, base);
            BigInt deux (2, true, base);
            BigInt zero (0, true, base);
            if (exp.isNull()) {
                return un;
            } else if (exp % deux == zero) {
                BigInt a = pow(exp / deux);
                return (a * a);
            } else {
                return (*this) * pow (exp - un);
            }
        }
        BigInt BigInt::prodMod (const BigInt &b, const BigInt &n) const {
          if(isNull()) {
            return 0;
          } else {
            // a = 2 * q  + e
            BigInt q = *this / BigInt(2, true, base);
            BigInt e = *this % BigInt(2, true, base);
            BigInt r = BigInt(2, true, base) * q.prodMod(b, n) % n;
            return e == 0 ? r : (r + b) % n;
          }
        }
        BigInt BigInt::modOfPow (const BigInt& exp, const BigInt& mod) const {
            BigInt result (1, true, base);
            BigInt one(1, true, base);
            BigInt two (2, true, base);
            BigInt zero(0, true, base);
            BigInt e = exp, bs;
            bs = *this;
            while (e > zero) {
     
                if (e % two == one)
                    result = result * bs % mod;
                e /= two;
                bs = bs * bs % mod;
            }
            return result;
        }
        BigInt& BigInt::operator%= (const BigInt &bi) {
            *this = *this % bi;
            return *this;
        }
        BigInt BigInt::arraySub (int n, int length) const {
            const_cast<BigInt*>(this)->shorten();
            BigInt result(0, true, this->base);
            if (n >= 0 && n + length <= size() && length  > 0) {
                result.chiffres.resize(length, 0);
                for (int i = n, j = 0; j < length; i++, j++) {
                    result.chiffres[j] = chiffres[i];
                }
            }
            return result;
        }
        BigInt BigInt::operator<< (int n) const {
            if (n < 0)
                return *this;
            if (isNull())
                return *this;
            BigInt result(0, true, base);
     
            result.chiffres.resize(size() + n, 0);
            for (int i = 0; i < size(); i++) {
                 result.insert(chiffres[i], 0);
            }
            return result;
        }
        BigInt BigInt::operator>> (int n) const {
            if (n < 0)
                return *this;
            if (isNull())
                return *this;
            if (n > size())
                n = size();
            BigInt result (0, true, base);
            result.chiffres.resize(size() - n, 0);
            for (unsigned int i = 0; i < result.size(); i++)
                result.chiffres[i] = chiffres[i];
            return result;
        }
        BigInt& BigInt::operator<<= (int n) {
            if (n < 0)
                return *this;
            if (isNull())
                return *this;
            BigInt result(0, true, base);
            result.chiffres.resize(size() + n, 0);
            for (int i = 0; i < size(); i++) {
                 result.insert(chiffres[i], 0);
            }
            *this = result;
            return *this;
        }
        BigInt& BigInt::operator>>= (int n) {
            if (n < 0)
                return *this;
            if (isNull())
                return *this;
            if (n > size())
                n = size();
            BigInt result (0, true, base);
            result.chiffres.resize(size() - n, 0);
            for (unsigned int i = 0; i < result.size(); i++)
                result.chiffres[i] = chiffres[i];
            *this = result;
            return *this;
        }
        BigInt BigInt::operator& (int n) const {
            BigInt a, b, bi(n);
            if (size() > bi.size()) {
                b = bi<<(size() - bi.size() - 1);
                a = *this;
            } else if (size() < bi.size()) {
                a = *this<<(bi.size() - size() - 1);
                b = bi;
            } else {
                a = *this;
                b = bi;
            }
            BigInt result (0, true, base);
            result.chiffres.resize(a.size(), 0);
            for (int i = 0; i < a.size(); i++) {
                result.chiffres[i] = chiffres[i] & b.chiffres[i];
            }
            result.shorten();
            return result;
        }
        BigInt BigInt::scaleUp (unsigned int n) const {
     
            unsigned long long int accu = 0;
            unsigned int retenue = 0;
            BigInt result(0, true, base);
            result.chiffres.resize(size() + 1, 0);
     
            if (n >= 0 && n < base) {
                if (n == 0)
                    return 0;
                for (int i = size(); i > 0; i--) {
                    accu = (unsigned long long int) chiffres[i-1] * n + (unsigned long long int) retenue;
                    result.chiffres[i] = (unsigned int) (accu % base);
                    retenue = (unsigned int) (accu / base);
                }
                result.chiffres[0] = retenue;
                if (retenue == 0)
                    result.shorten();
            }
            return result;
        }
        BigInt BigInt::scaleDown (unsigned long long int n, BigInt& r) const {
     
            unsigned long long int accu = 0;
            unsigned int retenue = 0;
            BigInt result = *this;
            if (n >= 0 && n < base * base) {
                for (unsigned int i = 0; i < size(); i++) {
                    accu = (unsigned long long) chiffres[i] + (unsigned long long int) retenue * base;
                    result.chiffres[i] = (unsigned int) (accu / n);
                    retenue = (unsigned int) (accu % n);
                }
     
            }
            r = BigInt(retenue, true, base);
            result.shorten();
            return result;
        }
        BigInt BigInt::burnikel_ziegler(const BigInt &bi, BigInt &r) const {
     
            BigInt q(0, true, base);
            if (bi.size() <= 2) {
                unsigned long long int b2 = (bi.size() < 2) ? bi.chiffres[0] : bi.chiffres[0] * base + bi.chiffres[1];
                q = scaleDown(b2, r);
                r.shorten();
                return q;
            }
            unsigned int n = (bi.size() - 1) / 2;
     
            // D´ecouper a et b en deux moiti´es
            BigInt a0, a1;
            a0 = arraySub(size() - n, n);
            a1 = arraySub(0, size() - n);
            if (a1 >= bi) {
     
                BigInt q1, r1, q0, r0;
                q1 = a1.burnikel_ziegler (bi, r1);
                r1 = r1.addZeros(n);
                q0 = (r1 + a0).burnikel_ziegler (bi, r0);
                q1 = q1.addZeros(n);
                r = r0;
                return q1 + q0;
            } else {
     
                BigInt b0, b1, q1, r1, a0_r1, b0_q1;
                b0 = bi.arraySub(bi.size() - n, n);
                b1 = bi.arraySub(0, bi.size() - n);
                q1 = a1.burnikel_ziegler(b1, r1);
                r1 = r1.addZeros(n);
                a0_r1 = r1 + a0;
                b0_q1 = b0 * q1;
                if (a0_r1 >= b0_q1) {
                    r = a0_r1 - b0_q1;
                    return q1;
                } else {
                    BigInt minus_x = b0_q1 - a0_r1;
                    r = bi - minus_x;
                    return q1 - BigInt(1, true, base);
                }
            }
        }
        BigInt BigInt::m_invert(const BigInt& b) const
        {
           BigInt m = *this;
           BigInt w = b;
           BigInt t0(0, true, base);
           BigInt t(1, true, base);
           BigInt q = *this / b;
           BigInt r = *this - q * b;
           BigInt tmp(0, true, base);
           BigInt zero(0, true, base);
           BigInt one(1, true, base);
     
           while(r > zero)
           {
             tmp = t0 - q * t;
     
             if (tmp >= zero)
             {
                tmp = tmp % m;
             }
             else
             {
                tmp = m - ((-tmp) % m);
             }
             t0 = t;
             t = tmp;
             m = w;
             w = r;
             q = m/w;
             r = m - q * w;
            }
     
            if (w!=one)
            {
                std::cerr<<w<<" haven't got invert modulo : "<<m<<std::endl;
                return w;
            }
            return t;
        }
        bool BigInt::miller(unsigned int nbBits, BigInt& n) {
            BigInt n1 = n-1;
            BigInt m = n1;
            unsigned int k = 1;
            BigInt zero(0, true, n.base);
            BigInt one(1, true, n.base);
            //std::cout<<"cond 1 : "<<(n1 % (BigInt(1 << (k + 2), true, n.base)) == zero)<<std::endl;
            while (n1 % (BigInt(1 << (k + 2), true, n.base)) == zero) {
                k += 2;
                m >>= 2;
                //std::cout<<(n1 % (BigInt(1 << (k + 2), true, n.base)) == zero)<<std::endl;
            }
            for (unsigned int i = 0; i < 10; i++) {
                BigInt a = genRandom(nbBits, n.base);
                //std::cout<<"a : "<<a<<" a >= n ?"<<(a >= n)<<std::endl;
                while (a >= n) {
                    //std::cout<<"a : "<<a<<" a >= n ?"<<(a >= n)<<std::endl;
                    /*std::string s;
                    std::cin>>s;*/
                    a = genRandom(nbBits, n.base);
                }
                BigInt b = a.modOfPow(m, n);
                if (b % n != one) {
                    unsigned int j;
                    for (j = 0; j < k && b % n != n1; ++j) {
                        b *= b;
                    }
                    if (j >= k)
                        return false;
                }
            }
            return true;
        }
        bool BigInt::isMersennePrime(const BigInt& p, unsigned int base) {
            if (p == 2) {
                return true;
            } else {
                BigInt s(4, true, base);
                BigInt div = BigInt(2, true, base).pow(p) - 1;
                for (BigInt i = 3; i <= p; i++) {
                    s = (s * s - BigInt(2, true, base)) % div;
     
                }
                return s == 0;
            }
        }
        BigInt BigInt::convert (unsigned int b) const {
     
            BigInt newBi = *this;
            if (b != base) {
     
                newBi = BigInt (0, true, 10);
                BigInt bs = BigInt(base, true, 10);
     
                for (int i = 0; i < size(); i++) {
                    BigInt exp (size() - i - 1, true, 10);
                    BigInt c1 (chiffres[i], true, 10);
                    BigInt c2 = c1 * bs.pow(exp);
                    newBi += c2;
                }
                BigInt max(1, true, 10);
                bs = BigInt (b, true, 10);
                BigInt r(0, true, 10);
     
                while (newBi >= max) {
                    max = max * bs;
                }
     
                max /= bs;
     
                newBi.shorten();
                BigInt a = newBi;
                newBi.clear();
                newBi.nbChiffres = 0;
                while (max > 0) {
                    BigInt c;
                    c = a / max;
                    if (c == 0) {
                        newBi.chiffres.push_back(0);
                        newBi.computeNbC(0);
                    } else {
                        unsigned int somme = 0;
                        for (int i = 0; i < c.size(); i++) {
     
                            somme += c.chiffres[i] * math::Math::power(10, c.size() - i - 1);
     
                        }
                        newBi.chiffres.push_back(somme);
                        a %= max;
                        newBi.computeNbC(somme);
                    }
                    max /= bs;
                }
                newBi.shorten();
                newBi.base = b;
                return newBi;
     
            }
            return newBi;
        }
     
        // Conversion valeur -> symbole pour la sortie
        char symbole( unsigned int valeur) {
     
            return ((valeur< 10) ? 48 + valeur : 55 + valeur);
        }
        // Op´erateur de sortie (afficher les chiffres, ou bien "0" pour une suite vide)
        ostream& operator<< ( ostream& out, const BigInt& bi) {
     
            if ( bi.size()== 0 ) return ( out << 0 );
            if (!bi.positif)
                    out<<"-";
            for( int i = 0; i < bi.size(); i++) {
     
                out<<symbole(bi.chiffres[i]);
                if (i != bi.size()-1)
                    out<<" ";
            }
            return out;
        }
    }

    Ca serait sympa, merci d'avance.

    PS : sinon, gmp n'a pas l'air de fonctionner ici, il ne m'affiche rien :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    #include <gmp.h>
    #include <gmpxx.h>
    int main() {
        gmp_randstate_t r_state;
        gmp_randinit_default(r_state);
        gmp_randseed_ui(r_state, time(nullptr));
        mpz_t rand_num;
        mpz_init(rand_num);
        mpz_urandomb(rand_num, r_state, 64);
        char* num=nullptr;
        mpz_get_str(num, 10, rand_num);
        std::cout<<num<<std::endl;
        return 0;
    }
    Dernière modification par Invité ; 09/07/2015 à 11h55.

  2. #22
    Membre régulier
    Homme Profil pro
    Cocher moderne
    Inscrit en
    Septembre 2006
    Messages
    50
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Oman

    Informations professionnelles :
    Activité : Cocher moderne

    Informations forums :
    Inscription : Septembre 2006
    Messages : 50
    Points : 118
    Points
    118
    Par défaut
    Salut,

    Sur 128 bits, on est sur des entiers qui peuvent atteindre 300 milliards de milliards de milliards de milliards (ouais, j'ai compté les chiffres, et alors? )

    Et tu te plains de trouver la primalité en 128 secondes ? Tu es peut-être un peu exigeant...

    Au fait, on est toujours dans le namespace de ton moteur de jeu. C'est quoi le rapport ?

    Bon courage dans tous les cas, et si les maths t'intéressent:
    Project Euler

    Moi, j'en suis là :

    Nom : landreagan.png
Affichages : 187
Taille : 6,8 Ko

    ... et c'est pas facile!

  3. #23
    Invité
    Invité(e)
    Par défaut
    Re, si j'ai besoin de 300 chiffres (en base 10) ça me fait combien de bits ?

    150 chiffres en base 10 ça ne correspondrait pas à 1024 bits ?

    C'est à peu prêt ça dont j'ai besoin.

    Wah ça à pas l'air du tout évidant...
    Dernière modification par Invité ; 09/07/2015 à 13h35.

  4. #24
    Membre émérite
    Avatar de white_tentacle
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    1 505
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 1 505
    Points : 2 799
    Points
    2 799
    Par défaut
    Citation Envoyé par Lolilolight Voir le message
    Re, si j'ai besoin de 300 chiffres (en base 10) ça me fait combien de bits ?
    De manière ultra-approximative, 10 bits de plus donnent 3 chiffres décimaux supplémentaires (10 bits -> 1024 -> 4 chiffres, 20*bits -> 7*chiffres, 30 bits -> 10 chiffres).

    Donc 300 chiffres, il te faut à la louche 1000 bits. 1024 ça parait pas mal pour ~300 chiffres.

    Wah ça à pas l'air du tout évidant...
    J’ai l’impression qu’il faut que tu te formes en mathématiques. C’est quelque chose de précis, qui ne s’improvise pas !

  5. #25
    Invité
    Invité(e)
    Par défaut
    Salut.

    J'ai trifouillé dans le code de openSSL et il ne semble pas du tout procéder de la même manière que moi, il génère simplement des nombres aléatoire, et pas des nombres premiers :

    http://www.codeproject.com/Questions...lusinplusC-b-b

    Je pourrai sans doute faire comme ça, sans m'embêter à générer des nombres premiers, la seule chose chose que je ne comprend pas c'est dans ce cas comment fait t'il pour générer n qui est un produit de deux grand nombre premiers.

    Si je veux que les clés fassent 300 chiffres, je pense que c'est 300 chiffres e ou d et n compris, donc n doit faire 512 bits donc, p * q = 512 bits et donc la taille de p doit faire à peu prêt 64 bits si je ne me trompe pas.

  6. #26
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 368
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 368
    Points : 23 620
    Points
    23 620
    Par défaut
    Citation Envoyé par Lolilolight Voir le message
    Re, si j'ai besoin de 300 chiffres (en base 10) ça me fait combien de bits ?
    150 chiffres en base 10 ça ne correspondrait pas à 1024 bits ?
    Citation Envoyé par white_tentacle Voir le message
    De manière ultra-approximative, 10 bits de plus donnent 3 chiffres décimaux supplémentaires (10 bits -> 1024 -> 4 chiffres, 20*bits -> 7*chiffres, 30 bits -> 10 chiffres).
    Donc 300 chiffres, il te faut à la louche 1000 bits. 1024 ça parait pas mal pour ~300 chiffres.
    Plus précisément, ça se fait avec les logarithmes :

    Formule mathématique

    Arrondi à l'unité supérieure, cela donne 997 bits pour représenter un entier décimal de 300 chiffres (donc strictement inférieur à 10300). Et en vérifiant à la calculatrice :

    2996 = 6,696928795×10²⁹⁹
    2997 = 1,339385759×10³⁰⁰

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. Réponses: 12
    Dernier message: 17/10/2014, 16h08
  2. Transaction, Dépassement de capacité
    Par SkYsO dans le forum MS SQL Server
    Réponses: 9
    Dernier message: 23/12/2008, 14h56
  3. Dépassement de capacité
    Par jean-pierre96 dans le forum Access
    Réponses: 2
    Dernier message: 10/05/2006, 16h04
  4. Réponses: 8
    Dernier message: 06/02/2006, 14h34
  5. détection de dépassement de capacité
    Par tut dans le forum C++
    Réponses: 10
    Dernier message: 01/12/2004, 22h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo