CunninghamLengths PrimeTester::ProbablePrimeChainTest(const Bn& origin, PrimeChainType chainType) { CunninghamLengths r; Bn t(origin); switch (chainType) { case PrimeChainType::Cunningham1: r.Cunningham1Length = ProbableCunninghamChainTest(t -= 1, 1); break; case PrimeChainType::Cunningham2: r.Cunningham2Length = ProbableCunninghamChainTest(t += 1, -1); break; case PrimeChainType::Twin: if ((r.Cunningham1Length = ProbableCunninghamChainTest(t -= 1, 1)) >= 1) { r.Cunningham2Length = ProbableCunninghamChainTest(t += 2, -1); r.TwinLength = floor(r.Cunningham1Length) > floor(r.Cunningham2Length) ? r.Cunningham2Length + 1 + floor(r.Cunningham2Length) : r.Cunningham1Length + floor(r.Cunningham1Length); } break; default: r.Cunningham1Length = ProbableCunninghamChainTest(t -= 1, 1); r.Cunningham2Length = ProbableCunninghamChainTest(t += 2, -1); r.TwinLength = floor(r.Cunningham1Length) > floor(r.Cunningham2Length) ? r.Cunningham2Length + 1 + floor(r.Cunningham2Length) : r.Cunningham1Length + floor(r.Cunningham1Length); } return r; }
int ChainTest(int ChainType, vec_uint4 n[3], int *chainLength) { if (ChainType == CHAINTYPE_CC1) { return ProbableCunninghamChainTest(n, 1, 0, chainLength); } else if (ChainType == CHAINTYPE_CC2) { return ProbableCunninghamChainTest(n, 0, 0, chainLength); } else if (ChainType == CHAINTYPE_TWN) { vec_uint4 n2[3]; n2[0] = n[0]; n2[1] = n[1]; n2[2] = n[2]; int c1=0; int c2=0; if (ProbableCunninghamChainTest(n, 1, 0, &c1)) { if (ProbableCunninghamChainTest(n2, 0, 0, &c2)) { if (c1 > c2) *chainLength = c2+c2+1; else *chainLength = c1+c1; // printf("TWN: %d + %d = %d\n", c1, c2, *chainLength); return 1; } } } return 0; }