# The Fibonacci Sequence

[XML] [Smart Pascal] [JavaScript] [Python] [output]

See Dr Ron Knott's Fibonacci page for the relevance of the sequence in nature. Fibonacci number 0 is 0 and Fibonacci number 1 is 1. Thereafter, each term of the sequence is equal to the sum of the preceding two terms.

We used the Blockly Playground to generate Python code from the same blocks.

The blocks

## XML Code of Blocks

```<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="variables_set" id="QN{xq__QQA[W?%aGW/KQ" x="29" y="67">
<field name="VAR">intLastButOne</field>
<value name="VALUE">
<block type="math_number" id="fFV2{V2jwcXqmLichv4U">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="variables_set" id="]9Bod/Rj7\$2rW]}a\$~l*">
<field name="VAR">intLast</field>
<value name="VALUE">
<block type="math_number" id="DTUY,~cRxpLk88J--@em">
<field name="NUM">1</field>
</block>
</value>
<next>
<block type="text_print" id="mC0G3r,O|[u7goZT}ho-">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="6U\$v:sam4dA3jpgA)cEa">
<mutation items="2"></mutation>
<block type="text" id="djF6H6;^=fz~8;ACbkH(">
<field name="TEXT">Fibonacci number 0: </field>
</block>
</value>
<block type="variables_get" id="czCZ6)1!8y!}^XK.R38!">
<field name="VAR">intLastButOne</field>
</block>
</value>
</block>
</value>
<next>
<block type="text_print" id="GYAnApz6pTTv*x?Pc{o8">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="YsS\$9K!~%yQDT2N_}tb,">
<mutation items="2"></mutation>
<block type="text" id="}ngQTWjVNyp#t5Z^%pp!">
<field name="TEXT">Fibonacci number 1: </field>
</block>
</value>
<block type="variables_get" id="IXKxZ4iw]/^y?NTjgctD">
<field name="VAR">intLast</field>
</block>
</value>
</block>
</value>
<next>
<block type="controls_for" id="~}n.\$JeC60O=K:!)7V]Z">
<field name="VAR">i</field>
<value name="FROM">
<field name="NUM">1</field>
</value>
<value name="TO">
<field name="NUM">19</field>
</value>
<value name="BY">
<field name="NUM">1</field>
</value>
<statement name="DO">
<block type="variables_set" id="FLsG0\$:l445CUaiwwN5=">
<field name="VAR">intCurrent</field>
<value name="VALUE">
<block type="math_arithmetic" id="sk5)}%ezXM_}8V9nGf@e">
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="LFM[2RrGBq:-CJR}~gBa">
<field name="VAR">intLastButOne</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block type="variables_get" id="VA:nitAL-d-[!#HXndRx">
<field name="VAR">intLast</field>
</block>
</value>
</block>
</value>
<next>
<block type="text_print" id="k(H?S7X50#UT\$PEzV;p9">
<value name="TEXT">
<field name="TEXT">abc</field>
<block type="text_join" id="JihEkAQK:btZGb5HG=#;">
<mutation items="4"></mutation>
<block type="text" id="Hf@Fc7akVLkX*\$;!FOxt">
<field name="TEXT">Fibonacci number </field>
</block>
</value>
<block type="math_arithmetic" id="P\$^1f:}3l|40EPXh0VZh">
<value name="A">
<field name="NUM">1</field>
<block type="variables_get" id="HKRF0471+@q2Wm/{Zd/{">
<field name="VAR">i</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
</value>
</block>
</value>
<block type="text" id="2,!9Hqv]bFW!n}H#np;C">
<field name="TEXT">: </field>
</block>
</value>
<block type="variables_get" id="uy]6GvNyRs2k?sRk12|Q">
<field name="VAR">intCurrent</field>
</block>
</value>
</block>
</value>
<next>
<block type="variables_set" id="=tq`h%WJ{FI,|X1P+f`Z">
<field name="VAR">intLastButOne</field>
<value name="VALUE">
<block type="variables_get" id="/k*^rRhKHoLFmPWx|6UU">
<field name="VAR">intLast</field>
</block>
</value>
<next>
<block type="variables_set" id="z|ZEV;T{;8nU0UUd[xSd">
<field name="VAR">intLast</field>
<value name="VALUE">
<block type="variables_get" id="w5(X~wYYW?vKmWtNA;d#">
<field name="VAR">intCurrent</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</xml>

```

## Generated Smart Pascal Code

```var intCurrent: Integer;
var intLast: Integer;
var intLastButOne: Integer;

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;

intLastButOne := 0;
intLast := 1;
Console.writeln(Str('Fibonacci number 0: ') + Str(intLastButOne));
Console.writeln(Str('Fibonacci number 1: ') + Str(intLast));
for var i := 1 to 19 do
begin
intCurrent := intLastButOne + intLast;
Console.writeln(StrJoin([Str('Fibonacci number '), Str(i + 1), Str(': '), Str(intCurrent)],''));
intLastButOne := intLast;
intLast := intCurrent;
end;

```

## Generated JavaScript

```var intCurrent, intLast, intLastButOne, i;

intLastButOne = 0;
intLast = 1;
window.alert(String('Fibonacci number 0: ') + String(intLastButOne));
window.alert(String('Fibonacci number 1: ') + String(intLast));
for (i = 1; i <= 19; i++) {
intCurrent = intLastButOne + intLast;
window.alert(['Fibonacci number ',i + 1,': ',intCurrent].join(''));
intLastButOne = intLast;
intLast = intCurrent;
}

```

## Generated Python Code

```intCurrent = None
intLast = None
intLastButOne = None
i = None

intLastButOne = 0
intLast = 1
print(str('Fibonacci number 0: ') + str(intLastButOne))
print(str('Fibonacci number 1: ') + str(intLast))
for i in range(1, 20):
intCurrent = intLastButOne + intLast
print(''.join([str(x) for x in ['Fibonacci number ', i + 1, ': ', intCurrent]]))
intLastButOne = intLast
intLast = intCurrent

```

## Copy of Output

```Fibonacci number 0: 0
Fibonacci number 1: 1
Fibonacci number 2: 1
Fibonacci number 3: 2
Fibonacci number 4: 3
Fibonacci number 5: 5
Fibonacci number 6: 8
Fibonacci number 7: 13
Fibonacci number 8: 21
Fibonacci number 9: 34
Fibonacci number 10: 55
Fibonacci number 11: 89
Fibonacci number 12: 144
Fibonacci number 13: 233
Fibonacci number 14: 377
Fibonacci number 15: 610
Fibonacci number 16: 987
Fibonacci number 17: 1597
Fibonacci number 18: 2584
Fibonacci number 19: 4181
Fibonacci number 20: 6765```
Programming - a skill for life!

Suggestions, with examples such as Caesar cipher and Fibonacci sequence, for Blockly programs that you could develop