---
title: "Repeating with for: Turtle Drawings"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 16
exercises: 12
source: https://one-course.com/books/computer-science/1/en/chapter/16-repeating-with-for-turtle-drawings
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 16 — Repeating with for: Turtle Drawings

Leo has written a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that draws a square: two lines, “go forward” and “turn left”, written four times. Now he wants a shape with 36 sides, almost a circle. That would be 72 lines, all the same, and one typing mistake would spoil the shape. There must be a better way: a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) is very good at doing the same thing again and again, if we tell it how many times.

**You already know.**

An [algorithm](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-algorithm) can say “repeat 4 times” instead of writing the same steps four times ([Chapter 10](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#ch-g4-algorithms-repeat-and-decide)). A [variable](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) is a name for a [value](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value), and an [assignment](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-assignment) such as `n = 6` gives it a [value](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) ([Chapter 15](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#ch-g6-variables-and-input)).

## 16.1 The turtle

**Definition 16.1 (Turtle graphics).**

*Turtle graphics* is a way of drawing with a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program): a pen, called the turtle, moves on the screen and leaves a line behind it. It has a position and a heading, the direction it faces, and it obeys `forward(d)` (move $d$ steps straight ahead), `left(a)` and `right(a)` (turn on the spot by $a$ degrees), `penup()` and `pendown()` (lift or lower the pen).

The turtle starts in the middle of the window, facing right. Python’s `turtle` module brings these [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction); a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) first takes the ones it needs with `from turtle import ...`. Its last line, `done()`, keeps the window open until it is closed.

![The turtle’s state is its position and its heading (the red arrow). forward changes the position and draws; left changes only the heading.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-70d1ed285ff3.svg)

*The turtle’s state is its position and its heading (the red arrow). `forward` changes the position and draws; `left` changes only the heading.*

```python
from turtle import forward, left, done

forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
done()
```

***Listing 16.1.** A square, the long way. code/grade-6/03-for-loops-turtle/listings/square_long.py*

When the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) runs, a window opens and the turtle draws a square of side 100. In this book, the turtle drawings are printed from the turtle’s recorded path rather than as a picture of the window.

**History — A turtle on the floor.**

The turtle comes from Logo, a [programming language](https://one-course.com/books/computer-science/1/en/chapter/14-a-first-python-program#def-g6-first-python-program-language) for children created in 1967 by a team led by Wallace Feurzeig, with Seymour Papert and Cynthia Solomon. The first turtle was a small robot on the floor, carrying a pen and drawing on large sheets of paper; it soon moved to the screen. Floor turtles were still sold to schools in the 1980s.

![A floor turtle robot of the 1980s, with a drawing it made.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/img-032ac251a166.jpg)

*A floor turtle robot of the 1980s, with a drawing it made.*

## 16.2 The `for` [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop)

**Definition 16.2 (For loop and block).**

A *for loop* repeats some lines a given number of times. `for i in range(4):` repeats four times the lines written under it, moved four spaces to the right. Those lines form the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop)’s *block*: the lines that are indented the same way under a line that ends with a colon.

**Definition 16.3 (Loop variable).**

In `for i in range(4):`, `i` is the *loop variable*: a [variable](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) that the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) sets to 0 the first time, then 1, 2 and 3. It can be used inside the [block](#def-g6-for-loops-turtle-for-loop).

```python
for i in range(5):
    print("i is", i)
print("done")
```

***Listing 16.2.** A [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) that shows its [loop variable](#def-g6-for-loops-turtle-loop-variable). code/grade-6/03-for-loops-turtle/listings/count.py*

```
$ python count.py
i is 0
i is 1
i is 2
i is 3
i is 4
done
```

***Output 16.1.** Five rounds, with `i` from 0 to 4; line 3 is after the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop).*

`range(5)` counts five numbers, starting from 0: 0, 1, 2, 3, 4. The last line is not indented, so it is not part of the [block](#def-g6-for-loops-turtle-for-loop): it runs once, after the [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) has finished. When the [block](#def-g6-for-loops-turtle-for-loop) does not need the [loop variable](#def-g6-for-loops-turtle-loop-variable), Python programmers name it `_`, a name that says “not used”. The square is now four lines shorter, and a 36-sided shape would take no more lines:

```python
from turtle import forward, left, done

for _ in range(4):
    forward(100)
    left(90)
done()
```

***Listing 16.3.** The same square, with a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop). code/grade-6/03-for-loops-turtle/listings/square.py*

Indentation matters. Here the second `print` is not indented, so it is not in the [block](#def-g6-for-loops-turtle-for-loop):

```python
for _ in range(3):
    print("Hip,")
print("hooray!")
```

***Listing 16.4.** Which lines repeat? code/grade-6/03-for-loops-turtle/listings/not_indented.py*

```
$ python not_indented.py
Hip,
Hip,
Hip,
hooray!
```

***Output 16.2.** Only the indented line repeats.*

## 16.3 Regular polygons and their angles

A regular polygon has equal sides and equal angles. To draw one, the turtle goes forward and turns by the same angle at each corner. Which angle? Walk once around any closed shape that does not cross itself and come back to the start, facing the way you started: you have turned exactly one full turn, $360^\circ$. In a regular polygon with $n$ sides, the $n$ turns are equal, so each is $360 \div n$ degrees.

![A regular hexagon. At each corner the turtle turns by 60 (red), from the dashed line it was following to the next side. The six turns put end to end make one full turn: 6 × 60 = 360.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-a4e6a76b5624.svg)

*A regular hexagon. At each corner the turtle turns by $60^\circ$ (red), from the dashed line it was following to the next side. The six turns put end to end make one full turn: $6 \times 60 = 360$.*

**Proposition 16.4 (The turning angle of a regular polygon).**

To draw a regular polygon with $n$ sides, the turtle turns by $360 \div n$ degrees at each corner: 120 for a triangle, 90 for a square, 60 for a hexagon.

**Proof.** A regular polygon does not cross itself, so going once around it the turtle turns one full turn, $360^\circ$, in $n$ equal turns. Each turn is $360 \div n$ degrees. ∎

The [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) below writes $n$ once, at the top, so changing one number changes the polygon.

```python
from turtle import forward, left, done

n = 6               # the number of sides: try 3, 5, 8, 36
for _ in range(n):
    forward(60)
    left(360 / n)
done()
```

***Listing 16.5.** Any regular polygon. code/grade-6/03-for-loops-turtle/listings/polygon.py*

A star is drawn the same way, but the turtle turns much more at each point: $144^\circ$, five times. In all, it turns $5 \times 144 = 720$ degrees, two full turns, and comes back to its start. A shape that crosses itself, like the star, can turn more than once.

```python
from turtle import forward, right, done

for _ in range(5):
    forward(150)
    right(144)
done()
```

***Listing 16.6.** A five-pointed star. code/grade-6/03-for-loops-turtle/listings/star.py*

## 16.4 The [loop variable](#def-g6-for-loops-turtle-loop-variable) and `range`

`range` can start somewhere else than 0, and can count by steps other than 1. `range(a, b)` counts from $a$ up to $b$, but without $b$; `range(a, b, s)` counts from $a$ in steps of $s$, also stopping before $b$. A negative step counts down.

```python
for i in range(3, 7):
    print(i)
print("---")
for i in range(0, 20, 5):
    print(i)
print("---")
for i in range(10, 0, -3):
    print(i)
```

***Listing 16.7.** Three ways to use `range`. code/grade-6/03-for-loops-turtle/listings/ranges.py*

```
$ python ranges.py
3
4
5
6
---
0
5
10
15
---
10
7
4
1
```

***Output 16.3.** The last number is never reached: 7, 20 and 0 are not printed.*

The [loop variable](#def-g6-for-loops-turtle-loop-variable) can also change a drawing at each round. In this spiral, the side grows with `i`: 0, then 5, 10, 15, and so on.

```python
from turtle import forward, left, done

for i in range(40):
    forward(5 * i)   # each side is longer than the one before
    left(90)
done()
```

***Listing 16.8.** A square spiral. code/grade-6/03-for-loops-turtle/listings/spiral.py*

![Four drawings, from the turtle’s recorded path. The red dot is the start. The star’s five turns of 144 make two full turns; the spiral’s sides grow by 5 at each turn.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-49a898a55d68.svg)

*Four drawings, from the turtle’s recorded path. The red dot is the start. The star’s five turns of $144^\circ$ make two full turns; the spiral’s sides grow by 5 at each turn.*

## 16.5 A loop inside a loop

The [block](#def-g6-for-loops-turtle-for-loop) of a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) can hold another [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop). Each time the outer [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) runs once, the inner [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) runs all its rounds.

**Definition 16.5 (Nested loop).**

A *nested loop* is a [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) written inside the [block](#def-g6-for-loops-turtle-for-loop) of another [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop). If the outer [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) runs $a$ times and the inner [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) $b$ times, the inner [block](#def-g6-for-loops-turtle-for-loop) runs $a \times b$ times.

```python
from turtle import forward, left, penup, pendown, done

for square in range(5):
    size = 20 + 10 * square      # 20, 30, 40, 50, 60
    for _ in range(4):           # one square
        forward(size)
        left(90)
    penup()
    forward(size + 15)           # move to the next square
    pendown()
done()
```

***Listing 16.9.** A row of five growing squares. code/grade-6/03-for-loops-turtle/listings/row_of_squares.py*

![The outer loop runs 5 times; each time, the inner loop draws the 4 sides of one square, then the turtle moves on with its pen up.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-dbaf75730ffd.svg)

*The outer [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) runs 5 times; each time, the inner [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) draws the 4 sides of one square, then the turtle moves on with its pen up.*

[Nested loops](#def-g6-for-loops-turtle-nested) print tables too. The multiplication table below has 5 rows, and each row has 5 numbers: the inner [block](#def-g6-for-loops-turtle-for-loop) runs $5 \times 5 = 25$ times. Inside the curly brackets, `:3` gives each number three places, so that the columns line up; `end=""` keeps the numbers of one row on the same line, and the `print()` of line 4 then ends the row.

```python
for row in range(1, 6):
    for column in range(1, 6):
        print(f"{row * column:3}", end="")
    print()
```

***Listing 16.10.** A multiplication table. code/grade-6/03-for-loops-turtle/listings/times_table.py*

```
$ python times_table.py
  1  2  3  4  5
  2  4  6  8 10
  3  6  9 12 15
  4  8 12 16 20
  5 10 15 20 25
```

***Output 16.4.** Row $r$, column $c$ holds $r \times c$.*

A text can be repeated with `*`, as in [Chapter 15](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#ch-g6-variables-and-input): with the [loop variable](#def-g6-for-loops-turtle-loop-variable), this draws stairs.

```python
for step in range(1, 6):
    print("#" * step)
```

***Listing 16.11.** Stairs of `#` signs. code/grade-6/03-for-loops-turtle/listings/stairs.py*

```
$ python stairs.py
#
##
###
####
#####
```

***Output 16.5.** Row $n$ has $n$ signs.*

## 16.6 Exercises

**Exercise 16.1 ★.**

What does `for i in range(3): print(i)` print?

**Solution of Exercise 16.1.**

Three lines: `0`, `1` and `2`.

**Exercise 16.2 ★.**

Which numbers does `range(2, 10, 3)` give?

**Solution of Exercise 16.2.**

2, 5 and 8: from 2 in steps of 3, stopping before 10.

**Exercise 16.3 ★.**

How many lines does this [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) print?

```python
for a in range(3):
    for b in range(4):
        print(a, b)
```

***Listing 16.12.** Count before running. code/grade-6/03-for-loops-turtle/exercises/nested_count.py*

**Solution of Exercise 16.3.**

12 lines: the outer [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) runs 3 times, and each time the inner [loop](https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide#def-g4-algorithms-repeat-and-decide-loop) prints 4 lines, from `0 0` to `2 3`.

**Exercise 16.4 ★.**

Write a turtle [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that draws an equilateral triangle of side 120.

**Solution of Exercise 16.4.**

Each turn is $360 \div 3 = 120$ degrees.

```python
from turtle import forward, left, done

for _ in range(3):
    forward(120)
    left(120)
done()
```

*An equilateral triangle. code/grade-6/03-for-loops-turtle/exercises/triangle.py*

**Exercise 16.5 ★★.**

Noor wants a regular pentagon, but her shape does not close. Find the mistake.

```python
from turtle import forward, left, done

for _ in range(5):
    forward(100)
    left(60)
done()
```

***Listing 16.13.** A pentagon that does not close. code/grade-6/03-for-loops-turtle/exercises/open_polygon.py*

**Solution of Exercise 16.5.**

A pentagon needs turns of $360 \div 5 = 72$ degrees; with 60 the five turns make only 300 degrees, less than a full turn, so the turtle does not come back to its start (the drawings below). The fix is `left(72)`.

**Exercise 16.6 ★★.**

Tom wants to print `Hip,` and `hooray!` three times each, in turn, but [Listing 16.4](#lst-g6-for-loops-turtle-not-indented) prints `hooray!` only once. Fix it.

**Solution of Exercise 16.6.**

Indent the second `print`, so that it is in the [block](#def-g6-for-loops-turtle-for-loop):

```python
for _ in range(3):
    print("Hip,")
    print("hooray!")
```

*Both lines in the [block](#def-g6-for-loops-turtle-for-loop). code/grade-6/03-for-loops-turtle/exercises/hip.py*

**Exercise 16.7 ★★.**

Write a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that prints the 7 times table, from `7 x 1 = 7` to `7 x 10 = 70`.

**Solution of Exercise 16.7.**

```python
for i in range(1, 11):
    print(f"7 x {i} = {7 * i}")
```

*The 7 times table. code/grade-6/03-for-loops-turtle/exercises/seven.py*

**Exercise 16.8 ★★.**

Write a turtle [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that draws a regular octagon of side 50.

**Solution of Exercise 16.8.**

Each turn is $360 \div 8 = 45$ degrees.

```python
from turtle import forward, left, done

for _ in range(8):
    forward(50)
    left(45)
done()
```

*A regular octagon. code/grade-6/03-for-loops-turtle/exercises/octagon.py*

**Exercise 16.9 ★★.**

By how many degrees must the turtle turn at each corner of a regular polygon with 10 sides? Why?

**Solution of Exercise 16.9.**

$360 \div 10 = 36$ degrees: going once around the polygon, the turtle makes one full turn, $360^\circ$, shared equally between the 10 corners.

**Exercise 16.10 ★★★.**

Write a turtle [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that draws a row of 5 triangles of side 50, side by side, each one starting where the last one’s base ends.

**Solution of Exercise 16.10.**

After each triangle, the turtle is back at the triangle’s first corner, facing right; one more `forward(50)` takes it to the end of the base, where the next triangle starts.

```python
from turtle import forward, left, done

for _ in range(5):          # five triangles
    for _ in range(3):      # the three sides of one triangle
        forward(50)
        left(120)
    forward(50)             # move along to the next one
done()
```

*A row of triangles. code/grade-6/03-for-loops-turtle/exercises/row_of_triangles.py*

![The drawings of exercises 4, 5, 8 and 10. With turns of 60 degrees, the “pentagon” stops short of its start.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-68ee8141398e.svg)

*The drawings of exercises 4, 5, 8 and 10. With turns of 60 degrees, the “pentagon” stops short of its start.*

**Exercise 16.11 ★★★.**

In [Listing 16.6](#lst-g6-for-loops-turtle-star), the turtle turns by $144^\circ$ five times. How many full turns is that? Why does the star close?

**Solution of Exercise 16.11.**

$5 \times 144 = 720 = 2 \times 360$: two full turns. After the five sides the turtle faces right again, as at the start. It is also back at its start: the five sides are the same length and the five turns the same, so the five points lie evenly on a circle, and the turtle jumps two points at a time until it has gone twice around the circle and reached the first point again.

**Exercise 16.12 ★★★.**

The numbers 1, 4, 7, 10, … go up by 3 each time. Write a [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) that prints the first 20 of them on one line. Which is the twentieth?

**Solution of Exercise 16.12.**

The number of rank $i$, counted from 0, is $1 + 3 \times i$:

```python
for i in range(20):
    print(1 + 3 * i, end=" ")
print()
```

*The first 20 numbers. code/grade-6/03-for-loops-turtle/exercises/sequence.py*

The twentieth is $1 + 3 \times 19 = 58$.

## 16.7 Problem: Stars of Every Size

**Problem 16.1.**

Weekend problem — stars with $n$ points

The [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) below draws $n$ equal sides and turns by $360 \times k \div n$ degrees after each one: in all, the turtle turns $k$ full turns. The two numbers $n$ and $k$ are written at the top.

```python
from turtle import forward, left, done

n = 8               # the number of points
k = 3               # the number of full turns in all
for _ in range(n):
    forward(150)
    left(360 * k / n)
done()
```

***Listing 16.14.** A star with $n$ points and $k$ full turns. code/grade-6/03-for-loops-turtle/listings/stars.py*

**Part I — By hand.**

1. For $n = 5$ and $k = 1$ , by how many degrees does the turtle turn each time? Which shape does it draw?
2. For $n = 5$ and $k = 2$ , compute the angle. Which listing of this chapter draws the same shape (it turns right instead of left)?
3. Draw five points on a circle, numbered 0 to 4. Join each point to the point two places further on: 0 to 2, 2 to 4, 4 to 1, and so on. Which shape do you get? Do the same jumping 3 places at a time, then compare with $k = 3$ .

**Part II — Eight points.** The drawings for $n = 8$ and $k$ from 1 to 7:

![Eight sides of length 150, turning 360 × k 8 degrees each time. The red dot is the start.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-b3ed14181dff.svg)

*Eight sides of length 150, turning $360 \times k \div 8$ degrees each time. The red dot is the start.*

4. What is the turning angle for $k = 2$ ? Why does the turtle draw a square, and how many times does it go around it?
5. What happens for $k = 4$ ? Explain with the angle.
6. Which [values](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) of $k$ draw a single line through all 8 points?
7. Compare the drawings for $k = 3$ and $k = 5$ , then for $k = 1$ and $k = 7$ . What do you notice? Explain with the angles.

**Part III — Counting the good stars.** The drawings for $n = 12$:

![Twelve sides, turning 360 × k 12 = 30 × k degrees.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-d379ecd86a07.svg)

*Twelve sides, turning $360 \times k \div 12 = 30 \times k$ degrees.*

8. For $n = 12$ , which $k$ draw all 12 points in one line? What does $k = 3$ draw, and $k = 4$ ?
9. Run the [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) for $n = 6$ , $7$ , $9$ , $10$ and $11$ and every $k$ from 1 to $n - 1$ . Make a table: for each $n$ , how many [values](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) of $k$ give a single line through all $n$ points?
10. The mathematics books prove the rule behind your table: the turtle goes through all $n$ points exactly when $n$ and $k$ have no common divisor other than 1. Check the rule on $n = 8$ and $n = 12$ .
11. Why does a prime number of points, such as 7 or 11, give a single line for every $k$ from 1 to $n - 1$ ?
12. State the result for $n = 12$ .

**Solution of Problem 16.1.**

**1.** $360 \times 1 \div 5 = 72$ degrees: a regular pentagon.

**2.** $360 \times 2 \div 5 = 144$ degrees: the five-pointed star of [Listing 16.6](#lst-g6-for-loops-turtle-star), drawn turning left, so it is its mirror image.

**3.** Jumping two places gives the five-pointed star. Jumping three places gives the same star, drawn the other way round: a jump of 3 forwards is a jump of 2 backwards. In the same way, $k = 3$ turns by $216^\circ$ to the left, which is $360 - 216 = 144^\circ$ to the right: the star again.

![Five points: the pentagon, and the star drawn in both directions.](https://one-course.com/images/onecourse/chapters/computer-science-1/g6-for-loops-turtle/fig-96027cdd4c09.svg)

*Five points: the pentagon, and the star drawn in both directions.*

**4.** $360 \times 2 \div 8 = 90$ degrees: the turtle turns as for a square, but draws 8 sides, so it goes around the square twice.

**5.** $360 \times 4 \div 8 = 180$ degrees: the turtle turns right round after each side, so it walks back and forth on one segment, 4 times each way.

**6.** $k = 1$, 3, 5 and 7.

**7.** $k = 5$ turns $225^\circ$ to the left, which is $135^\circ$ to the right, while $k = 3$ turns $135^\circ$ to the left: the same star, drawn in mirror image. $k = 7$ turns $315^\circ$ left, that is $45^\circ$ right: the octagon of $k = 1$, drawn the other way. In general, $k$ and $n - k$ give mirror drawings.

**8.** $k = 1$, 5, 7 and 11. For $k = 3$, the turn is $90^\circ$: a square, gone around three times. For $k = 4$, it is $120^\circ$: a triangle, gone around four times.

**9.** Counting the drawings:

| $n$ | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| [values](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) of $k$ | 4 | 2 | 6 | 4 | 6 | 4 | 10 | 4 |

For example, for $n = 6$ only $k = 1$ and $k = 5$ work (the hexagon); for $n = 9$, every $k$ except 3 and 6.

**10.** The divisors of 8 other than 1 are 2, 4 and 8: the $k$ sharing none of them are the odd ones, 1, 3, 5, 7, as in question 6. The divisors of 12 other than 1 are 2, 3, 4, 6 and 12: the $k$ from 1 to 11 that are neither even nor multiples of 3 are 1, 5, 7 and 11, as in question 8.

**11.** The only divisors of a prime number $n$ are 1 and $n$ itself. A number $k$ from 1 to $n - 1$ is too small to have $n$ as a divisor, so $k$ and $n$ share only the divisor 1, and every $k$ works: 6 [values](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) for 7, 10 for 11.

**12.** **Result.** For 12 points, only $k = 1$, 5, 7 and 11 draw all 12 points in one line: $k = 1$ and 11 the regular 12-sided polygon, $k = 5$ and 7 a 12-pointed star; the other [values](https://one-course.com/books/computer-science/1/en/chapter/15-variables-and-input#def-g6-variables-and-input-value) of $k$ go several times around a smaller shape.
