Objective-C中的BigInt ModInverse函数

时间:2013-12-09 07:02:00

标签: ios objective-c

现在我正在使用BigInt类。我想实现像(deno.modInverse(P);)这样的BigInt ModInverse函数。如果有任何人知道的话,Plz会帮助..

1 个答案:

答案 0 :(得分:0)

-(BigInt *)modInvers:(BigInt *)b
{


    BigInt *a = [BigInt createFromBigInt:self];
    BigInt *one = [[BigInt alloc]initWithInt:1];
    BigInt *zero = [[BigInt alloc]initWithInt:0];

    if ([b equals:zero]) {
        return one;}


    BigInt *x2 = one,*x1 = zero;
    BigInt *y2 = zero,*y1 = one;
    BigInt *x,*y,*q,*r;
    while ([b greaterThan:zero])
    {
        q = [a divide:b], r = [a subtract:[b multiply:q]];
        x = [x2 subtract:[x1 multiply:q]];
        y = [y2 subtract:[y1 multiply:q]];
        a = b; b = r; x2 = x1;  x1 = x; y2 = y1; y1 = y;
    }
    if (![a equals:one]) {

    }
    return x2; 
}