[Ç̲ÑÒ]              [Äàë³]                   [Íàçàä]

 

4. ÍÀÉÏÐÎÑҲز ÂËÀÑÒÈÂÎÑÒ² ÏÐÎÃÐÀÌ

 

          4.1. Äîñë³äèòè öèêë íà ñê³í÷åíí³ñòü

          ïîêè x>0 ïîâò x ¬ x-1 êö .

          Ðîçâ`ÿçîê. Íåîáõ³äíà óìîâà ñê³í÷åííîñò³ öèêëó î÷åâèäíî âèêîíóºòüñÿ. Ðîçãëÿíåìî ôóíêö³þ Â(õ)=[x]+1. Îñê³ëüêè [x]³0 ïðè x>0, òî x>0 òÿãíå B(x)>0. Îñê³ëüêè [x-1]=[x]-1, òî Â(õ)=[x]+1>[x]=[x-1]+1=B(x-1). Îòæå B(x) - îáìåæóâàëüíà ôóíêö³ÿ ³ öèêë ñê³í÷åííèé.

 

          4.2. Âñòàíîâèòè óìîâè ñê³í÷åííîñò³ öèêë³â:

à)       ïîêè  x<>0 ïîâò  x ¬ x-1 êö;

á)      ïîêè  x>0 ïîâò  x ¬ x+1 êö;

â)       ïîêè  x<=b ïîâò  x ¬ x+a êö;

ã)       ïîêè  x<>b ïîâò  x ¬ x+a êö;

ä)      ïîêè  x<y ïîâò  z ¬ x; x ¬ y; y ¬ z+2 êö.

 

          4.3. Äîâåñòè ñê³í÷åíí³ñòü öèêëó â àëãîðèòì³

 

          Àëã Circle_1 öå

                   çì³í n: ö³ë;

          ïî÷

                   âçÿòè(n);

                   ïîêè n>1 ïîâò

                             ÿêùî n mod 2 =0 òî n ¬ n div 2

                             ³íàêøå n ¬ n+1

                             êð

                   êö;

                   ïîêàçàòè(n)

          êà.

 

          4.4. Äîâåñòè íåñê³í÷åíí³ñòü öèêëó â àëãîðèòì³

 

          Àëã Circle_2 öå

                   çì³í n: ö³ë;

          ïî÷

                   âçÿòè(n);

                             ïîêè n>1 ïîâò

                                      ÿêùî n mod 2 =0 òî n ¬ n div 2+1

                                      ³íàêøå n ¬ n+1

                                      êð

                             êö;

                   ïîêàçàòè(n)

          êà.

 

          4.5. Âñòàíîâèòè óìîâè ñê³í÷åííîñò³ öèêëó ó íàñòóïíîìó ôðàãìåíò³ àëãîðèòìó

          s¬ 0;

          ïîêè n<>m ïîâò

                   n ¬ n+1; s ¬ s+1

          êö

 

          4.6. Ñêëàñòè àëãîðèòì  ä³ëåííÿ íàòóðàëüíèõ ÷èñåë n i m íàö³ëî ç îñòà÷åþ:

          n=q*m+r, 0<=r<m.

          Ðîçâ`ÿçîê. Âèáåðåìî ïî÷àòêîâ³ çíà÷åííÿ  q=0, r=n.  öüîìó âèïàäêó óìîâà

                             J=(n=m*q+r)&(r>=0)

î÷åâèäíî ³ñòèííà. Ïîáóäóºìî öèêë òàêèì ÷èíîì, ùîá óìîâà J áóëà éîãî ³íâàð³àíòîì, à óìîâó ïîâòîðåííÿ öèêëó âèáåðåìî òàê, ùîá éîãî çàïåðå÷åííÿ ðàçîì ç ³íâàð³àíòîì Øa & J äàâàëî á óìîâó çàäà÷³. Î÷å­âèäíî, a=Ø (r<m) = (r>=m).

          Áóäåìî øóêàòè 䳿, ùî çáåð³ãàþòü óìîâó J ïðè óìîâ³ a. Çíà÷åííÿ çì³ííî¿ q â ðåçóëüòàò³ âèêîíàííÿ ö³º¿ 䳿, î÷åâèäíî, ïîâèííå çá³ëüøóâàòèñÿ. Ðîçãëÿíåìî çá³ëüøåííÿ q íà 1. Òîä³ r ïîòð³áíî çìåíøèòè íà âåëè÷èíó (q+1)*m-q*m=m, ùîá çàáåçïå÷èòè çáåðåæåííÿ J .Òàêèì ÷èíîì, ïàðà ïðèñâîºíü q ¬  q+1; r ¬  r-m çáåð³ãຠóìîâó J ïðè r>=m. Îñê³ëüêè çàäà÷à ìຠçì³ñò ïðè íàòóðàëüíèõ m ³ íåâ³ä`ºìíèõ ö³ëèõ n, òî ïåðåäáà÷èìî â í³é çàõèùåíå ââåäåííÿ. Îòðèìàºìî àëãîðèòì:

 

          àëãîðèòì Div öå

                   çì³í m,n,q,r :ö³ë;

          ïî÷

                   ïîâò

                             âçÿòè (m,n)

                   äî (m>0)&(n>=0) ;

                   q¬ 0; r¬ n;

                   ïîêè r>=m ïîâò

                             q¬ q+1; r¬ r-m

                   êö;

                   ïîêàçàòè(q,r)

          êà.

 

          Ñïàäíîþ ö³ëî÷èñëîâîþ âåëè÷èíîþ, î÷åâèäíî, º çíà÷åííÿ çì³ííî¿ r, îñê³ëüêè m>0 , à îáìåæóâàëüíîþ ôóíêö³ºþ - ôóíêö³ÿ B(r,m)=r-m.

 

          4.7. Ñêëàñòè àëãîðèòì îá÷èñëåííÿ íàéá³ëüøîãî ñï³ëüíîãî ä³ëüíèêà äâîõ íàòóðàëüíèõ ÷èñåë m, n .

          Ðîçâ`ÿçîê 1. Çàäà÷à ïîëÿãຠâ îá÷èñëåíí³ ôóíêö³¿, ïîçíà÷èìî ¿¿ ÍÑÄ, äâîõ íàòóðàëüíèõ àðãóìåíò³â: ÍÑÄ(m,n). Ïðè m=n îá÷èñëåííÿ ÍÑÄ òðèâ³àëüíå, òàê ÿê ÍÑÄ(m,m)=m.

           ñèëó àäèòèâíîñò³ ôóíêö³¿ ÍÑÄ âîíà ³íâàð³àíòíà â³äíîñíî ïåðåòâîðåíü

          m ¬  m - n  ÿêùî  m>n

          n ¬  n - m  ÿêùî  n>m

à òàêîæ ä³é n ¬  n+m;  m ¬  m+n.

          Âèá³ð ïîòð³áíî¿ ä³¿ ñåðåä ïåðåë³÷åíèõ âèøå íåîáõ³äíî çä³éñíèòè îäíî÷àñíî ç ïîøóêîì îáìåæóâàëüíî¿ ôóíêö³¿. ßêùî çà òàêó ôóíêö³þ âçÿòè B(m,n)=m+n , à çà óìîâó ïîâòîðåííÿ öèêëó óìîâó m<>n , òî ôóíêö³ÿ B(m,n) áóäå ñïàäàòè ï³ä 䳺þ ïåðøî¿ ñóêóïíîñò³ ïåðåòâîðåíü. Ùîá ïðèâåñòè öþ ôóíêö³þ ó â³äïîâ³äí³ñòü ç óìîâîþ ñê³í÷åííîñò³, ïåðåéäåìî äî p³çíèö³ B1(m,n) = B(m,n) - 2ÍÑÄ(m,n). Îòðèìàºìî àëãîðèòì

 

          àëãîðèòì ÍÑÄ öå

                   çì³í m,n :íàò;

          ïî÷

                   ïîâò

                             âçÿòè (m,n)

                   äî m>0 & n>0;

                   ïîêè m<>n ïîâò

                             ÿêùî m>n òî m ¬  m-- n

                             ³íàêøå n ¬  n - m

                             êð

                   êö;

                   ïîêàçàòè (m)

          êà.

          Ïîêàçàòè, ùî ôóíêö³ÿ B1(m,n) ä³éñíî º îáìåæóâàëüíîþ ôóíêö³ºþ îòðèìàíîãî öèêëó.

          Ðîçâ`ÿçîê 2. Äðóãèé ñïîñ³á ïîëÿãຠó ðîçãëÿä³ ³íøî¿ óìîâè  òðèâ³àëüíîñò³ îá÷èñëåííÿ ÍÑÄ, íàïðèêëàä ÍÑÄ(m,0)=m. Ôóíêö³ÿ ÍÑÄ ³íâàð³àíòíà â³äíîñíî ïåðåòâîðåíü m ¬  n; n ¬  m mod n. ßêùî çà îáìåæóâàëüíó ôóíêö³þ âçÿòè B(n)=n, à çà óìîâó ïîâòîðåííÿ öèêëó óìîâó n<>0, òî ôóíêö³ÿ B(n) áóäå ñïàäàòè ï³ä 䳺þ âêàçàíèõ ïåðåòâîðåíü. Îòðèìàºìî àëãîðèòì

 

          àëãîðèòì ÍÑÄ öå

                   çì³í m,n :íàò:

          ïî÷

                   ïîâò

                             âçÿòè (m,n)

                   äî m>0 & n>0 ;

                   ïîêè n<>0 ïîâò

                             l¬ m; m¬ n; n¬ l mod n

                   êö;

                   ïîêàçàòè (m)

          êà.

 

          4.8. Ñêëàñòè àëãîðèòì îá÷èñëåííÿ äîáóòêó äâîõ íàòóðàëüíèõ ÷èñåë, êîðèñòóþ÷èñü îïåðàö³ÿìè äîäàâàííÿ òà ä³ëåííÿ íàâï³ë.

 

          4.9. Ñêëàñòè àëãîðèòì îá÷èñëåííÿ ñòåïåíÿ y=xn íàòóðàëüíîãî ïîêàçíèêà, âèêîðèñòîâóþ÷è ò³ëüêè îïåðàö³¿ ìíîæåííÿ òà ä³ëåííÿ íàâï³ë.

          Ðîçâ`ÿçîê. Ðîçãëÿíåìî ôóíêö³þ y=zxk. Ïðè z=1 ³ k=n âîíà äaº øóêàíó âåëè÷èíy y=xn. Ïðè k=0 ôóíêö³ÿ y=zxk îá÷èñëþºòüñÿ òðèâ³àëüíî : y=z. Öÿ ôóíêö³ÿ º ³íâàð³àíòíîþ â³äíîñíî ïåðåòâîðåíü

                   x¬ x¬ x, k¬ k/2;

                   z¬ z¬ x, k¬ k-1.

          Ïåðøó ïàðó ä³é ìîæíà çàñòîñîâóâàòè ïðè ïàðíîìó k, äðóãó äîðå÷íî çàñòîñîâóâàòè ïðè íåïàðíîìó k. Óìîâà çàê³í÷åííÿ k=0. Îòðèìàºìî àëãîðèòì

 

                   àëã  ñòåï³íü   öå

                             çì³í n,k: íàò; x,z : ä³éñí;

                   ïî÷

                             âçÿòè(n,x);

                             z¬ 1; k¬ n;

                             ïîêè k <> 0 ïîâò

                                      ÿêùî k mod 2=0 òî

                                                x¬ x*x; k¬ k div 2

                                      ³íàêøå

                                                z¬ z* k; k¬ k-1;

                                      êð

                             êö;

                             ïîêàçàòè(z)

                   êà.

          Îáìåæóâàëüíó ôóíêö³þ ïîáóäóâàòè ñàìîñò³éíî.

 

[Ç̲ÑÒ]              [Äàë³]                   [Íàçàä]