page52.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 84 行
HTML
84 行
<HTML><HEAD><TITLE>Example-Geometric Series Summation Again</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="tex2html1805" HREF="page53.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html1803" HREF="page49.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html1797" HREF="page51.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html1807" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION002230000000000000000">Example-Geometric Series Summation Again</A></H2><P><A NAME="secmodelgeometric2"> </A><P>In this example we revisit the problem of computing a<em>geometric series summation</em><A NAME=957> </A>.We have already seen an algorithm to compute this summationin Section <A HREF="page50.html#secmodelgeometric"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (Program <A HREF="page50.html#progexamplef"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).This algorithm was shown to take <IMG WIDTH=113 HEIGHT=29 ALIGN=MIDDLE ALT="tex2html_wrap_inline58105" SRC="img137.gif" > cycles.<P>The problem of computing the geometric series summationis identical to that of computing the value of a polynomialin which all of the coefficients are one.This suggests that we could make use of<em>Horner's rule</em><A NAME=965> </A>as discussed in Section <A HREF="page41.html#secmodelhorner"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.An algorithm to compute a geometric series summationusing Horner's rule is given in Program <A HREF="page52.html#progexampleg"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.<P><P><A NAME="1211"> </A><A NAME="progexampleg"> </A> <IMG WIDTH=575 HEIGHT=142 ALIGN=BOTTOM ALT="program968" SRC="img150.gif" ><BR><STRONG>Program:</STRONG> Program to compute <IMG WIDTH=53 HEIGHT=27 ALIGN=MIDDLE ALT="tex2html_wrap_inline58081" SRC="img133.gif" > using Horner's rule.<BR><P><P>The executable statements in Program <A HREF="page52.html#progexampleg"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>comprise lines 2-7.Table <A HREF="page52.html#tblgeometric2c"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the running times,as given by the simplified model,for each of these statements.<P><P><A NAME="1213"> </A><P> <A NAME="tblgeometric2c"> </A> <DIV ALIGN=CENTER><P ALIGN=CENTER><TABLE COLS=2 BORDER FRAME=HSIDES RULES=GROUPS><COL ALIGN=CENTER><COL ALIGN=CENTER><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> statement </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> time </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>2 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 2 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 3 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 2 </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 4 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 3(<I>n</I>+2) </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 5 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 6(<I>n</I>+1) </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 6 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 4(<I>n</I>+1) </TD></TR><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 7 </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 2 </TD></TR></TBODY><TBODY><TR><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP>TOTAL </TD><TD VALIGN=BASELINE ALIGN=CENTER NOWRAP> 13<I>n</I>+22 </TD></TR></TBODY><CAPTION ALIGN=BOTTOM><STRONG>Table:</STRONG> Computing the running time of Program <A HREF="page52.html#progexampleg"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>.</CAPTION></TABLE></P></DIV><P><P>In Programs <A HREF="page50.html#progexamplef"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> and <A HREF="page52.html#progexampleg"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>we have seen two different algorithms to compute the samegeometric series summation.We determined the running time of the former to be <IMG WIDTH=113 HEIGHT=29 ALIGN=MIDDLE ALT="tex2html_wrap_inline58105" SRC="img137.gif" > cyclesand of the latter to be 13<I>n</I>+22 cycles.In particular, note that for all non-negative values of <I>n</I>, <IMG WIDTH=207 HEIGHT=29 ALIGN=MIDDLE ALT="tex2html_wrap_inline58169" SRC="img151.gif" >.Hence, according to our simplified model of the computer,Program <A HREF="page52.html#progexampleg"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>, which uses Horner's rule,<em>always</em> runs faster than Program <A HREF="page50.html#progexamplef"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>!<P><HR><A NAME="tex2html1805" HREF="page53.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html1803" HREF="page49.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html1797" HREF="page51.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html1807" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?