I am attempting to write a modular inverse function based on this c++ version
#include <iostream>
using namespace std;
int mul_inv(int a, int b)
{
int b0 = b, t, q;
int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
int main(void) {
cout<<mul_inv(42, 2017)<<endl;
return 0;
}
in my TI-Nspire CX So far I have the following code but am getting variable not defined in if statement.
Here is the code I have in my Nspire CX so far.
Define LibPub modinv(a,b)=
Func
Local b0,t,q,x0,x1
0→x0
1→x1
If b=1 Then
Return 1
EndIf
While a>1
q:=((a)/(b))
t:=b
b:=mod(a,b)
a:=t
t:=x0
x0:=x1-q*x0
x1:=t
EndWhile
If x1<0 Then
x1:=x1+b0
EndIf
Return x1
EndFunc
the inspire is throwing an error on this line.
If x1<0 Then
x1:=x1+b0
EndIf
Not sure what I am doing wrong here! Any insight would be appreciated!