1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<META NAME="GENERATOR" CONTENT="Adobe FrameMaker 5.5/HTML Export Filter">
<LINK REL="STYLESHEET" HREF="VisualRef.css" CHARSET="ISO-8859-1" TYPE="text/css">
<TITLE> Description of Objects in VPython</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="controls.html"><img src="icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="index.html"><img src="icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="index.html"><img src="icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Visual Reference</td>
<td><A HREF="index.html"><img src="icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="controls.html">Controls</a>
<b class="navlabel">Up:</b> <a class="sectref" href="index.html">Contents</a>
<b class="navlabel">Next:</b> <a class="sectref" href="index.html">Contents</a>
<br>
<hr>
</DIV>
<DIV>
<H1 CLASS="Heading-1"> <font color="#0000A0">The factorial and combin Functions</font></H1>
</DIV>
<DIV>
<DIV>
<H2 CLASS="program0"> from visual import *</H2>
<H2 CLASS="program0">from visual.factorial import *</H2>
<H2 CLASS="program0">factorial(N) = N! </H2>
<h2 class="program0">combin(a,b) = a!/(b!*(a-b)!)</h2>
<p class="Normal"> </p>
<P CLASS="Normal"><em>Note: To avoid confusion between the module named "factorial"
and the function named "factorial", import the factorial module
<strong>after</strong> importing the visual module itself.</em></P>
<P CLASS="Normal">A major use of these functions is in calculating the number
of ways of arranging a group of objects. For example, if there are 5 numbered
balls in a sack, there are <span class="attribute">factorial(5)</span> =
5! = 5*4*3*2*1 = 120 ways of taking them sequentially out of the sack (5
possibilities for the first ball, 4 for the next, and so on). </P>
<P CLASS="Normal">If on the other hand the 5 balls are not numbered, but 2
are green and 3 are red, of the 120 ways of picking the balls there are
2! indistinguishable ways of arranging the green balls and 3! ways of arranging
the red balls, so the number of different arrangements of the balls is <span class="attribute">combin(5,2)</span>
= 5!/(3!*2!) = 10.</P>
<P CLASS="Normal">Logically, the combin function is just a combination of
factorial functions. However, cancellations in the numerator and denominator
make it possible to evaluate the combin function for values of its arguments
that would overflow the factorial function, due to the limited size of floating-point
numbers. For example, <span class="attribute">combin(5,2)</span> = 5!/(3!*2!)
= (5*4)/2 = 10, and we didn't have to evaluate 5! fully.</P>
<P CLASS="Normal"> </P>
</DIV>
</DIV>
<DIV> </DIV>
<hr>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="controls.html"><img src="icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="index.html"><img src="icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="index.html"><img src="icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Visual Reference</td>
<td><A HREF="index.html"><img src="icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
<td><img src="icons/blank.gif" border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="controls.html">Controls</a>
<b class="navlabel">Up:</b> <a class="sectref" href="index.html">Contents</a>
<b class="navlabel">Next:</b> <a class="sectref" href="index.html">Contents</a>
<br>
<hr>
</DIV>
</BODY>
</HTML>
|