Posts (old posts, page 3)

NumPy mgrid vs. meshgrid

The meshgrid function is useful for creating coordinate arrays to vectorize function evaluations over a grid. Experienced NumPy users will have noticed some discrepancy between meshgrid and the mgrid, a function that is used just as often, for exactly the same purpose. What is the discrepancy, and why does a discrepancy even exist when "there should be one - and preferably only one - obvious way to do it." [1]

First, recall that meshgrid behaves as follows:

>>> import numpy as np
>>> x1, y1 = np.meshgrid(np.arange(1, 11, 2), np.arange(-12, -3, 3))
>>> x1
array([[1, 3, 5, 7, 9],
       [1, 3, 5, 7, 9],
       [1, 3, 5, 7, 9]])
>>> y1
array([[-12, -12, -12, -12, -12],
       [ -9,  -9,  -9,  -9,  -9],
       [ -6,  -6,  -6,  -6,  -6]])

Read more…

Quotes: "One broken window starts the process toward decay"

A building with broken windows looks like nobody cares about it. So other people stop caring. They allow more windows to become broken. Eventually they actively break them. They despoil the facade with graffiti and allow garbage to collect. One broken window starts the process toward decay.

—Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

Quotes: "It's harder to read code than to write it"

We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by incremental renovation: tinkering, improving, planting flower beds.

There's a subtle reason that programmers always want to throw away the code and start over. The reason is that they think the old code is a mess. And here is the interesting observation: they are probably wrong. The reason that they think the old code is a mess is because of a cardinal, fundamental law of programming:

It’s harder to read code than to write it.

—Joel Spolsky, Things You Should Never Do, Part I

Project Euler Problem 42: Coded triangle numbers

The nth term of the sequence of triangle numbers is given by, $T_n = \frac{n(n+1)}{2}$; so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = $T_{10}$. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

Read more…

Project Euler Problem 43: Sub-string divisibility

The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.

Let $d_1$ be the 1st digit, $d_2$ be the 2nd digit, and so on. In this way, we note the following:

$d_2d_3d_4=406$ is divisible by 2

$d_3d_4d_5=063$ is divisible by 3

$d_4d_5d_6=635$ is divisible by 5

$d_5d_6d_7=357$ is divisible by 7

$d_6d_7d_8=572$ is divisible by 11

$d_7d_8d_9=728$ is divisible by 13

$d_8d_9d_{10}=289$ is divisible by 17

Find the sum of all 0 to 9 pandigital numbers with this property.

Read more…

Project Euler Problem 44: Pentagon numbers

Pentagonal numbers are generated by the formula, $P_n=\frac{n(3n−1)}{2}$. The first ten pentagonal numbers are:

1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...

It can be seen that $P_4 + P_7 = 22 + 70 = 92 = P_8$. However, their difference, $70 − 22 = 48$, is not pentagonal.

Find the pair of pentagonal numbers, $P_j$ and $P_k$, for which their sum and difference are pentagonal and $D = |P_k − P_j|$ is minimised; what is the value of $D$?

In [1]:
def polygonal(s):
    c = s - 2
    a = b = 1
    while True:
        yield a
        b += c
        a += b
In [2]:
def sum_diff_polygonal(s):
    seen = set()
    for i in polygonal(s):
        for j in seen:
            p = i - j
            q = j
            # we already know `p+q=i` is s-gonal 
            # since `i` must be; just need to check 
            # that `p` and `p-q` are as well
            if p in seen and p-q in seen:
                yield p, q    
        seen.add(i)
In [3]:
it = sum_diff_polygonal(5)
In [4]:
next(it)
Out[4]:
(7042750, 1560090)

Project Euler Problem 45: Triangular, pentagonal, and hexagonal

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle

$$T_n = \frac{n(n+1)}{2} \\ 1, 3, 6, 10, 15, \dotsc$$

Pentagonal

$$P_n = \frac{n(3n−1)}{2} \\ 1, 5, 12, 22, 35, \dotsc$$

Hexagonal

$$H_n = n(2n−1) \\ 1, 6, 15, 28, 45, \dotsc$$

It can be verified that $T_{285} = P_{165} = H_{143} = 40755$.

Find the next triangle number that is also pentagonal and hexagonal.

Read more…