32 lines
819 B
Text
32 lines
819 B
Text
{def mulinv
|
|
{def mulinv.loop
|
|
{lambda {:t :nt :r :nr}
|
|
{if {not {= :nr 0}}
|
|
then {mulinv.loop :nt
|
|
{- :t {* {floor {/ :r :nr}} :nt}}
|
|
:nr
|
|
{- :r {* {floor {/ :r :nr}} :nr}} }
|
|
else {cons :t :r} }}}
|
|
{lambda {:a :n}
|
|
{let { {:a :a} {:n :n}
|
|
{:cons {mulinv.loop 0
|
|
1
|
|
{if {< :n 0} then {- :n} else :n}
|
|
{if {< :a 0} then {- :n {% {- :a} :n}} else :a}}}
|
|
} {if {> {cdr :cons} 1}
|
|
then not invertible
|
|
else {if {< {car :cons} 0}
|
|
then {+ {car :cons} :n}
|
|
else {car :cons} }}}}}
|
|
-> mulinv
|
|
|
|
{mulinv 42 2017}
|
|
-> 1969
|
|
{mulinv 40 1}
|
|
-> 0
|
|
{mulinv 52 -217}
|
|
-> 96
|
|
{mulinv -486 217}
|
|
-> 121
|
|
{mulinv 40 218}
|
|
-> not invertible
|