# 7. New Experiments: Sum of Squares and Plotting II¶

I hope you had a wonderful time during the holiday break. In today’s lecture, we will revisit the problem on sum of squares of integers, which we began to explore just before the holiday break. The problems is as follows:

Question: Given that and are integers, what integers can be written in the form ?

Here are a cases that do and do not work.

works;

does not work, since we only allow integers. is too small, is too big;

actually does work, because we allow . (That’s just one of our rules; you could play a different game if you wanted.);

That means works.

works.

And so forth. In our last lecture, we were able to pose the following questions relating to the sum of squares:

Which positive integers can be written as a sum of two squares?

Which ones cannot?

Which positive integers can be written as a sum of squares of it factors?

Can there be a formula for expressing as the sum of two positive square numbers?

Which numbers can be written as a sum of squares of 2 prime numbers?

These numbers that can be written as a sum of two squares, are they odd or even?

Is the sum of squares even or odd?

Given and are prime, is the sum of their squares, prime?

Can the square roots of numbers be written in any other form aside from ?

Is there more than one way to get the , and if they is, what’s the relationship between the , and .

In our last lecture, we started by first tried working with pens and paper. We tried by listing those positive integers ranging from to that can be written as a sum of squares and those that can not be written.

What possible *Python* buitin data structure(s) do you think will be
best in storing the numbers that can be written?

```
my_list_database = [0, 1, 4, 5]
```

What if, in addition to the possible positive integers that can we can
write as the sum of two squares, we also want to know the number of ways
that they can be written. What *Python* builtin data structure(s) do you
think will be suitable in this case?

Let us create a database of some of the numbers that can be written starting with those that we tried with pens and paper.

```
number_of_ways = [1, 2, ]
```

```
sum_of_square_database = {0: [(0,0)], 1: [(1,0), (0,1)], 2:[(1,1)]}; sum_of_square_database
```

```
{0: [(0, 0)], 1: [(1, 0), (0, 1)], 2: [(1, 1)]}
```

```
sum_of_square_database[4] = [(0,2), (2, 0)]
```

```
sum_of_square_database
```

```
{0: [(0, 0)], 1: [(1, 0), (0, 1)], 2: [(1, 1)], 4: [(0, 2), (2, 0)]}
```

```
for k in sum_of_square_database:
print(k)
```

```
0
1
2
4
```

```
list(iter(sum_of_square_database))
```

```
[0, 1, 2, 4]
```

```
sum_of_square_database
```

```
{0: [(0, 0)], 1: [(1, 0), (0, 1)], 2: [(1, 1)], 4: [(0, 2), (2, 0)]}
```

```
sum_of_square_database = {}
for a in range(10):
for b in range(10):
m = a^2 + b^2
```

After sometime you will want to think of an effective way of doing this
by using some *Python* programming construct? What could be your
possible effective solution?

Hopefully, by now you should have a fair amount of data in
`sum_of_square_database`

. Let us now begin exploring the questions we
posed regarding the sum of two squares by querying the database we have
created.

After a while, we will come back and I will ask you about possible ways to rethink the problem on the sum of squares.

Can you think of a way to solve this problem? *Hint: What do you know
about equations of this form :math:`n = a^{2} + b ^{2}`?*

This brings up the question of how to do such plotting in the first place? We will look at this in more detail in the next Section.

## 7.1. More Plotting and Graphics in Two Dimensions¶

There is absolutely no way we could cover all the graphics in *SageMath*
(or the program that does it for *SageMath*, matplotlib) in one day, or
even a week. We will look at a bunch of different resources you have
access to, and see lots of different plot types. As we go through these
resources, think of how they will be of in solving our new experiment or
where you could have used them some time back in your academic career. I
definitely do not know everything about the plotting command.
Nevertheless, you may interrupt me with your question so that we can
explore the solution together.

We will begin with the advanced 2D-plotting in the built-in live documentation. (This link only works if you are already using

*SageMath*; the non-live version is here.There are two very important pages with many, MANY examples - the documentation for plot and that for showing graphics.

The general plotting reference - there are a lot of surprises, like animation, etc.

*SageMath*to explore the sums of squares,

*or*try to recreate your favorite graphic using

*SageMath*!