Files
/
kGMP.cc
Latest commit
628 lines (597 loc) · 24.6 KB
/
kGMP.cc
438
void handelf(family f)
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
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
#include <list>
#include <cmath>
#include <string>
#include <random>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <gmpxx.h>
#include <gmp.h>
#define cyclemax 12
using namespace std;
// code base mxlg [d1[@d2] [d3[@d4]]]
// tries to extend the current kernel for base with primes up to length mxlg
// if d1 is specified, we only consider families starting with digit d1 (up to d2 if specified)
// if d3 is specified, we only consider families ending with digit d3 (up to d4 if specified)
// new found primes are gathered in a file newprimes{base}-suffix
// and families to be still analyzed are gathered in a file left{base}-suffix
// details of the exploration are send to the standard output
char tr[128]; int w[128];
using family=vector <string>;
using sint=long unsigned int; // std::vector<std::__cxx11::basic_string<char> >::size_type;
vector <string> found;
ifstream kern; ofstream result,reject,court;
sint b;
vector <sint> fact; string covering;
clock_t cpustarttime,cputime;
mpz_class nt,nprime,nlong,n1,y,z,z1,z2,bx,zz1,zz2,zzz;
sint k,lgmax,nbsmall;
mpz_t rtt,rt1,rt2;
random_device rd;
mt19937 gen(rd()); // set random gen
string s10(mpz_class x)
{mpz_class y=x; int r; string s="";
while (y!=0){r=mpz_class{y%10}.get_si();y=y/10;s=tr[r]+s;}
return s;
}
string sb(mpz_class x)
{mpz_class y=x; int r; string s="";if(x==0)return "0";
while (y!=0){r=mpz_class{y%b}.get_si();y=y/b;s=tr[r]+s;}
return s;
}
string familystrip(family f)
{string ss; sint i;
ss="";
for(i=0;i<f.size();i=i+2)ss=ss+f[i];
return(ss);
}
string stringf(family f)
{sint i; string s;if(f.size()==1)return f[0];
if(f.size()==1){s=f[0];return s;}
for(i=0;i<f.size()-2;i=i+2)s=s+f[i]+"{"+f[i+1]+"}";
s=s+f[i];
return s;
}
long long gcd(long long i,long long j)
{long long ii,jj,kk; if(i==0)return j;if(j==0)return i;
if(j>i){ii=j;jj=i;} else {ii=i;jj=j;}
while(jj!=0){kk=ii%jj;ii=jj;jj=kk;}
return ii;}
mpz_class gcd(mpz_class i,mpz_class j)
{mpz_class ii,jj,kk; if(i==0)return j;if(j==0)return i;
if(j>i){ii=j;jj=i;} else {ii=i;jj=j;}
while(jj!=0){kk=ii%jj;ii=jj;jj=kk;}
return ii;}
for(k2=1;k2<cyclemax;k2++)
{pj2="";dk2=dk2*b;ddk2=ddk2*b+w[f[i2][0]];
for(j2=0;j2<k2;j2++) // fam= fam1(pj1){dk1}fam2(pj2){dk2}fam3
{ff[i2-1]=f[i2-1]+pj2; ff[i2]=sb(ddk2);pj2=pj2+f[i2];
for(ii=0;ii<nbsmall;ii++)
{iff=fact[ii];n1=0;
for(iii=0;iii<ff.size();iii++)
{if(iii%2==0)
{for(ii2=0;ii2<ff[iii].size();ii2++)
n1=(n1*b+w[ff[iii][ii2]])%iff;}
else
{if(iii==i1){if(n1!=((n1*dk1+ddk1)%iff))break;} else
if(iii==i2){if(n1!=((n1*dk2+ddk2)%iff))break;} else
{for(ii2=0;ii2<f[iii].size();ii2++)
{if(n1!=((n1*b+w[f[iii][ii2]])%iff))break;}
if(ii2!=f[iii].size())break; } } }
if(iii==ff.size()&&n1==0){cycles[j1][j2]=iff;break;}}
if(ii==nbsmall)break;}
if(j2==k2)break;}
if(k2==cyclemax)break;}
if(j1==k1)break;}
if(k1!=cyclemax)
{cout<<" rectangular cycle of factors of length ("<<k1<<","<<k2<<") ";
cout<<"at positions (" <<i1<<","<<i2<<") :\n";
for(j1=0;j1<k1;j1++)
{cout<<"(";for(j2=0;j2<k2;j2++)cout<<cycles[j1][j2]<<",";
cout<<")\n";
}
return true;
}
}
return false;
}
bool par3(family f) // check if a ternary choice has cyclic even/odd factors
{if(f.size()<7)return false;
sint i1,i2,i3,j1,j2,j3,ii,ii2,iii,iff;mpz_class n1,dk1,dk2,ddk1,ddk2;
int cycles[2][2][2]; string pj1,pj2;family ff;
for(i1=1;i1<f.size();i1=i1+2)if(f[i1].size()==1)
for(i2=i1+2;i2<f.size();i2=i2+2)if(f[i2].size()==1)
for(i3=i2+2;i3<f.size();i3=i3+2)if(f[i3].size()==1)
{
for(j1=0;j1<2;j1++){for(j2=0;j2<2;j2++){for(j3=0;j3<2;j3++)
{ff=f;ff[i1]=f[i1]+f[i1];ff[i2]=f[i2]+f[i2];ff[i3]=f[i3]+f[i3];
if(j1==1)ff[i1-1]=f[i1-1]+f[i1];
if(j2==1)ff[i2-1]=f[i2-1]+f[i2];
if(j3==1)ff[i3-1]=f[i3-1]+f[i3];
for(ii=0;ii<nbsmall;ii++)
{iff=fact[ii];n1=0;
for(iii=0;iii<ff.size();iii++)
{if(iii%2==0)
{for(ii2=0;ii2<ff[iii].size();ii2++)
n1=(n1*b+w[ff[iii][ii2]])%iff;}
else
{if(iii==i1){if(n1!=((n1*b*b+(b+1)*w[f[iii][0]])%iff))break;} else
if(iii==i2){if(n1!=((n1*b*b+(b+1)*w[f[iii][0]])%iff))break;} else
if(iii==i3){if(n1!=((n1*b*b+(b+1)*w[f[iii][0]])%iff))break;} else
{for(ii2=0;ii2<f[iii].size();ii2++)
if(n1!=((n1*b+w[f[iii][ii2]])%iff))break;
if(ii2!=f[iii].size())break; } } }
if(iii==ff.size()&&n1==0){ cycles[j1][j2][j3]=iff;break;}}
if(ii==nbsmall)break;}
if(j3!=2)break;} if(j2!=2)break;} if(j1==2)
{cout<<" ternary even/odd factors at positions (" <<i1<<","<<i2<<","<<i3<<") for : "<<stringf(f)<<"===>\n";
for(j1=0;j1<2;j1++) {cout<<"[";for(j2=0;j2<2;j2++){cout<<"(";for(j3=0;j3<2;j3++)cout<<cycles[j1][j2][j3]<<",";}
cout<<"),";}
cout<<"],";cout<<"\n"; return true;}
}
return false;
}
bool reccycle2(family f,sint j)
{family ff;sint n1,i1,i2,ii,iff;
if(j<f.size())
{ff=f;
if(!reccycle2(ff,j+2))return false;
ff[j-1].push_back(ff[j][0]);return reccycle2(ff,j+2);
}
for(ii=0;ii<nbsmall;ii++)
{iff=fact[ii];n1=0;
for(i1=0;i1<f.size()-2;i1=i1+2)
{for(i2=0;i2<f[i1].size();i2++)n1=(n1*b+w[f[i1][i2]])%iff;
if(n1!=((n1*b*b+w[f[i1+1][0]]*b+w[f[i1+1][0]])%iff))break;
}
if(i1<(f.size()-2))continue;
for(i2=0;i2<f[i1].size();i2++)n1=(n1*b+w[f[i1][i2]])%iff;
if(n1!=0)continue;
cout<<stringf(f)<<" has a divisor "<<iff<<"\n";return true;
}
cout<<stringf(f)<<" has no small common divisor\n";
return false;
}
bool manycycle2(family f) // check if many single choices have an even/odd cycle
{sint i;
for(i=1;i<f.size();i=i+2)if(f[i].size()>1)return false;
cout<<"?"<<stringf(f)<<"\n";
return reccycle2(f,1);
}
void handelf(family f)
{family ff,fff; string s,s1,s2,p,p1,p2; bool test;
sint i,j,l,ic,jc,ic1,ic2,lc1,lc2,jc1,jc2,i1,i2,j1,j2;
cout<<stringf(f)<<"\n";cout.flush();fff=f;nt++;
for(j=1;j<f.size();j=j+2) // clean choices
{p="";
for(l=0;l<f[j].size();l++)
{ff=f;ff[j-1]=f[j-1]+f[j][l];s=familystrip(ff);
if(!cover(s))p.push_back(f[j][l]);
}
f[j]=p;
}
for(i=1;i<f.size();i=i+2)if(f[i].size()==0) // simplify void choices
{f[i-1]=f[i-1]+f[i+1];f.erase(f.begin()+i,f.begin()+i+2);i=i-2;}
for(i=1;i<f.size();i=i+2)if(f[i].size()==1&&f[i][0]==f[i+1][0])
{f[i-1]=f[i-1]+f[i];f[i+1].erase(0,1);} // simplify {a}a into a{a}
if(f.size()!=1)
for(i=1;i<f.size()-2;i=i+2){
if(f[i].size()==1&&f[i+1].size()==0&&f[i]==f[i+2])
{f.erase(f.begin()+i+1,f.begin()+i+3);}} // simplify {a}""{a} into {a}
if(f!=fff){cout<<"=======> ";cout<<stringf(f)<<"\n";}
s=familystrip(f);
if(cover(s)){cout<<"covered by "<<covering<<"\n";return;}
if(newprime(s)){return;}
if(f.size()==1)return; // single string
if(smallfactor(f))return;
if(cyclicfactors(f))return;
if(dcyclfact(f))return;
if(par3(f))return;
if(manycycle2(f))return;
if(s.size()>lgmax){cout<<"still to analyze (too long)\n";nlong++;
reject<<stringf(f)<<"\n";reject.flush();return;}
// if a digit must be repeated, repeat it, if not already very long
if(s.size()<=lgmax/2)
for(i=1;i<f.size();i=i+2)for(j=0;j<f[i].size();j++)
{ff=f;ff[i]=ff[i].erase(j,1);p=f[i][j];
test=smallfactor(ff);
if(!test)test=cyclicfactors(ff);
if(!test)test=dcyclfact(ff);
if(!test)test=par3(ff);
if(test)
{
if(f[i].size()==1){f[i-1]=f[i-1]+f[i];cout<<"needed extension ";handelf(f);return;}
ff.insert(ff.begin()+i+1,p);ff.insert(ff.begin()+i+2,f[i]);
cout<<"needed extension ";handelf(ff);return;
}
}
s=""; // if no covering
for(i=1;i<f.size();i=i+2)
{s=s+f[i-1];for(j=0;j<lgmax;j++)s=s+f[i];}
s=s+f.back();if(!cover(s)){cout<<"too long:"<<stringf(f)<<"\n";
reject<<stringf(f)<<"\n";reject.flush();return;}
// search for a digit with minimal extension
lc1=1000;ic=1;jc=0;
for(i=1;i<f.size();i=i+2)
{s1="";s2="";
for(j=0;j<i;j=j+2)s1=s1+f[j];
for(j=i+1;j<f.size();j=j+2)s2=s2+f[j];
for(l=0;l<f[i].size();l++)
{p=f[i][l];for(j=0;j<10;j++)p=p+p;
if(cover(s1+p+s2)){if(covering.size()<lc1){lc1=covering.size();ic=i;jc=l;}}
}
}
// search for a pair of options with a minimal covering
lc2=lc1;
for(i1=1;i1<f.size();i1=i1+2) for(i2=i1+2;i2<f.size();i2=i2+2)
for(j1=0;j1<f[i1].size();j1++) for(j2=0;j2<f[i2].size();j2++)
{ff=f; p1=f[i1][j1];for(j=0;j<10;j++)p1=p1+p1;p2=f[i2][j2];for(j=0;j<10;j++)p2=p2+p2;
ff[i1-1]=ff[i1-1]+p1;ff[i2-1]=ff[i2-1]+p2;s=familystrip(ff);
if(cover(s)){if(covering.size()<lc2){ic1=i1;ic2=i2;jc1=j1;jc2=j2;lc2=covering.size();}}
}
for(i1=1;i1<f.size();i1=i1+2)
for(j1=0;j1<f[i1].size();j1++) for(j2=j1+1;j2<f[i2].size();j2++)
{ff=f;i2=i1;p1=f[i1][j1];for(j=0;j<10;j++)p1=p1+p1;p2=f[i2][j2];for(j=0;j<10;j++)p2=p2+p2;
ff[i1-1]=f[i1-1]+p1+p2;s=familystrip(ff);
if(!cover(s))continue;
l=covering.size();if(l>=lc2)continue;
ff[i1-1]=f[i1-1]+p2+p1;s=familystrip(ff);
if(!cover(s))continue;
if(l<covering.size())l=covering.size();
if(l>=lc2)continue;
ic1=i1;ic2=i2;jc1=j1;jc2=j2;lc2=l;
}
// handle selection
if(lc2>=lc1) // no pair is really interesting
{if(lc1==1000) // no digit is really selected: chose one randomly
{test=false;
if(f.size()>3){l=f.size()/2;uniform_int_distribution<> distri(0,l-1);test=true;
ic=1+2*(distri(gen));}
if(f[ic].length()>1){uniform_int_distribution<> distrj(0,f[ic].length()-1);jc=distrj(gen);test=true;}
if(test)cout<<"! "<<ic<<","<<jc<<"\n";
}
ff=f;p=f[ic][jc];ff[ic].erase(jc,1);
cout<<"split1 "<<stringf(f)<<" without ("<<ic<<","<<f[ic][jc]<<")-ext="<<lc1<<"\n";handelf(ff);
cout<<"extend1 "<<stringf(f)<<" with ("<<ic<<","<<f[ic][jc]<<")\n";
ff.insert(ff.begin()+ic+1,p);ff.insert(ff.begin()+ic+2,f[ic]); handelf(ff);
}
else // a pair should be handled
{p1=f[ic1][jc1];p2=f[ic2][jc2];
ff=f;ff[ic2].erase(jc2,1);ff[ic1].erase(jc1,1);
cout<<"split2 "<<stringf(f)<<" without ("<<ic1<<","<<f[ic1][jc1]<<") and ("<<ic2<<","<<f[ic2][jc2]<<")-ext="<<lc2<<"\n";
handelf(ff);fff=ff;
ff.insert(ff.begin()+ic1+1,p1);ff.insert(ff.begin()+ic1+2,ff[ic1]+f[ic1][jc1]);
cout<<"extend21 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc1]<<
") and without ("<<ic2<<","<<f[ic2][jc2]<<")\n";
handelf(ff);ff=fff;
ff.insert(ff.begin()+ic2+1,p2);ff.insert(ff.begin()+ic2+2,ff[ic2]+f[ic2][jc2]);
cout<<"extend22 "<<stringf(f)<<" with ("<<ic2<<","<<f[ic2][jc2]<<
") and without ("<<ic1<<","<<f[ic1][jc1]<<")\n";
handelf(ff);ff=fff;
if(ic1!=ic2)
{ff.insert(ff.begin()+ic1+1,p1);ff.insert(ff.begin()+ic1+2,f[ic1]);
ff.insert(ff.begin()+ic2+3,p2);ff.insert(ff.begin()+ic2+4,f[ic2]);
cout<<"extend212 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc1]<<
") and ("<<ic2<<","<<f[ic2][jc2]<<")\n";
handelf(ff);
} else
{ff[ic1]=ff[ic1]+p2;ff.insert(ff.begin()+ic1+1,p1);ff.insert(ff.begin()+ic1+2,fff[ic1]+p1);
ff.insert(ff.begin()+ic2+3,p2);ff.insert(ff.begin()+ic2+4,f[ic2]);
cout<<"extend212 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc1]<<
") and ("<<ic2<<","<<f[ic2][jc2]<<")\n";
handelf(ff);ff=fff;
ff[ic1]=ff[ic1]+p1;ff.insert(ff.begin()+ic1+1,p2);ff.insert(ff.begin()+ic1+2,fff[ic2]+p2);
ff.insert(ff.begin()+ic2+3,p1);ff.insert(ff.begin()+ic2+4,f[ic1]);
cout<<"extend221 "<<stringf(f)<<" with ("<<ic1<<","<<f[ic1][jc2]<<
") and ("<<ic2<<","<<f[ic2][jc1]<<")\n";
handelf(ff);
}
}
} // fin du traitement de la base
int main(int argc, char *argv[])
{string ms,pref,prefms,p,ss,chunkl,chunkr; family f;
sint i,ilb,ilh,irb,irh,il,ir,j;
mpz_init(rtt); mpz_init(rt1); mpz_init(rt2);
ms=argv[1];b=atoi(argv[1]); lgmax=atoi(argv[2]);
pref="kernel"; prefms=pref+ms;
kern.open(prefms.c_str());
if(argc>3){chunkl=argv[3];ms=ms+'-'+argv[3];}else{chunkl="";}
if(argc>4){chunkr=argv[4];ms=ms+'-'+argv[4];}else{chunkr="";}
pref="result"; prefms=pref+ms;
result.open(prefms.c_str(),ios::out);
pref="newprimes"; prefms=pref+ms;
court.open(prefms.c_str(),ios::out);
pref="left"; prefms=pref+ms;
reject.open(prefms.c_str(),ios::out);
for(char c='0';c<='9';c++){w[c]=0+c-'0';tr[0+c-'0']=c;}
for(char c='A';c<='Z';c++){w[c]=10+c-'A';tr[10+c-'A']=c;}
for(char c='a';c<='z';c++){w[c]=36+c-'a';tr[36+c-'a']=c;}
buildfact();
if(chunkl!="")cout<<" for initial digits "<<chunkl;
if(chunkr!="")cout<<" and for terminal digits "<<chunkr;
cout<<"\n";
cout<<"lgmax="<<lgmax<<"\n";cout.flush();
found.clear();nt=0;nprime=0;nlong=0;
while(kern>>p)
{found.push_back(p);
}
// initialisation
if(chunkl==""){ilb=1;ilh=b-1;}
else {for(i=0;i<chunkl.length();i++)if(chunkl[i]=='@')break;
if(i==chunkl.length()){ilb=stoi(chunkl);ilh=ilb;}
else {ilb=stoi(chunkl.substr(0,i));ilh=stoi(chunkl.substr(i+1));}
}
if(chunkr==""){irb=1;irh=b-1;}
else {for(i=0;i<chunkr.length();i++)if(chunkr[i]=='@')break;
if(i==chunkr.length()){irb=stoi(chunkr);irh=irb;}
else {irb=stoi(chunkr.substr(0,i));irh=stoi(chunkr.substr(i+1));}
}
cpustarttime=clock();
ss="";for(j=0;j<b;j++)ss=ss+tr[j];
f.clear();f.push_back("0");f.push_back(ss);f.push_back("0");
for(il=ilb;il<=ilh;il++)for(ir=irb;ir<=irh;ir++)if(gcd(b,ir)==1)
{f[0][0]=tr[il];f[2][0]=tr[ir];
//construction of the familles to be explored from the first and last digits
handelf(f);
}
cputime = clock()-cpustarttime; cputime=cputime/1000000;
result<<"\nFinally, in "<<cputime<<" time units, we explored "
<<nt<<" families of numbers for base "<<b;
if(chunkl!="")result<<" and the initial digit "<<chunkl;
if(chunkr!="")result<<" and the terminal digit "<<chunkr;
result<<"\nthere remains "<<nprime<<" primes to incorporate in the kernel, and "
<<nlong<<" families to analyse\n";
} // end main