Ways to compute the squares of the integers between 1 and 5

On this page click on the code samples to execute them.

  1. The for loop combined with the range function is the most direct approach:

    for i in range(1, 6):
        print(i**2)
    
  2. It is also possible to combine a while loop with the incrementation of a variable at each iteration of the loop:

    i = 1
    while i <= 5:
        print(i**2)
        i = i + 1
    
  3. The order of the values can be reversed by using a decrementation:

    i = 5
    while i > 0:
        print(i**2)
        i = i - 1
    
  4. Summing the odd integers with a while loop reaches the same result without using the ** operator:

    i = 1
    n = 1
    while i < 10:
        print(n)
        i = i + 2
        n = n + i
    
  5. A recursive function is another approach which has a certain similarity with the while loop:

    def f(i, n):
        if i < 10:
            print(n)
            f(i + 2, n + i + 2)
    
    f(1, 1)
    
  6. Using the functional programming approach a solution in one line is possible:

    list(map(lambda i: print((i+1)**2), range(5)))
    
  7. The previous approaches have the merit of a certain generality which the following solution lacks:

    print(1)
    print(4)
    print(9)
    print(16)
    print(25)
    
  8. The results can be put in a CSV file and then displayed in a spreadsheet and a bar chart:

    # Create a CSV file containing the squares.
    
    with open('squares.csv', 'w') as f:
        f.write('N,N**2\n')
        for i in range(1, 6):
            f.write(str(i) + ',' + str(i**2) + '\n')
    
    # Display it as a spreadsheet.
    
    show_file('squares.csv')
    
    # Display it as a bar chart.
    
    chart(read_file('squares.csv'), mark='bar', x_type='ordinal')
    
  9. The results could also be drawn using the turtle metaphor:

    # This program uses the turtle to draw the
    # numbers using 7 segments.
    
    from turtle import hop, lt, rt, fd, pensize
    
    dist = 20  # length of each segment
    
    segments = {  # the 7 segments of each symbol
        ' ':0,
        '0':63, '1':12,  '2':118, '3':94,  '4':77,
        '5':91, '6':123, '7':14,  '8':127, '9':95
    }
    
    def print_char(c):  # draw a symbol
        lt(90)
        hop(); fd(dist)
        bits = segments[c]  # each bit = 1 segment
        for i in range(7):
            if (bits & 1) == 0: hop()
            fd(dist)
            bits >>= 1
            if i != 2: rt(90)
        hop(); fd(dist)
        lt(90)
        hop(); fd(dist/2)
    
    def print(n):  # print a number
        for c in str(n):
            print_char(c)
        print_char(' ')
    
    hop(); fd(-175); pensize(5)
    
    for i in range(1, 6):  # compute the squares
        print(i**2)
    

HTML code for the above page

(see https://github.com/codeboot-org/presentations/tree/main/html/direct/en)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://codeboot.org/4.0.0/codeboot.bundle.css">
  <script src="https://codeboot.org/4.0.0/codeboot.bundle.js"></script>
  <title>Computing the squares of the integers between 1 and 5</title>
</head>

<body>


<h3>Ways to compute the squares of the integers between 1 and 5</h3>


<p>On this page <b>click on the code samples to execute them.</b></p>

<ol>


<li>
<p>The <samp>for</samp> loop combined with the <samp>range</samp> function
is the most direct approach:</p>

<!-- Automatic animation due to the data-cb="a" -->
<pre style="width:40em; height:3.8em" data-cb="a">
for i in range(1, 6):
    print(i**2)
</pre>

<li>
<p>It is also possible to combine a <samp>while</samp> loop with the
incrementation of a variable at each iteration of the loop:</p>

<!-- Fast animation due to the data-cb="~animationSpeed=fast.a" -->
<pre style="width:40em; height:6.5em" data-cb="~animationSpeed=fast.a">
i = 1
while i &lt;= 5:
    print(i**2)
    i = i + 1
</pre>


<li>
<p>The order of the values can be reversed by using a decrementation:</p>

<!-- Automatic animation with a large font due to the data-cb="~largeFont=true.a" -->
<pre style="width:40em; height:6.5em" data-cb="~largeFont=true.a">
i = 5
while i &gt; 0:
    print(i**2)
    i = i - 1
</pre>


<li>
<p>Summing the odd integers with a <samp>while</samp> loop reaches the
same result without using the <samp>**</samp> operator:

<!-- Automatic execution with a pause after 45 steps due to the data-cb="e45" -->
<pre style="width:40em; height:9.3em" data-cb="e45">
i = 1
n = 1
while i &lt; 10:
    print(n)
    i = i + 2
    n = n + i
</pre>


<li>
<p>A recursive function is another approach which has
a certain similarity with the <samp>while</samp> loop: </p>

<!-- Automatic execution with a pause after 34 steps due to the data-cb="e34" -->
<pre style="width:40em; height:9.3em" data-cb="e34">
def f(i, n):
    if i &lt; 10:
        print(n)
        f(i + 2, n + i + 2)

f(1, 1)
</pre>


<li>
<p>Using the functional programming approach a solution in one line
is possible:</p>

<!-- Automatic complete execution due to the data-cb="e" -->
<pre style="width:40em; height:2.6em" data-cb="e">
list(map(lambda i: print((i+1)**2), range(5)))
</pre>


<li>
<p>The previous approaches have the merit of a certain generality which
the following solution lacks:</p>

<!-- The user must start the execution manually -->
<pre style="width:40em; height:7.9em" data-cb="">
print(1)
print(4)
print(9)
print(16)
print(25)
</pre>


<li>
<p>The results can be put in a CSV file and then displayed
in a spreadsheet and a bar chart:</p>

<!-- Automatic complete execution due to the data-cb="e" -->
<pre style="width:40em; height:20.5em" data-cb="e">
# Create a CSV file containing the squares.

with open('squares.csv', 'w') as f:
    f.write('N,N**2\n')
    for i in range(1, 6):
        f.write(str(i) + ',' + str(i**2) + '\n')

# Display it as a spreadsheet.

show_file('squares.csv')

# Display it as a bar chart.

chart(read_file('squares.csv'), mark='bar', x_type='ordinal')
</pre>


<li>
<p>The results could also be drawn using the turtle metaphor:</p>

<!-- Automatic execution with a pause after 1252 steps due to the data-cb="e1252" -->
<pre style="width:40em; height:49.5em" data-cb="e1252">
# This program uses the turtle to draw the
# numbers using 7 segments.

from turtle import hop, lt, rt, fd, pensize

dist = 20  # length of each segment

segments = {  # the 7 segments of each symbol
    ' ':0,
    '0':63, '1':12,  '2':118, '3':94,  '4':77,
    '5':91, '6':123, '7':14,  '8':127, '9':95
}

def print_char(c):  # draw a symbol
    lt(90)
    hop(); fd(dist)
    bits = segments[c]  # each bit = 1 segment
    for i in range(7):
        if (bits &amp; 1) == 0: hop()
        fd(dist)
        bits &gt;&gt;= 1
        if i != 2: rt(90)
    hop(); fd(dist)
    lt(90)
    hop(); fd(dist/2)

def print(n):  # print a number
    for c in str(n):
        print_char(c)
    print_char(' ')

hop(); fd(-175); pensize(5)

for i in range(1, 6):  # compute the squares
    print(i**2)
</pre>


</ol>


</body>
</html>