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. #1
    Invité
    Invité(e)
    Par défaut Crash et dépassement de capacité.
    Salut, j'ai effectuer une boucle qui recherche quel est la plus grande puissance de deux qu'il faut pour représenter un nombre en base deux.

    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
     
    BigInt BigInt::genPrime(unsigned int nbBits, unsigned int base) {
            BigInt p;
            BigInt tmp;
            BigInt two(2, true, base);
            BigInt exp(0, true, base);       
            p = genRandom(nbBits, base);
            while(p > two.pow(exp)) {
                std::cout<<"p : "<<p<<std::endl<<"exp : "<<two.pow(exp)<<" > ? "<<(p > two.pow(exp))<<std::endl;
                std::string s;
                std::cin>>s;
                exp+=1;
            }       
    }

    Mais ma boucle while ne fonctionne pas, le programme ne sort jamais de la boucle while et pourtant à un moment donné ma condition devient bien fausse vu que il m'affiche bien 0 pour (p > two.pow(exp)).

    Bon là à part un problème de compilo, je ne vois pas du tout ce que ça pourrait être.
    Dernière modification par Invité ; 07/07/2015 à 14h52.

  2. #2
    Invité
    Invité(e)
    Par défaut


    Parce que j'ai oublier de mettre le return il ne sort pas du while, bref ce comportement me surprend quand même.

    Si j'oublie le return à la fin de la fonction qu'est ce qui est sensé ce passé comme comportement ?

    Il ré-appelle sans cesse la fonction ?

    Ou bien c'est comportement indéterminé ?

    PS : bon j'ai eu la réponse à ma question, ça pointe vers une zone mémoire indéterminée donc c'est comportement indéterminé.
    Dernière modification par Invité ; 06/07/2015 à 12h49.

  3. #3
    Expert éminent sénior
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 629
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Analyste/ Programmeur

    Informations forums :
    Inscription : Juillet 2013
    Messages : 4 629
    Points : 10 554
    Points
    10 554
    Par défaut
    Je ne sais pas où tu as trouvé cette solution , mais pour convertir un décimal en base 2, il faut juste faire des divisions par 2

    Exemple: avec le nombre est 164
    • 164 ÷ 2 = 82 + 0
    • 82 ÷ 2 = 41 + 0
    • 41 ÷ 2 = 20 + 1
    • 20 ÷ 2 = 10 + 0
    • 10 ÷ 2 = 5 + 0
    • 5 ÷ 2 = 2 + 1
    • 2 ÷ 2 = 1 + 0
    • 1 ÷ 2 = 0 + 1

    On s'arrête lorsque le quotient vaut zéro et il faut lire le nombre à l'envers: 10100100

    Édit: Les 0 et les 1 sont le reste des divisions

  4. #4
    Membre chevronné Avatar de Astraya
    Homme Profil pro
    Consommateur de café
    Inscrit en
    Mai 2007
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Consommateur de café
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 043
    Points : 2 234
    Points
    2 234
    Par défaut
    Je reviens sur le problème.. Tu veux en fait arrondir à la puissance de deux supérieur ou inférieur?
    car il existe des algos optimisé pour ça.... et ce que tu propose n'es pas du tout correct en terme performance et même relecture..
    Homer J. Simpson


  5. #5
    Inactif  
    Homme Profil pro
    feignant
    Inscrit en
    Mars 2015
    Messages
    300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : feignant

    Informations forums :
    Inscription : Mars 2015
    Messages : 300
    Points : 0
    Points
    0
    Par défaut
    Voilà comment je fais pour arrondir à la puissance de deux supérieure:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    int upperPow2 ( int nb ) {
     int res = 1;
     while ( res < nb ) res <<= 1;
     return res;
    }

  6. #6
    Invité
    Invité(e)
    Par défaut
    Non, ce que je recherche à faire c'est vérifier si un nombre est premier :

    1 = 2 exp 1 - 2 exp 0
    3 = 2 exp 2 - 2 exp 1 + 2 exp 0
    5 = 2 exp 3 - 2 exp 2 + 2 exp 1 - 2 exp 0
    7 = 2 exp 3 - 2 exp 2 - 2 exp 1 + 2 exp 0
    11 = 2 exp 4 - 2 exp 3 + 2 exp 2 - 2 exp 1 + 2 exp 0.

    Pour l'instant mon algorithme fais comme ça : 2 exp 4 = 16, 16 >= 11 donc temp = 16 - 2 exp 3 = 8
    8 < 11 donc temp = 8 + 2 exp 2 = 12.
    12 >= 11 donc temp = 8 - 2 exp 1 = 10.
    10 < 11 donc temps = 10 + 2 exp 0 = 11.
    temp = p donc p est premier.
    Le soucis est que ça fonctionne pour tout les nombres impair, même les nombres pas premier.
    Si l'on prend par exemple 9 : 16 >= 9 donc temp = 16 - 2 exp 3 = 8
    8 < 9 donc temp = 8 + 2 exp 2 = 12.
    12 >= 9 donc temp = 8 - 2 exp 1 = 10.
    10 >= 9 donc temps = 10 - 2 exp 0 = 9.
    temp = p donc p est premier ce qui est faux!

    Je cherche donc un moyen d'exclure 9 (et donc les nombres impairs qui ne sont pas premier), si quelqu'un à une idée je suis preneur car ça fait plusieurs jours que je me casse la tête là dessus. :/

    Ce qui serait bien c'est de pouvoir déterminer les signes + ou - entre les différentes puissances de 2 en fonction de n mais je ne vois pas comment.

    Ainsi ça donnerait quelque chose comme : 9 = 2 exp 4 - 2 exp 3 + 2 exp 2 - 2 exp 1 + 2 exp 0 = 11 donc 9 n'est pas premier car 9 est différent de 11.

    Mais je ne me rappelle plus de la formule pour déterminer les signes!

    PS : j'ai essayé avec un test de miller-rabbin mais l'algorithme est beaucoup trop lent. :/
    Donc si quelqu'un pourrait m'aider à optimiser ce code ça serait bien. (Je pense que je vais tout convertir en puissance 16 pour les calculs!)

    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
    912
    913
    914
    915
    916
    917
     
    #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);
                }
            }
        }
        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) ? base : nbBits / base * 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;
            BigInt tmp;
            BigInt two(2, true, base);
            BigInt exp(0, true, base);
            BigInt zero(0, true, base);
            do {
                p = genRandom(nbBits, base);
                //std::cout<<"p : "<<p<<std::endl;
                while(p > two.pow(exp)) {
                    exp++;
                }
                tmp = exp;
                for (exp = exp - 1;exp > zero; exp-=1) {
                    if (tmp >= p) {
                        tmp -= two.pow(exp);
                    } else {
                        tmp += two.pow(exp);
                    }
                }
                if (tmp >= p) {
                    tmp -= 1;
                } else {
                    tmp += 1;
                }
            } while (tmp != p);
            return p;
        }
        bool BigInt::isPrime(unsigned int nbBits, BigInt& p) {
            // on effectue NB_ITER fois la boucle.
            BigInt pm1 = p - 1;
     
            for(unsigned int i = 0; i < 10; i++)
            {
                // recherche d'un nombre a<=p
                BigInt a = genRandom(nbBits, a.base);
                while(a >= p)
                    a = genRandom(nbBits, a.base);
     
                // si le pgcs de a et p est différent de 1, p n'est pas premier
                if(pgcd(a, p) != 1)
                    return false;
     
                // on vérifie si les 2 expressions suivantes sont égales
                BigInt val2 = a.modOfPow(pm1 >> 1, p);
                if(val2 != 1 && val2 != pm1)
                    return false;
     
                int val1 = jacobien(a, p);
                if(val1 == 1 && val2 != 1)
                    return false;
                if(val1 == -1 && val2 != pm1)
                    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 (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.chiffres.push_back(chiffres[i]);
            }
            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.chiffres.push_back(chiffres[i]);
            }
            *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);
            BigInt bs = BigInt(base, true, base);
            BigInt ni(n);
     
            if (ni >= 0 && ni < bs) {
                if (n == 0)
                    return 0;
                for (int i = size(); i > 0; i--) {
                    accu = (unsigned long long) chiffres[i-1] * n + (unsigned long long) 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);
                }
     
            }
            if((int) chiffres.size() -1 > 0 && result.chiffres[0] == 0)
                result.arraySub(1,chiffres.size()-1);
            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;
                }
            }
            std::cout<<"true!"<<std::endl;
            return true;
        }
        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;
        }
    }

    Merci d'avance.

  7. #7
    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

  8. #8
    Membre chevronné Avatar de Astraya
    Homme Profil pro
    Consommateur de café
    Inscrit en
    Mai 2007
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Consommateur de café
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 043
    Points : 2 234
    Points
    2 234
    Homer J. Simpson


  9. #9
    Invité
    Invité(e)
    Par défaut
    J'ai essayer plusieurs tests. (Celui de lucas leihmer)
    Mais le soucis c'est que les nombres sont tellement grand que même mon std::vector n'arrive plus à allouer assez de place pour les stocker!

    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
     
    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;
                    std::cout<<"s : "<<s<<std::endl;
                    std::string s;
                    std::cin>>s;
                }
                return s == 0;
            }
        }

    Quel est la taille max d'un std::vector par défaut, et comment l'augmenter ?

  10. #10
    Inactif  
    Homme Profil pro
    feignant
    Inscrit en
    Mars 2015
    Messages
    300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : feignant

    Informations forums :
    Inscription : Mars 2015
    Messages : 300
    Points : 0
    Points
    0
    Par défaut
    Désolé lolilolight j'avais rien compris

  11. #11
    Invité
    Invité(e)
    Par défaut Houla!!!
    Houlah, j'ai un dépassement de capacité et je ne peux pas réserver plus de la taille d'un unsigned short d'espace mémoire pour mon std::vector.

    Sinon j'ai une erreur du style bad_alloc!

    Ca crains, si j'essaye des tests successif de miller je n'ai pas de dépassement de capacité mais non seulement c'est trop lent mais en plus, ça crashe dans la fonction resize de std::vector si je l'appelle trop souvent. :/

    PS : je sens que je vais devoir utiliser gmp moi!

    Même si je n'ai jamais compris pourquoi cette lib est plus rapide surtout pour les tests de primalité.

  12. #12
    Inactif  
    Homme Profil pro
    feignant
    Inscrit en
    Mars 2015
    Messages
    300
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : feignant

    Informations forums :
    Inscription : Mars 2015
    Messages : 300
    Points : 0
    Points
    0
    Par défaut
    Je viens de lire ces sources, ça m'a l'air simple comme techniques, pas besoin de vecteurs ou chai pas quoi
    A vue de nez je ferais comme ça:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    bool isPrime(n) {
     bool prime = true;
     for (int i=2; i<n-1; i++) {
      if ( n%i == 0 ) {
       prime = false;
       break;
      }
     }
     return prime;
    }
    Par contre je crains que mon truc soit assez lent.
    Peut-être que lolilolight cherche une solution plus optimisée ?

  13. #13
    Membre émérite
    Avatar de prgasp77
    Homme Profil pro
    Ingénieur en systèmes embarqués
    Inscrit en
    Juin 2004
    Messages
    1 306
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur en systèmes embarqués
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Juin 2004
    Messages : 1 306
    Points : 2 466
    Points
    2 466
    Par défaut
    Citation Envoyé par Lolilolight Voir le message
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    bool BigInt::isMersennePrime(const BigInt& p, unsigned int base) {
            if (p == 2) {
                return true;
            } else {
                // ...
            }
        }
    Lolilolight, je me demande : pourquoi cette fonction prend-elle une base comme argument ? La notion de primalité est indépendante de la notion de base (tout comme la notion de passoire est indépendante de la notion de trous et réciproquement). De plus, 2 n'est pas un nombre premier de Mersenne : il n'est pas de la forme 2^p-1. Idem pour la suite : tu cherches si un nombre est premier, pas si c'est un mersenne.

    Cdlt,


    EDIT :
    Au temps pour moi, isMersennePrime(p, ...) teste si 2^p-1 est premier.
    -- Yankel Scialom

  14. #14
    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
    Mais le soucis c'est que les nombres sont tellement grand que même mon std::vector n'arrive plus à allouer assez de place pour les stocker!
    Reviens aux fondamentaux.

    De ce que j’ai compris, tu stockes tes nombres en décimal sous forme de chaîne. Donc un nombre de 10000 chiffres (c’est à dire un grand nombre) prend en gros 10k de mémoire.

    Admettons que tu manipules beaucoup de ces nombres, par exemple, un gros millier en même temps, ça ne devrait occuper en tout et pour tout qu’une dizaine de Mo de mémoire, ce qui est largement à la portée de n’importe quelle machine moderne.

    L’explication la plus logique : tu as une fuite mémoire (au hasard, dans ta classe BigInt).

    Par contre je crains que mon truc soit assez lent.
    Peut-être que lolilolight cherche une solution plus optimisée ?
    Il y a déjà deux optimisations triviales que tu peux faire qui vont diviser l’une par deux ton temps d’exécution, et l’autre d’un facteur encore plus grand dépendant de n.

  15. #15
    Membre éprouvé
    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2009
    Messages
    552
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mars 2009
    Messages : 552
    Points : 1 060
    Points
    1 060
    Par défaut
    Citation Envoyé par Lolilolight Voir le message
    PS : je sens que je vais devoir utiliser gmp moi!
    Tu deviens enfin raisonnable

  16. #16
    Invité
    Invité(e)
    Par défaut
    Il y a déjà deux optimisations triviales que tu peux faire qui vont diviser l’une par deux ton temps d’exécution, et l’autre d’un facteur encore plus grand dépendant de n.
    Ha oui, lesquelles ?

  17. #17
    Expert éminent sénior
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 629
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Analyste/ Programmeur

    Informations forums :
    Inscription : Juillet 2013
    Messages : 4 629
    Points : 10 554
    Points
    10 554
    Par défaut
    Citation Envoyé par Lolilolight Voir le message
    Ha oui, lesquelles ?
    La réponse a été donnée, et cela semble [très] logique ... mais c'est un truc mathématiques

    [Si je ne me plante pas comme un gland] Un exemple: 23 est premier
    L'algo donnait par stopviolence va diviser 23 par les nombres de 2 à 22.

    Or
    • 23 = 3 * 7 + 2
    • 23 = 6 * 3 + 5 = 3 * 2 * 3 + 5 = 3 * (2 * 3 + 1) + 2 = 3 * 7 + 2
    • 23 = 9 * 2 + 5 = 3 * 3 * 2 + 5 = 3 * 7 + 2
    • 23 = 12 * 1 + 11 = 3 * 4 * 1 + 9 + 2 = 3 * (4 + 3) + 2 = 3 * 7 + 2
    • 23 = 15 * 1 + 8 = 3 * 5 * 1 + 6 + 2 = 3 * (5 + 2) + 2 = 3 * 7 + 2
    • 23 = 21 * 1 + 2 = 3 * 7 + 2


    Tu vois bien qu'il y a des simplifications à faire sur les multiples des nombres et éventuellement un truc avec le reste (qui peut être un multiple d'un multiple ... [<- pas compréhensible ])

  18. #18
    gl
    gl est déconnecté
    Rédacteur

    Homme Profil pro
    Inscrit en
    Juin 2002
    Messages
    2 165
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 165
    Points : 4 637
    Points
    4 637
    Par défaut
    Plus simplement, vu que white_tentacle répondait par rapport à l'algo de stopviolence, je pense qu'il parlait surtout :
    • D'arrêter la boucle à sqrt(n) : un nombre supérieur ne peut pas être diviseur sans qu'un autre nombre inférieur le soit également, or ce nombre aurait déjà été découvert avant.
    • Si 2 n'est pas un diviseur, il ne sert à rien de tester les autres nombres pairs, autant commencer la boucle à 3 et incrémenter de 2.

    Qui sont des optimisations triviales (et classique sur ce genre d'algo).

  19. #19
    Invité
    Invité(e)
    Par défaut
    Ok, pour l'instant je ne vois pas trop comment exclure les multiples de 3 par exemple...
    Dernière modification par Invité ; 09/07/2015 à 08h46.

  20. #20
    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
    Ok mais quelqu'un n'aurait pas la fonction sqrt pour les grands nombres ?
    sqrt est une opération coûteuse, donc il vaut mieux comparer i*i à n que i à sqrt(n).

    Par ailleurs j'ai lu que cet algorithme serait encore plus lent qu'un algorithme probabiliste tel que celui de miller-rabbin.
    C’est effectivement le cas.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

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