Arithmetic progression
[XML] [Smart Pascal] [JavaScript]
[Python] [output]
This demonstration outputs the last term and arithmetic series (sum) of an arithmetic progression given the first term, difference and number of terms. It shows how arithmetic can be built up from several blocks in a chain.
We used the Blockly Playground to generate the JavaScript and Python code from the same blocks.

The blocks
XML Code of Blocks
<xml xmlns="http://www.w3.org/1999/xhtml"> <block type="procedures_defnoreturn" id="f6Ob)+^iHN_}K;WTlfaH" x="13" y="113"> <mutation> <arg name="floatFirst"></arg> <arg name="floatDifference"></arg> <arg name="intTerms"></arg> </mutation> <field name="NAME">SeqA</field> <comment pinned="false" h="80" w="160">Outputs last term and series of arithmetic progression with first term floatFirst, difference floatDifference and number of terms intTerms </comment> <statement name="STACK"> <block type="variables_set" id=":wlgM#^Z}DR.rHBszgue"> <field name="VAR">floatLast</field> <value name="VALUE"> <block type="math_arithmetic" id="ZWP;Z:)=`BIi3Crt7DnQ"> <field name="OP">ADD</field> <value name="A"> <shadow id="(2c=X2`*Wy$}}Ai~8*,8" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="93w;iJfG%X)ExL=xKZPY"> <field name="VAR">floatFirst</field> </block> </value> <value name="B"> <shadow id="T2eL{c2O;i,FUa7|Kbxr" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="wH#_OBn7M~@@7yRco`i_"> <field name="OP">MULTIPLY</field> <value name="A"> <shadow id="I.?I[u*fSuwu8.-k70Xh" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="4Aqb(DR?:c6wYW:JMP_l"> <field name="VAR">floatDifference</field> </block> </value> <value name="B"> <shadow id="E%]$1$6tfK6)Hiw2_=E[" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="$y?/5UX7!_S-VpbdvL{X"> <field name="OP">MINUS</field> <value name="A"> <shadow id="=05q#J+aTs[T0b$_L4L+" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="cAzuk~pW?aNAOKGIrr@]"> <field name="VAR">intTerms</field> </block> </value> <value name="B"> <shadow type="math_number" id="qPZuJ9-U3)k!+00/i!wT"> <field name="NUM">1</field> </shadow> </value> </block> </value> </block> </value> </block> </value> <next> <block type="variables_set" id="}:#Xq_FxT)|6y*zz1kd{"> <field name="VAR">floatSeries</field> <value name="VALUE"> <block type="math_arithmetic" id="v1fNU$+jO[,(cYtC89a]"> <field name="OP">DIVIDE</field> <value name="A"> <shadow id="UjuUT~EF_p(36$W.]a8S" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="3bWz19at(bM*;$D{,VM{"> <field name="OP">MULTIPLY</field> <value name="A"> <shadow id="({kR#AHOV#[*WT8Gl$2#" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="_f]w9h/7uB[DX/XpD/9W"> <field name="VAR">intTerms</field> </block> </value> <value name="B"> <shadow id="{^VWVQtJJ,;DtAeF:ua5" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="B/lY%;iC]NB@*?|yf1oZ"> <field name="OP">ADD</field> <value name="A"> <shadow id="(2c=X2`*Wy$}}Ai~8*,8" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="dZ2[N$3:(l62#(]S;J.A"> <field name="OP">MULTIPLY</field> <value name="A"> <shadow type="math_number" id=":mHx$i/E6wL$Ve%tVVq^"> <field name="NUM">2</field> </shadow> </value> <value name="B"> <shadow id="gNz_rreMM,.qW[.!gBep" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="MTuaB~:RPdKN1(=Y91fB"> <field name="VAR">floatFirst</field> </block> </value> </block> </value> <value name="B"> <shadow id="T2eL{c2O;i,FUa7|Kbxr" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="MUqY5BPI;B,,uen`}lDK"> <field name="OP">MULTIPLY</field> <value name="A"> <shadow id="I.?I[u*fSuwu8.-k70Xh" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="-Hf8VbFj0b8peKGMV8wj"> <field name="VAR">floatDifference</field> </block> </value> <value name="B"> <shadow id="E%]$1$6tfK6)Hiw2_=E[" type="math_number"> <field name="NUM">1</field> </shadow> <block type="math_arithmetic" id="zy@y#frN:V.zTS];ye[d"> <field name="OP">MINUS</field> <value name="A"> <shadow id="=05q#J+aTs[T0b$_L4L+" type="math_number"> <field name="NUM">1</field> </shadow> <block type="variables_get" id="B2=CE.ms^?b3D8dG2H1{"> <field name="VAR">intTerms</field> </block> </value> <value name="B"> <shadow type="math_number" id="CGKi{D)7t;cAVp,vO9XY"> <field name="NUM">1</field> </shadow> </value> </block> </value> </block> </value> </block> </value> </block> </value> <value name="B"> <shadow type="math_number" id="2]tc4y6eAfSzd~}d090|"> <field name="NUM">2</field> </shadow> </value> </block> </value> <next> <block type="text_print" id="76`i3m%!k$/pUySvo*1#"> <value name="TEXT"> <shadow id="`m5bGJ@-w/+(?eaYWUC?" type="text"> <field name="TEXT">abc</field> </shadow> <block type="text_join" id="6hqAv![$6ILgF_j4U00j"> <mutation items="4"></mutation> <value name="ADD0"> <block type="text" id="exn`,EvF*rERF_hQ,(Eh"> <field name="TEXT">Last term: </field> </block> </value> <value name="ADD1"> <block type="variables_get" id="um$BgIKhAj84;4gUQqIE"> <field name="VAR">floatLast</field> </block> </value> <value name="ADD2"> <block type="text" id="Ih7k1DC{.(i9{*iPOy35"> <field name="TEXT"> Series (sum): </field> </block> </value> <value name="ADD3"> <block type="variables_get" id="Vn$cf[(wRZ;6bZ-L6[P2"> <field name="VAR">floatSeries</field> </block> </value> </block> </value> </block> </next> </block> </next> </block> </statement> </block> <block type="variables_set" id="#t#Fc,)qcSM4Av@go]TH" x="12" y="413"> <field name="VAR">floatFirst</field> <value name="VALUE"> <block type="text_prompt_ext" id="L_$41vMwhIrk=(Z_mimP"> <mutation type="NUMBER"></mutation> <field name="TYPE">NUMBER</field> <value name="TEXT"> <shadow type="text" id="Ni|`H*mNW-w{Ri]lOKvB"> <field name="TEXT">Please enter first term</field> </shadow> </value> </block> </value> <next> <block type="variables_set" id=".(!d#7ApmSl|!x$Gr_fp"> <field name="VAR">floatDifference</field> <value name="VALUE"> <block type="text_prompt_ext" id="?qX1ItfuI?tM#!s=PEEi"> <mutation type="NUMBER"></mutation> <field name="TYPE">NUMBER</field> <value name="TEXT"> <shadow type="text" id="Swd:FnirR-FgN;|Tk7S:"> <field name="TEXT">Please enter difference</field> </shadow> </value> </block> </value> <next> <block type="variables_set" id="sap8n;Xv($FGvjY^oIvc"> <field name="VAR">intTerms</field> <value name="VALUE"> <block type="math_round" id="X-2^]y[*vZw!R|bJ#t}]"> <field name="OP">ROUND</field> <value name="NUM"> <shadow id="or.LKCH_q@nQo_WjdrN}" type="math_number"> <field name="NUM">3.1</field> </shadow> <block type="text_prompt_ext" id="nIRgX.M5$D0,FK4YV)~2"> <mutation type="NUMBER"></mutation> <field name="TYPE">NUMBER</field> <value name="TEXT"> <shadow type="text" id="X(0^8kF[XUBd$c_Mnqd}"> <field name="TEXT">Please enter number of terms</field> </shadow> </value> </block> </value> </block> </value> <next> <block type="procedures_callnoreturn" id=":IR#oM|!]Uh@Ww+07?5/"> <mutation name="SeqA"> <arg name="floatFirst"></arg> <arg name="floatDifference"></arg> <arg name="intTerms"></arg> </mutation> <value name="ARG0"> <block type="variables_get" id=":sJVgYxx@F0ubbpOU2z$"> <field name="VAR">floatFirst</field> </block> </value> <value name="ARG1"> <block type="variables_get" id="dWkYH]Cc:Vy|M%~@6^Rf"> <field name="VAR">floatDifference</field> </block> </value> <value name="ARG2"> <block type="variables_get" id="1)c$.N?^4izrALE4[av/"> <field name="VAR">intTerms</field> </block> </value> </block> </next> </block> </next> </block> </next> </block> </xml>
Generated Smart Pascal Code
var floatSeries: Float; var floatLast: Float; var intTerms: Integer; var floatDifference: Float; var floatFirst: Float; function Str(v: Variant) : string; begin asm if (@v === 0) { @Result = '0'; } else if (@v === '') { @Result = ''; } else if (isNaN(@v)) { @Result = @v; } else { @Result = (@v).toString(); } end; end; { Outputs last term and series of arithmetic progression with first term floatFirst, difference floatDifference and number of terms intTerms } procedure SeqA(floatFirst: Float; floatDifference: Float; intTerms: Integer); begin floatLast := floatFirst + floatDifference * (intTerms - 1); floatSeries := (intTerms * (2 * floatFirst + floatDifference * (intTerms - 1))) / 2; Console.writeln(StrJoin([Str('Last term: '), Str(floatLast), Str(' Series (sum): '), Str(floatSeries)],'')); end; floatFirst := StrToFloat(prompt('Please enter first term')); floatDifference := StrToFloat(prompt('Please enter difference')); intTerms := Round(StrToFloat(prompt('Please enter number of terms'))); SeqA(floatFirst, floatDifference, intTerms);
Generated JavaScript
var floatSeries, floatLast, intTerms, floatDifference, floatFirst; /** * Outputs last term and series of arithmetic progression * with first term floatFirst, difference floatDifference * and number of terms intTerms */ function SeqA(floatFirst, floatDifference, intTerms) { floatLast = floatFirst + floatDifference * (intTerms - 1); floatSeries = (intTerms * (2 * floatFirst + floatDifference * (intTerms - 1))) / 2; window.alert(['Last term: ',floatLast,' Series (sum): ',floatSeries].join('')); } floatFirst = parseFloat(window.prompt('Please enter first term')); floatDifference = parseFloat(window.prompt('Please enter difference')); intTerms = Math.round(parseFloat(window.prompt('Please enter number of terms'))); SeqA(floatFirst, floatDifference, intTerms);
Generated Python Code
import math floatSeries = None floatLast = None intTerms = None floatDifference = None floatFirst = None """Outputs last term and series of arithmetic progression with first term floatFirst, difference floatDifference and number of terms intTerms """ def SeqA(floatFirst, floatDifference, intTerms): global floatSeries, floatLast floatLast = floatFirst + floatDifference * (intTerms - 1) floatSeries = (intTerms * (2 * floatFirst + floatDifference * (intTerms - 1))) / 2 print(''.join([str(x) for x in ['Last term: ', floatLast, ' Series (sum): ', floatSeries]])) def text_prompt(msg): try: return raw_input(msg) except NameError: return input(msg) floatFirst = float(text_prompt('Please enter first term')) floatDifference = float(text_prompt('Please enter difference')) intTerms = round(float(text_prompt('Please enter number of terms'))) SeqA(floatFirst, floatDifference, intTerms)
Copy of Output
The output on entering the values of 1.6 for the first term, 2.6 for the difference and 4 for the number of steps was as follows.
Last term: 9.4 Series (sum): 22