NumPy Project Euler Problem 6

Project Euler Problem 6 is perfect to demonstrate the power of NumPy. No loops are required and only a few lines of code.

1. Create an array with the first 100 natural numbers

First we will create a NumPy array of the numbers 1 – 100 with the arange function.

1
a = numpy.arange(101)

2. Sum the squares of the numbers

Second we will sum the squares of the numbers with the sum function.

1
sum_squares = numpy.sum(a ** 2)

3. Square the sum of the numbers

The NumPy ndarray class has a sum method, that we can use to sum the numbers in our array. After that calculate the square of the sum.

1
square_sum = a.sum() ** 2

Below is the complete solution.

1
print square_sum - sum_squares
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy
 
#The sum of the squares of the first ten natural numbers is,
#1 ** 2 + 2 ** 2 + ... + 10 ** 2 = 385
 
#The square of the sum of the first ten natural numbers is,
#(1 + 2 + ... + 10) ** 2 = 55 ** 2 = 3025
 
#Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
 
#Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
 
# 1. Create an array with the first 100 natural numbers
a = numpy.arange(101)
 
# 2. Sum the squares of the numbers
sum_squares = numpy.sum(a ** 2) 
 
# 3. Square the sum of the numbers
square_sum = a.sum() ** 2
 
# Calculate the difference
print square_sum - sum_squares


If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Share
Posted in programming | Leave a comment

Linear Algebra Book review

I had to read “Linear Algebra and Its Applications” by David Lay for the Linear Algebra 1 class in my first semester in University. So this is a gentle introduction to Linear Algebra. The book doesn’t assume a lot of previous knowledge.

Linear Algebra Book
Author David C. Lay
ISBN-10 0201845563

Chapter Structure

Each chapter starts with an introductory example. Each section within a chapter ends with practice problems and exercises. Worked out examples with solutions are given too. As you would expect from a Linear Algebra book, there are lots of theorems and numerical notes.

1. Systems of Linear Equations

The first chapter gives some examples of linear systems. The row reduction algorithm is explained. I remember having to solve these kind of problems by hand for weeks. As is usual in mathematics, we learn to work out something with paper and pencil the hard way and then we figure out how to do it faster by writing a computer program. If you are into Python, please check out NumPy.

2. Vector and Matrix Equations

Chapter 2 starts with a number of examples as well. We learn about the fundamental idea of representing a linear combination of vectors as a product of a matrix and a vector. This leads to this famous equation:

A x = b

3. Matrix Algebra

Chapter 3 teaches about matrix operations such as matrix multiplication, matrix inversion and transposing matrices. The chapter ends with the Leontief Input Output Model from economics and applications to computer graphics.

4. Determinants

The introductory example in this chapter is about determinants in analytic geometry. Properties of determinants are mentioned as well as calculation methods.

5. Vector Spaces

I don’t know if it has anything to do with the chapter title, but the first example of this chapter is about space flight and control systems. In my opinion this chapter is more theoretical than the preceding chapters. The chapter ends with applications to difference equations and Markov Chains.

6. Eigenvalues and Eigenvectors

Dynamical systems and spotted owls are the topic of the introductory example of chapter 6. This chapter covers amongst others the characteristic equation, diagonalization and iterative algorithms to estimate eigenvalues.

7. Orthogonality and Least Squares

Chapter 7 begins with a short text about the North American Datum. After that we continue with sections on:

  • orthogonality
  • orthogonal sets
  • orthogonal projections
  • the Gram-Schmidt process
  • least square problems
  • inner product spaces

8. Symmetric Matrices and Quadratic Forms

A story about multi channel image processing is the introduction of chapter 8. This chapter has sections on quadratic forms and singular value decomposition.


The book is very readable and entertaining. The diverse list of examples are already reason enough to recommend “Linear Algebra and Its Applications”. I give this book 5 stars out of 5.

Share
Posted in books | Leave a comment