Kev tsim kho sai ntawm lub tshuab thev naus laus zis ua rau nws tuaj yeem daws qhov kev daws teeb meem ntawm ntau yam teeb meem. Yog tias dhau los ua kom sib txig sib luag yuav tsum raug daws rau ntawm daim ntawv, tam sim no koj tuaj yeem yooj yim sau qhov program thiab ua ob peb feeb. Cov lus tsis haum rau qhov no yog sej.
Npaj sau ib qho program
Kawm qhov theoretical lub hauv paus ntawm kev daws cov linear equations ua ntej tsim koj cov program sib tham sib. Qhov no yuav pab koj siv koj daim ntawv thov tom ntej code tau zoo dua.
Ua lub hauv paus rau qhov kev zov me nyuam. Thawj kauj ruam yog los txhais cov chav kawm. Ua haujlwm nrog cov pab pawg loj ntawm cov lej raws li chav kawm yog qhov yooj yim dua yog tias koj lub koos phis tawj siv tsawg. Qhov no yuav pab ua kom qhov kev siv tau ntawm koj qhov chaws zoo.
Tsim cov kev cai rau daim ntawv thov. Ib qho piv txwv ua piv txwv yog qhov muaj nqis ntawm cov ntaub ntawv sau. Qhov tsawg RAM dawb nyob hauv lub computer, tsawg yuav tsum tau sau tus lej.
Tsim kev cai code
Qhib cov davhlau ya nyob twg thiab kom tus neeg txhais lus Python nrog cov lus txib nram qab no:
My-iMac: ~ kuv $ sej –v
Qhov no yuav qhia ib daim ntawv uas ntev ntawm txhua Python modules muaj nyob rau hauv ib qho muab ntawm cov program. Ntawm qhov kawg, lub compiler yuav qhia koj tias hom twg ntawm Python tau siv rau hauv computer.
Tsim kom muaj nuj nqi tshiab cov lus txhais hauv sej los ntawm kev nkag mus hauv cov cai hauv qab no ntawm lub qhov rai compiler. Ntau qhov chaw hu ua txoj haujlwm no "cais tawm":
>> def cais (a, b, c):
Lub nyuv yuav tiv thaiv tus compiler los ntawm kev txhais tus lej tam sim ntawd thaum koj nias nkag, thiab yuav cia koj ua tiav txoj haujlwm.
Ua kom muaj ob qhov hloov pauv, q thiab r, uas coj tus quotient thiab qhov seem ntawm qhov sib npaug nrog cov hloov pauv a thiab b, thiab tom qab ntawd hu xov tooj div div muaj nuj nqi kom nrhiav thiab cais cov zauv ob. Tom qab ntawd, tus kws sib faib thiab cov kev ua haujlwm seem, yog tias muaj, yuav tshwm ntawm qhov screen. Txoj cai yuav tsum zoo li no:
… Q, r = divmod divmod (a, b)
Tsim qhov xwm txheej yog tias qhov yuav daws tau qhov teeb meem sai sai ntawm kab zauv thaum tsis muaj cov seem. Nkag mus rau hauv qab no:
… Yog r == 0:
… rov qab los ([0, c / b])
Tsim lwm yam mob rau rooj plaub thaum uas tseem tshuav:
… lwm yam:
… Sol = cais (b, r, c)
… U = sol [0]
… V = sol [1]
… rov qab ([v, u - q * v])
Qhov no yuav tso b thiab r hauv nqe lus divmod, rov qab rau lawv raws li koj thiab v, thiab tom qab ntawd rov qab lawv raws li cov kev daws teeb meem. Tus lej tag rau cov program no zoo li no:
>> def cais (a, b, c):
… Q, r = divmod (a, b)
… Yog r == 0:
… rov qab los ([0, c / b])
… lwm yam:
… Sol = cais (b, r, c)
… U = sol [0]
… V = sol [1]
… rov qab ([v, u - q * v])
Ua tib zoo saib xyuas kom paub meej tom qab lwm qhov thiab yog muaj hle. Nab hab sej yuav tsis tua tus lej no yam tsis muaj lub ntsiab txhais.
Nias lub khawm xa rov qab kom rov qab mus rau kab kab tom ntej. Nkag mus rau txoj haujlwm "cais tawm" thiab peb qhov tseem ceeb rau z, y thiab c thiab nias Rov qab. Koj yuav tsum pom cov hauv qab no:
>> cais (5, 17, 103)
[721, -206]
Qhov no txhais tau hais tias qhov kev zov me nyuam ua haujlwm tau raug thiab tsis muaj qhov yuam kev hauv qhov chaws. Sim nkag mus sib txawv qhov pib ntawm qhov tseem ceeb los saib seb cov kev suav puas yog.