⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page54.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Example-Computing Powers</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html1827" HREF="page55.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html1825" HREF="page49.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html1819" HREF="page53.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html1829" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION002250000000000000000">Example-Computing Powers</A></H2><P>In this section we consider the running timeto raise a number to a given integer power.That is, given a value <I>x</I> and non-negative integer <I>n</I>,we wish to compute the  <IMG WIDTH=17 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline58189" SRC="img156.gif"  >.A na&#305;ve way to calculate  <IMG WIDTH=17 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline58189" SRC="img156.gif"  > would be to use a loop such as<PRE>result = 1i = 0while i &lt;= n:    result = result * x    i += 1</PRE>While this may be fine for small values of <I>n</I>,for large values of <I>n</I> the running time may become prohibitive.As an alternative, consider the following recursive definition<P><A NAME="eqnmodelpow">&#160;</A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation1035" SRC="img157.gif"  ><P><P>For example, using Equation&nbsp;<A HREF="page54.html#eqnmodelpow"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,we would determine  <IMG WIDTH=21 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline58201" SRC="img158.gif"  > as follows<P> <IMG WIDTH=339 HEIGHT=52 ALIGN=BOTTOM ALT="displaymath58177" SRC="img159.gif"  ><P>which requires a total of five multiplication operations.Similarly, we would compute  <IMG WIDTH=20 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline58203" SRC="img160.gif"  > as follows<P> <IMG WIDTH=352 HEIGHT=43 ALIGN=BOTTOM ALT="displaymath58178" SRC="img161.gif"  ><P>which requires a total of eight multiplication operations.<P>A recursive algorithm to compute  <IMG WIDTH=17 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline58189" SRC="img156.gif"  >based on the direct implementation of Equation&nbsp;<A HREF="page54.html#eqnmodelpow"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>is given in Program&nbsp;<A HREF="page54.html#progexampleh"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.Table&nbsp;<A HREF="page54.html#tblpowerc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the running time,as predicted by the simplified model,for each of the executable statements in Program&nbsp;<A HREF="page54.html#progexampleh"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="1056">&#160;</A><A NAME="progexampleh">&#160;</A> <IMG WIDTH=575 HEIGHT=145 ALIGN=BOTTOM ALT="program1053" SRC="img162.gif"  ><BR><STRONG>Program:</STRONG> Program to compute  <IMG WIDTH=17 HEIGHT=11 ALIGN=BOTTOM ALT="tex2html_wrap_inline58189" SRC="img156.gif"  >.<BR><P><P><P><A NAME="1216">&#160;</A><P>    <A NAME="tblpowerc">&#160;</A>    <DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=4 BORDER FRAME=HSIDES RULES=GROUPS><COL ALIGN=CENTER><COL ALIGN=CENTER><COL ALIGN=CENTER><COL ALIGN=CENTER><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>	    </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP COLSPAN=3> time</TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP><P>	    statement </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I>=0 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I><I>&gt;</I>0 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I><I>&gt;</I>0 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I> is even </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> <I>n</I> is odd </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>2 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 3 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 2 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    4 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 5 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=97 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58231" SRC="img163.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 	    7 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> -- </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=97 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58233" SRC="img164.gif"  > </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>TOTAL </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>  <IMG WIDTH=97 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58237" SRC="img165.gif"  > </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>		 <IMG WIDTH=97 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline58239" SRC="img166.gif"  > </TD></TR></TBODY><CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program&nbsp;<A HREF="page54.html#progexampleh"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.</CAPTION></TABLE></P></DIV><P><P>By summing the columns in Table&nbsp;<A HREF="page54.html#tblpowerc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> we getthe following recurrence for the running time of Program&nbsp;<A HREF="page54.html#progexampleh"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A><P><A NAME="eqnmodelpowtime">&#160;</A> <IMG WIDTH=500 HEIGHT=67 ALIGN=BOTTOM ALT="equation1075" SRC="img167.gif"  ><P>As the first attempt at solving this recurrence,let us suppose that  <IMG WIDTH=45 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline58245" SRC="img168.gif"  > for some <I>k</I><I>&gt;</I>0.Clearly, since <I>n</I> is a power of two, it is even.Therefore,  <IMG WIDTH=136 HEIGHT=28 ALIGN=MIDDLE ALT="tex2html_wrap_inline58251" SRC="img169.gif"  >.<P>For  <IMG WIDTH=45 HEIGHT=14 ALIGN=BOTTOM ALT="tex2html_wrap_inline58245" SRC="img168.gif"  >, Equation&nbsp;<A HREF="page54.html#eqnmodelpowtime"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives<P> <IMG WIDTH=358 HEIGHT=19 ALIGN=BOTTOM ALT="displaymath58179" SRC="img170.gif"  ><P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -