# 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">
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="93w;iJfG%X)ExL=xKZPY">
<field name="VAR">floatFirst</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="math_arithmetic" id="wH#_OBn7M~@@7yRco`i_">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="4Aqb(DR?:c6wYW:JMP_l">
<field name="VAR">floatDifference</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="math_arithmetic" id="\$y?/5UX7!_S-VpbdvL{X">
<field name="OP">MINUS</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="cAzuk~pW?aNAOKGIrr@]">
<field name="VAR">intTerms</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
</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">
<field name="NUM">1</field>
<block type="math_arithmetic" id="3bWz19at(bM*;\$D{,VM{">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="_f]w9h/7uB[DX/XpD/9W">
<field name="VAR">intTerms</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="math_arithmetic" id="B/lY%;iC]NB@*?|yf1oZ">
<value name="A">
<field name="NUM">1</field>
<block type="math_arithmetic" id="dZ2[N\$3:(l62#(]S;J.A">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">2</field>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="variables_get" id="MTuaB~:RPdKN1(=Y91fB">
<field name="VAR">floatFirst</field>
</block>
</value>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="math_arithmetic" id="MUqY5BPI;B,,uen`}lDK">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="-Hf8VbFj0b8peKGMV8wj">
<field name="VAR">floatDifference</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="math_arithmetic" id="zy@y#frN:V.zTS];ye[d">
<field name="OP">MINUS</field>
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="B2=CE.ms^?b3D8dG2H1{">
<field name="VAR">intTerms</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
<value name="B">
<field name="NUM">2</field>
</value>
</block>
</value>
<next>
<block type="text_print" id="76`i3m%!k\$/pUySvo*1#">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="6hqAv![\$6ILgF_j4U00j">
<mutation items="4"></mutation>
<block type="text" id="exn`,EvF*rERF_hQ,(Eh">
<field name="TEXT">Last term: </field>
</block>
</value>
<block type="variables_get" id="um\$BgIKhAj84;4gUQqIE">
<field name="VAR">floatLast</field>
</block>
</value>
<block type="text" id="Ih7k1DC{.(i9{*iPOy35">
<field name="TEXT"> Series (sum): </field>
</block>
</value>
<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">
</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">
</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">
<field name="NUM">3.1</field>
<block type="text_prompt_ext" id="nIRgX.M5\$D0,FK4YV)~2">
<mutation type="NUMBER"></mutation>
<field name="TYPE">NUMBER</field>
<value name="TEXT">
<field name="TEXT">Please enter number of terms</field>
</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'));
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'));
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'))
`Last term: 9.4 Series (sum): 22`