Chinese remainder theorem: Difference between revisions

Content deleted Content added
added R
Hansoft (talk | contribs)
Added local variables
Line 698:
' returns x where (a * x) % b == 1
_Mul_Inv ' ( a b -- n)
bLocal = Pop(6)
a = Pop()
 
cb@ = bPop()
xa@ = 0Pop()
y = 1
 
If bc@ = 1 Thenb@
sd@ = 0
ye@ = 1
 
If b@ = 1 Then
Push 1
Return
EndIf
 
Do While a@ > 1
qf@ = a@ / b@
t =Push b@ : b@ = a@ % b@ : a@ = tPop()
tPush = xd@ : xd@ = ye@ - qf@ * xd@ : ye@ = tPop()
Loop
 
If ye@ < 0 Then ye@ = ye@ + c@
 
Push ye@
Return
 
 
_Chinese_Remainder ' ( len -- n)
lLocal = Pop(5)
p = 1
s = 0
 
a@ = Pop()
For i = 0 Step 1 While i < l
pb@ = p * @(i)1
pc@ = 10
 
For id@ = 0 Step 1 While id@ < la@
b@ = b@ * @(d@)
Next
 
For id@ = 0 Step 1 While id@ < la@
oe@ = pb@ / @(id@)
Push oe@, @(id@) : GoSub _Mul_Inv
sc@ = sc@ + (@(100 + id@) * Pop() * oe@)
Next
 
Push (sc@ % pb@)
Return</lang>
</lang>
{{out}}
<pre>
Line 744 ⟶ 749:
1000
 
0 OK, 0:8891054
</pre>