//testuj odwrotnosc modulo
void Tester::autotest_inverse(int len){
	Large one = Large::Set("1", base, in_base);
	Large zero = Large::Set("0", base, in_base);

	TesterRational tr = TesterRational();
	int i;
	Large a;
	Large mod;
	Large inv;
	srand( (unsigned)time(NULL) );

	for (i = 0; i < TIMES; i++) {
		a = Large::Set(tr.randomString(len), base, in_base);
		mod = Large::Set(tr.randomString(len), base, in_base);
		//jeœli a jest wiêksza ni¿ mod to nie ma sensu szukaæ
		if(a > mod){
			i--;
			continue;
		}
		inv = a.inverseMod(mod);

		Large result = Large(); 
		(a * inv).divide(mod, result);
		//wynik jest b³êdny gdy reszta nie jest równa jeden i inv nie jest zerem(znaczy to ze nie ma odwrotnosci)
		if (result.compareAbsolute(one)!=0 && inv.compareAbsolute(zero)!=0) {
			cout << " auto_Inv:Error " << endl;  
			cout << " a " << a.toString() << " inv " << inv.toString()  << " mod " << mod.toString() << endl;;
		}
		cout << "." ;
	}
	cout << "k" << endl;
}
Esempio n. 2
0
bool process_number(int * pan, Large a) {
    for (int i = 0; i < a.size(); i++) {
        if (pan[a.get(i)] ==  1) {
            return false;
        } else {
            pan[a.get(i)] = 1;
        }
    }
    return true;
}
Esempio n. 3
0
bool is_pandigital(Large a, Large b, Large c) {
    if ((a.toString()+b.toString()+c.toString()).length() != 9) {
        return false;
    }
    int pan [10];
    int i;
    for (i = 0; i < 10; i++) {
        pan[i] = 0;
    }
    return process_number(pan,a) && process_number(pan,b) && process_number(pan,c) && pan[0] == 0;
}
//testuj odwrotnosc modulo
void Tester::test_inverse(){
	//test1
	Large one = Large::Set("1", base, in_base);
	Large zero = Large::Set("0", base, in_base);

	Large test1 = Large::Set("12", base, in_base);
	Large mod = Large::Set("2f", base, in_base);
	Large inv = test1.inverseMod(mod);
	Large result;

	//jeœli istnieje liczba odwrotna(nie jest zerem) i odwrotnoœæ razy liczba mod to jeden,
	// to wynik jest poprawny
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse1: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test2
	test1 = Large::Set("3", base, in_base);
	mod = Large::Set("7", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse2: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test3
	test1 = Large::Set("c", base, in_base);
	mod = Large::Set("11", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse3: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test4
	test1 = Large::Set("21", base, in_base);
	mod = Large::Set("61", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse4: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test5
	test1 = Large::Set("b", base, in_base);
	mod = Large::Set("17", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse5: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test6
	test1 = Large::Set("5", base, in_base);
	mod = Large::Set("13", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse6: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test7
	test1 = Large::Set("80722", base, in_base);
	mod = Large::Set("41413", base, in_base);
	inv = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse7: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test8
	test1 = Large::Set("39782", base, in_base);
	mod = Large::Set("43d35", base, in_base);
	inv = test1.inverseMod(mod);
	Large test = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse8: " << test1.toString() << " " << mod.toString() << endl;
	}
	//test9
	test1 = Large::Set("45881", base, in_base);
	mod = Large::Set("152", base, in_base);
	inv = test1.inverseMod(mod);
	test = test1.inverseMod(mod);
	(test1 * inv).divide(mod, result);
	if (inv.compareAbsolute(zero)!=0 && result.compareAbsolute(one)!=0){
		cout << "Test_Inverse9: " << test1.toString() << " " << mod.toString() << endl;
	}
}
void Tester::autotest_crt(int len){
	TesterRational tr = TesterRational();
	Large a;
	Large a_m;
	Large b;
	Large b_m;
	Large c;
	Large c_m;

	vector<Large> vect = vector<Large>();
	srand( (unsigned)time(NULL) );
	Large result(base);

	for(int i=0; i<TIMES; i++){
		a = Large::Set(tr.randomString(len/2), base, in_base);
		a_m = Large::Set(tr.randomString(len), base, in_base);
		a.divide(a_m, a);

		b = Large::Set(tr.randomString(len/2), base, in_base);
		b_m = Large::Set(tr.randomString(len), base, in_base);
		while(!Large::coPrime(a_m, b_m)){
			b_m = Large::Set(tr.randomString(len), base, in_base);
		}
		b.divide(b_m, b);

		c = Large::Set(tr.randomString(len/2), base, in_base);
		c_m = Large::Set(tr.randomString(len), base, in_base);
		while(!Large::coPrime(b_m, c_m) || !Large::coPrime(a_m, c_m)){
			c_m = Large::Set(tr.randomString(len), base, in_base);
		}
		c.divide(c_m, c);
		
		vect.clear();
		vect.push_back(a);
		vect.push_back(a_m);
		vect.push_back(b);
		vect.push_back(b_m);
		vect.push_back(c);
		vect.push_back(c_m);

		result = Large::crt(vect);

		if(result.mod(a_m) != a){
			cout << "AutoTest CRT: Error przy a= " << a.toString() << " mod "
				<< a_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy b= " << b.toString() << " mod " 
				<< b_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy c= " << c.toString() << " mod " 
				<< c_m.toString() << " wynik " << result.toString() << endl;
			break;
		}

		if(result.mod(b_m) != b){
			cout << "AutoTest CRT: Error przy a= " << a.toString() << " mod "
				<< a_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy b= " << b.toString() << " mod " 
				<< b_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy c= " << c.toString() << " mod " 
				<< c_m.toString() << " wynik " << result.toString() << endl;
			break;
		}

		if(result.mod(c_m) != c){
			cout << "AutoTest CRT: Error przy a= " << a.toString() << " mod "
				<< a_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy b= " << b.toString() << " mod " 
				<< b_m.toString() << " wynik " << result.toString() << endl;
			cout << "AutoTest CRT: Error przy c= " << c.toString() << " mod " 
				<< c_m.toString() << " wynik " << result.toString() << endl;
			break;
		}

		cout << "." ;
	}
}