Shane Chism

Summation Example


Description


This script shows how a mathematical operation, in this case a summation, can be applied in terms of code. In this instance we've got sum{i=0}{n}{i^2}, where n is the input parameter.

You can see the mathematics at WolframAlpha and the code at CodePad. Moreover, you can view the C Version or C# Version of the script.

Usage


Modify Line 2 changing n to any value you wish to test.

With the default parameter of n = 5 we get the following output:
Summation of (i^2) from i = 0 to n = 5 is 55

Code

squareToN.py
View Plain   Download Code
# Change n to modify the number of iterations
n = 5;

x = 0;
for i in range( 1, n + 1 ):
	x = x + ( i * i );

print 'Summation of (i^2) from i = 0 to n = ' + str( n ) + ' is ' + str( x )


 
Python
NOVEMBER 22 2010
Download
56 DOWNLOADS