Domino Sums Take 2

A single domino is a rectangular tile divided into two square ends marked with spots. A domino set of order n n consists of all the domino tile combinations with spot counts from 0 0 to n n inclusive.

T ( n ) T(n) is the sum of the totals of the two spot counts on each tile in the domino set of order n n , and P ( n ) P(n) is the sum of the products of the two spot counts on each tile in the domino set of order n n .

For example, a domino set of order 2 2 would consist of the domino tiles with numbers ( 0 , 0 ) (0, 0) , ( 0 , 1 ) (0, 1) , ( 0 , 2 ) (0, 2) , ( 1 , 1 ) (1, 1) , ( 1 , 2 ) (1, 2) , ( 2 , 2 ) (2, 2) , such that T ( 2 ) = ( 0 + 0 ) + ( 0 + 1 ) + ( 0 + 2 ) + ( 1 + 1 ) + ( 1 + 2 ) + ( 2 + 2 ) = 12 P ( 2 ) = ( 0 0 ) + ( 0 1 ) + ( 0 2 ) + ( 1 1 ) + ( 1 2 ) + ( 2 2 ) = 7. \begin{aligned} T(2) &= (0 + 0) + (0 + 1) + (0 + 2) + (1 + 1) + (1 + 2) + (2 + 2) \\&= 12\\\\ P(2) &= (0 \cdot 0) + (0 \cdot 1) + (0 \cdot 2) + (1 \cdot 1) + (1 \cdot 2) + (2 \cdot 2) \\&= 7. \end{aligned} What is lim n n T ( n ) P ( n ) ? {\displaystyle \lim_{n\to\infty}} \frac{n T(n)}{P(n)}?


The answer is 4.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

6 solutions

Zico Quintina
Jun 27, 2018

Let k k , for 0 k n 0 \le k \le n , be one of the numbers in the domino set of order n n . Then the tiles containing k k are ( 0 , k ) , ( 1 , k ) , ( k , k ) , ( n , k ) (0, k), (1, k), \ldots (k, k), \ldots (n, k) . There are n + 1 n + 1 such tiles, so due to the one double tile the number k k will appear on the tiles exactly n + 2 n + 2 times. This gives us a formula for T ( n ) T(n) :

T ( n ) = ( n + 2 ) ( 1 + 2 + 3 + + n ) = ( n + 2 ) k = 0 n k = ( n + 2 ) [ n ( n + 1 ) 2 ] = n ( n + 1 ) ( n + 2 ) 2 \begin{array}{rl} T(n) &= \ \ (n + 2) (1 + 2 + 3 + \ldots + n) \\ \\ &= \ \ (n + 2) \sum_{k = 0}^n k \\ \\ &= \ \ (n + 2) \left[ \dfrac{n(n + 1)}{2} \right] \\ \\ &= \dfrac{n(n + 1)(n + 2)}{2} \end{array}

To generate a formula for P ( n ) P(n) , we multiply ( 0 + 1 + 2 + + n ) ( 0 + 1 + 2 + + n ) = ( 0 0 ) + ( 0 1 ) + ( 1 0 ) + ( 0 2 ) + ( 1 1 ) + ( 2 0 ) + + ( n n ) (0 + 1 + 2 + \ldots + n) (0 + 1 + 2 + \ldots + n) = (0 \cdot 0) + (0 \cdot 1) + (1 \cdot 0) + (0 \cdot 2) + (1 \cdot 1) + (2 \cdot 0) + \ldots + (n \cdot n) . This gives us all the terms we need, but every product not of the form ( k k ) (k \cdot k) is represented twice.

To get around this, we add in a second set of ( k k ) (k \cdot k) 's, so that now every term is included twice, and then take half of that sum.

P ( n ) = 1 2 [ ( 0 + 1 + 2 + + n ) ( 0 + 1 + 2 + + n ) + ( 1 1 + 2 2 + 3 3 + + n n ) ] = 1 2 [ ( k = 0 n k ) 2 + k = 0 n k 2 ] = 1 2 ( n 2 ( n + 1 ) 2 4 + n ( n + 1 ) ( 2 n + 1 ) 6 ) = n ( n + 1 ) 24 [ 3 n ( n + 1 ) + 2 ( 2 n + 1 ) ] = n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) 24 \begin{array}{rl} P(n) &= \ \ \dfrac{1}{2} \left[ (0 + 1 + 2 + \ldots + n) (0 + 1 + 2 + \ldots + n) + (1 \cdot 1 + 2 \cdot 2 + 3 \cdot 3 + \ldots +n \cdot n) \right] \\ \\ &= \ \ \dfrac{1}{2} \left[ \left( \sum_{k = 0}^n k \right)^2 + \sum_{k = 0}^n k^2 \right] \\ \\ &= \ \ \dfrac{1}{2} \left( \dfrac{n^2(n + 1)^2}{4} + \dfrac{n(n + 1)(2n + 1)}{6} \right) \\ \\ &= \ \ \dfrac{n(n + 1)}{24} \left[ 3n(n + 1) + 2(2n + 1) \right] \\ \\ &= \ \ \dfrac{n(n + 1)(n + 2)(3n + 1)}{24} \end{array}

Then we have

n T ( n ) P ( n ) = n 2 ( n + 1 ) ( n + 2 ) 2 n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) 24 = 12 n 2 ( n + 1 ) ( n + 2 ) n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) = 12 n 3 n + 1 \begin{array}{rl} \dfrac{n \cdot T(n)}{P(n)} &= \ \ \dfrac{\dfrac{n^2(n + 1)(n + 2)}{2}}{\dfrac{n(n + 1)(n + 2)(3n + 1)}{24}} \\ \\ &= \ \ \dfrac{12n^2(n + 1)(n + 2)}{n(n + 1)(n + 2)(3n + 1)} \\ \\ &= \ \ \dfrac{12n}{3n + 1} \end{array}

and so

lim n n T ( n ) P ( n ) = lim n 12 n 3 n + 1 = 4 \begin{array}{rl} \lim_{n \to \infty} \dfrac{n \cdot T(n)}{P(n)} &= \ \ \lim_{n \to \infty} \dfrac{12n}{3n + 1} \\ \\ &= \boxed{4} \end{array}

Great solution! This is similar to how I did it.

David Vreken - 2 years, 11 months ago
Chew-Seong Cheong
Jun 28, 2018

Note that

T ( n ) = j = 0 n k = j n ( j + k ) = j = 0 n ( ( n j + 1 ) j + n j + 1 2 ( j + n ) ) = j = 0 n ( 2 n + 3 ) j 3 j 2 + n ( n + 1 ) 2 = 1 2 ( n ( n + 1 ) ( 2 n + 3 ) 2 3 × n ( n + 1 ) ( 2 n + 1 ) 6 + n ( n + 1 ) 2 ) = n ( n + 1 ) ( n + 2 ) 2 \begin{aligned} T(n) & = \sum_{j=0}^n \sum_{k=j}^n (j+k) \\ & = \sum_{j=0}^n \left((n-j+1)j + \frac {n-j+1}2 (j+n) \right) \\ & = \sum_{j=0}^n \frac {(2n+3)j - 3j^2 + n(n+1)}2 \\ & = \frac 12 \left(\frac {n(n+1)(2n+3)}2 - 3 \times \frac {n(n+1)(2n+1)}6 + n(n+1)^2 \right) \\ & = \frac {n(n+1)(n+2)}2 \end{aligned}

and that

P ( n ) = j = 0 n k = j n j k = j = 1 n k = j n j k = j = 1 n j ( n j + 1 2 ( j + n ) ) = 1 2 j = 1 n ( n ( n + 1 ) j + j 2 j 3 ) = 1 2 ( n 2 ( n + 1 ) 2 2 + n ( n + 1 ) ( 2 n + 1 ) 6 n 2 ( n + 1 ) 2 4 ) = 1 2 ( n 2 ( n + 1 ) 2 4 + n ( n + 1 ) ( 2 n + 1 ) 6 ) = n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) 24 \begin{aligned} P(n) & = \sum_{\color{#3D99F6}j=0}^n \sum_{k=j}^n jk = \sum_{\color{#D61F06}j=1}^n \sum_{k=j}^n jk \\ & = \sum_{j=1}^n j \left(\frac {n-j+1}2 (j+n) \right) \\ & = \frac 12 \sum_{j=1}^n \left(n(n+1)j+j^2-j^3 \right) \\ & = \frac 12 \left(\frac {n^2(n+1)^2}2 + \frac {n(n+1)(2n+1)}6 - \frac {n^2(n+1)^2}4 \right) \\ & = \frac 12 \left(\frac {n^2(n+1)^2}4 + \frac {n(n+1)(2n+1)}6 \right) \\ & = \frac {n(n+1)(n+2)(3n+1)}{24} \end{aligned}

Therefore,

lim n n T ( n ) P ( n ) = lim n 24 n 2 ( n + 1 ) ( n + 2 ) 2 n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) = lim n 12 n 3 n + 1 Divide up and down by n = lim n 12 3 + 1 n = 4 \begin{aligned} \lim_{n \to \infty} \frac {nT(n)}{P(n)} & = \lim_{n \to \infty} \frac {24n^2(n+1)(n+2)}{2n(n+1)(n+2)(3n+1)} \\ & = \lim_{n \to \infty} \frac {12n}{3n+1} & \small \color{#3D99F6} \text{Divide up and down by }n \\ & = \lim_{n \to \infty} \frac {12}{3+\frac 1n} \\ & = \boxed{4} \end{aligned}

Very nice solution!

David Vreken - 2 years, 11 months ago

Log in to reply

Thanks. No upvote?

Chew-Seong Cheong - 2 years, 11 months ago

Log in to reply

I just did, thanks for the reminder.

David Vreken - 2 years, 11 months ago

Log in to reply

@David Vreken Thanks too.

Chew-Seong Cheong - 2 years, 11 months ago

I did a similar method, but found it easier to sum k from 0 to j. (Saying WLOG k <= j rather than k >= j ).

Alex Burgess - 2 years, 10 months ago
Romain Bouchard
Jun 27, 2018

By arranging the dominoes, we can write T ( n ) T(n) as : i = 0 n ( j = i n i + j ) \sum_{i=0}^{n} (\sum_{j=i}^{n} i+j) and P ( n ) P(n) as i = 0 n ( j = 0 i i j ) \sum_{i=0}^{n} (\sum_{j=0}^{i} i\cdot j)

Hence T ( n ) = i = 0 n ( n + i 1 ) i + ( j = i n j ) = i = 0 n ( ( n + i 1 ) i + n ( n + 1 ) 2 i ( i 1 2 ) = n ( n + 1 ) 2 + i = 0 n ( n 3 2 ) i 2 + i 2 = n ( n 2 + 3 n + 2 ) 2 = n ( n + 1 ) ( n + 2 ) 2 T(n) = \sum_{i=0}^{n} (n+i-1)*i+(\sum_{j=i}^{n} j) = \sum_{i=0}^{n} ((n+i-1)*i + \frac{n(n+1)}{2}-\frac{i(i-1}{2}) = \frac{n(n+1)}{2} + \sum_{i=0}^{n} (n-\frac{3}{2})*i^2+\frac{i}{2} = \frac{n(n^2+3n+2)}{2} = \frac{n(n+1)(n+2)}{2}

Similarly, P ( n ) = i = 0 n i ( j = i n j ) = i = 0 n i 2 ( i + 1 ) 2 = 1 2 i = 0 n i 3 + i 2 = n ( n + 1 ) ( 2 n + 1 ) 6 + ( n ( n + 1 ) ) 2 4 = n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) 24 P(n) = \sum_{i=0}^{n} i\cdot(\sum_{j=i}^{n} j) = \sum_{i=0}^{n} \frac{i^2(i+1)}{2} = \frac{1}{2}\sum_{i=0}^{n} i^3 + i^2 = \frac{n(n+1)(2n+1)}{6} + \frac{(n(n+1))^2}{4} = \frac{n(n+1)(n+2)(3n+1)}{24}

Thus :

lim n > n T ( n ) P ( n ) = lim n > 12 n 2 ( n 2 + 3 n + 2 ) n ( n + 1 ) ( n + 2 ) ( 3 n + 1 ) = lim n > 12 n 4 3 n 4 = 12 3 = 4 \lim_{n->\infty} \frac{nT(n)}{P(n)}= \lim_{n->\infty} \frac{12n^2(n^2+3n+2)}{n(n+1)(n+2)(3n+1)} = \lim_{n->\infty} \frac{12n^4}{3n^4} = \frac{12}{3} = \boxed{4}

Nicely done!

David Vreken - 2 years, 11 months ago
Erik Ludwig
Jul 11, 2018

Since we only want to compute lim n n T ( n ) P ( n ) \lim\limits_{n \to \infty} \frac{n T(n)}{P(n)} it is not necessary to calculate T ( n ) T(n) and P ( n ) P(n) in full detail. We will estimates them (sufficiently).

Preamble: (what you need to know)

We will make use of the telescope-identity a n a 0 = k = 1 n ( a k a k 1 ) a_n-a_0 =\sum\limits^n_{k=1}\left( a_{k}-a_{k-1}\right) which can simply proven by canceling the middle terms (See A1).

and we will use the following "antiderivitive"-rule, which is a discrete equivalent to t l d t = 1 l + 1 t l + 1 \displaystyle \int t^l \,\mathrm{d} t=\tfrac1{l+1}t^{l+1} :

Let n n be a non-negative integer, then F ( n ) = k = 1 n k l F(n)=\sum\limits^n_{k=1} k^l is a polynomial of degree l + 1 l+1 with leading coefficient 1 l + 1 \frac1{l+1} . (See A2.)

Furthermore if f ( k ) f(k) is a polynomial of degree l l with leading coefficient a l a_l , then F ( n ) = k = 1 n f ( k ) F(n)=\sum\limits^n_{k=1} f(k) is a polynomial of degree l + 1 l+1 leaded by the coefficient a l l + 1 \dfrac{a_l}{l+1} .

The Solution:

By the telescope-identity we get T ( n ) = T ( 0 ) + k = 1 n ( T ( k ) T ( k 1 ) ) \quad T(n) = T(0)+\sum\limits^{n}_{k=1}(T(k)-T(k-1))\quad and the same for P P instead of T T . But T ( k ) T ( k 1 ) T(k)-T(k-1) is just sum for the domino pairs containing k k which are the k + 1 k+1 dominos ( k , j ) (k, j) for 0 j k 0 \leq j \leq k .

So the sum is j = 0 k ( k + j ) = ( k + 1 ) k + j = 0 k j \quad \sum^k_{j=0} (k+j)=(k+1)k+\sum^k_{j=0}j\quad and using "antiderivitive"-rule we get T ( k ) T ( k 1 ) = ( k + 1 ) k + j = 0 k j = k 2 + k + ( 1 2 k 2 + r 1 ( k ) ) = 3 2 k 2 + r ~ 1 ( k ) T(k)-T(k-1) = (k+1)k+\sum^k_{j=0}j = k^2+k+(\tfrac12 k^2+r_1(k)) =\tfrac32k^2 +\tilde r_1(k)
with r ~ 1 ( k ) \tilde r_1(k) a degree 1 or less polynomial. And hence T ( n ) = T ( 0 ) + k = 1 n ( T ( k ) T ( k 1 ) ) = T ( 0 ) + k = 1 n ( 3 2 k 2 + r ~ 1 ( k ) ) = T ( 0 ) + 1 3 3 2 n 3 + k = 1 n r ~ 1 ( k ) = 1 2 n 3 + r T ( n ) T(n) = T(0)+\sum\limits^{n}_{k=1}(T(k)-T(k-1))= T(0)+\sum\limits^{n}_{k=1}\left(\tfrac32k^2 +\tilde r_1(k)\right) = T(0)+\tfrac13\cdot \tfrac32 n^3+\sum\limits^{n}_{k=1}\tilde r_1(k) = \tfrac12 n^3+ r_T(n) where r T ( n ) r_T(n) is a polynomial with degree 2 or less.

For P ( k ) P ( k 1 ) P(k)-P(k-1) is the sum of the product of the domino pairs ( k , j ) (k,j) and thus: P ( k ) P ( k 1 ) = j = 0 k k j = k j = 0 k j = k ( 1 2 k 2 + r 1 ( k ) ) = 1 2 k 3 + r 2 ( k ) P(k)-P(k-1)= \sum^{k}_{j=0}k \cdot j =k\cdot \sum^{k}_{j=0}j=k \cdot (\tfrac12 k^2+r_1(k)) = \tfrac12 k^3+r_2(k) with r 2 ( k ) r_2(k) a polynomial of degree 2 or less.

Using the same Idea for P P we estimate P ( n ) = P ( 0 ) + k = 1 n ( P ( k ) P ( k 1 ) ) = P ( 0 ) + k = 1 n ( 1 2 k 3 + r 2 ( k ) ) = P ( 0 ) + 1 4 1 2 n 4 + k = 1 n r 2 ( k ) = 1 8 n 4 + r P ( n ) P(n) = P(0)+\sum\limits^{n}_{k=1}(P(k)-P(k-1))= P(0)+\sum\limits^{n}_{k=1} \left(\tfrac12 k^3+r_2(k)\right)=P(0)+\tfrac14 \cdot \tfrac12 n^4 +\sum\limits^{n}_{k=1}r_2(k) = \tfrac18 n^4+r_P(n) with r P ( n ) r_P(n) a polynomial of degree 3 or less.

Since r T ( n ) r_T(n) and r P ( n ) r_P(n) are polynomials of degree 2 and 3 respectively, r T ( n ) n 3 \dfrac{r_T(n)}{n^3} and r P ( n ) n 4 \dfrac{r_P(n)}{n^4} tend to be 0 0 for n n \to \infty .

Now we calculate lim n n T ( n ) P ( n ) = lim n n ( 1 2 n 3 + r T ( n ) ) 1 8 n 4 + r P ( n ) = lim n n 4 ( 1 2 + r T ( n ) n 3 ) n 4 ( 1 8 + r P ( n ) n 4 ) = 4 \lim\limits_{n \to \infty} \frac{n T(n)}{P(n)} = \lim\limits_{n \to \infty} \frac{n \left(\tfrac12 n^3+ r_T(n)\right)}{\tfrac18 n^4+r_P(n)} = \lim\limits_{n \to \infty} \frac{n^4 \left(\tfrac12+ \tfrac{r_T(n)}{n^3}\right)}{n^4\left(\tfrac18+\tfrac{r_P(n)}{n^4}\right)} = \underline{\underline{4}}

Appendix

A1: k = 1 n ( a k a k 1 ) = ( k = 1 n a k ) ( k = 1 n a k 1 ) = k = 1 n a k k = 0 n 1 a k = a n + ( k = 1 n 1 a k ) ( k = 1 n 1 a k ) a 0 = a n a 0 \sum\limits^n_{k=1}\left( a_{k}-a_{k-1}\right)=\left(\sum\limits^n_{k=1}a_{k}\right)-\left(\sum\limits^n_{k=1}a_{k-1}\right)=\sum\limits^n_{k=1}a_{k}-\sum\limits^{n-1}_{k=0}a_{k}= a_n + \left(\sum\limits^{n-1}_{k=1}a_{k}\right)-\left(\sum\limits^{n-1}_{k=1}a_{k}\right)-a_0=a_n-a_0

A2: Using the binomial theorem ( a + b ) l = i = 0 l ( n i ) a l i b i (a+b)^l=\sum\limits^l_{i=0} \binom{n}{i}a^{l-i}b^{i} we will get the identity k l + 1 ( k 1 ) l + 1 = k l + 1 i = 0 l + 1 ( l + 1 i ) k l + 1 i ( 1 ) i = i = 1 l + 1 ( l + 1 i ) k l + 1 i ( 1 ) i + 1 k^{l+1} - (k-1)^{l+1} = k^{l+1} - \sum\limits^{l+1}_{i=0} \binom{l+1}{i}k^{l+1-i}(-1)^{i} = \sum\limits^{l+1}_{\color{#D61F06}i=1} \binom{l+1}{i}k^{l+1-i}(-1)^{\color{#D61F06}i+1} and with above telescope-identity n l + 1 = n l + 1 0 l + 1 = k = 1 n k l + 1 ( k 1 ) l + 1 = k = 1 n i = 1 l + 1 ( l + 1 i ) k l + 1 i ( 1 ) i + 1 = k = 1 n ( ( l + 1 ) k l + r ( k ) ) n^{l+1}=n^{l+1}-0^{l+1} = \sum^n_{k=1} k^{l+1} - (k-1)^{l+1} = \sum^n_{k=1}\sum\limits^{l+1}_{i=1} \binom{l+1}{i}k^{l+1-i}(-1)^{i+1}= \sum^n_{k=1} \left((l +1)k^{l}+ r(k)\right) So every Monomial n l + 1 n^{l+1} is just a sum of a polynomial p ( k ) p(k) of degree l l with leading coefficient l + 1 l+1 . The r ( k ) r(k) is the rest-term which is of degree l 1 l-1 .

As polynomials of degree j j the p j ( k ) : = i = 1 j + 1 ( j + 1 i ) k j + 1 i ( 1 ) i + 1 p_j(k) :=\sum\limits^{j+1}_{i=1} \binom{j+1}{i}k^{j+1-i}(-1)^{i+1} for different j j are indepentent and k l k^l is just a combination of the ones with degree j l j \leq l . The leading coefficient for p l ( k ) p_l(k) have to be 1 l + 1 \frac1{l+1} (why?). Hence k = 1 n k l = P ( n ) \sum\limits^n_{k=1} k^l = P(n) is the same combination of the k = 1 n p j ( k ) \sum\limits^n_{k=1}p_j(k) and therefore a Polynomial of degree l + 1 l+1 with leading coefficient 1 l + 1 \frac1{l+1} .

Filip Kramaric
Jul 9, 2018

Integer Sequence Database and Wolfram are needed for this to work.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import scipy.special
import itertools
final, final1 = [], []
def totals(n):
    thelist = list(itertools.combinations(list(range(n+1)), 2))
    counter = 0
    for z in thelist:
        counter += z[0]
        counter += z[1]
    counter += n*(n+1)
    return counter
def product(n):
    thelist = list(itertools.combinations(list(range(n+1)), 2))
    counter = 0
    temp = 0
    for z in thelist:
        temp = z[0]*z[1]
        counter += temp
    counter += n*(n+1)*(2*n+1)/6
    return int(counter)
for x in range(15):
    final.append(totals(x))
for x in range(15):
    final1.append(product(x))
print(final)
print(final1)

Nice job!!! Upvote here

Michael Fitzgerald - 2 years, 11 months ago

Log in to reply

Very simple. I always tend to complicate things :)

Filip Kramaric - 2 years, 11 months ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import itertools
final, final1 = [], []
def totals(n):
    thelist = list(itertools.combinations(list(range(n+1)), 2))
    counter = 0
    for z in thelist:
        counter += z[0]
        counter += z[1]
    counter += n*(n+1)
    return counter
def product(n):
    thelist = list(itertools.combinations(list(range(n+1)), 2))
    counter = 0
    temp = 0
    for z in thelist:
        temp = z[0]*z[1]
        counter += temp
    counter += n*(n+1)*(2*n+1)/6
    return int(counter)
for x in range(1,1000):
    final.append(totals(x))
for x in range(1,1000):
    final1.append(product(x))

print final
print final1    

for k,i in enumerate(final): 
    print k, i, final1[k], (k+1.)*i/final1[k]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
…………………..
…………………..
988 485149005 119993520570 3.99865229111
989 486620640 120479160120 3.99865365197
990 488095248 120966272296 3.99865501009
991 489572832 121454860072 3.99865636547
992 491053395 121944926425 3.99865771812
993 492536940 122436474335 3.99865906805
994 494023470 122929506785 3.99866041527
995 495512988 123424026761 3.99866175979
996 497005497 123920037252 3.9986631016
997 498501000 124417541250 3.99866444073
998 499999500 124916541750 3.99866577718

Michael Fitzgerald - 2 years, 11 months ago

added this to the end of your script

Michael Fitzgerald - 2 years, 11 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np
#solution using numpy (vectorization)
def unique_rows(a, **kwargs):
    rowtype = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    b = np.ascontiguousarray(a).view(rowtype)
    return_index = kwargs.pop('return_index', False)
    out = np.unique(b, return_index=True, **kwargs)
    idx = out[1]
    uvals = a[idx]
    if (not return_index) and (len(out) == 2):
        return uvals
    elif return_index:
        return (uvals,) + out[1:]
    else:
        return (uvals,) + out[2:]

def cartesian(*arrays):
    mesh = np.meshgrid(*arrays)  # standard numpy meshgrid
    dim = len(mesh)  # number of dimensions
    elements = mesh[1].size  # number of elements, any index will do
    flat = np.concatenate(mesh).ravel()  # flatten the whole meshgrid
    reshape = np.reshape(flat, (dim, elements)).T  # reshape and transpose
    return reshape

for i in range(3,1002):
    x = np.arange(i)
    data = np.array(cartesian(x, x),dtype='int64')
    y = unique_rows(np.sort(data))
    t = np.sum(y)   # same as t = np.sum(np.sum(y, axis=1))
    p = np.sum(np.product(y, axis=1))
    print len(x)-1, t, p, (len(x)-1.)*t/p

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
n T(n)  P(n)   nT(n)/P(n)
2 12 7 3.42857142857
3 30 25 3.6
4 60 65 3.69230769231
5 105 140 3.75
6 168 266 3.78947368421
…………………
………………...
50 66300 834275 3.97350993377
…………………
………………...
215 5038740 271252170 3.99380804954
…………………
………………...
267 9624282 643222847 3.99501246883
…………………
………………...
332 18462852 1533955287 3.99598796389
…………………
…………………
998 498501000 124417541250 3.9986644407345575
999 499999500 124916541750 3.9986657771847898
1000 501501000 125417041750 3.9986671109630123
1001 503005503 125919044251 3.9986684420772303
1002 504513012 126422552257 3.9986697705354173
…………………
…………………
5000 62537505000 78177092708750 3.999733351109926
…………………
…………………
10000 500150010000 1250416704167500 3.999866671110963
…………………
…………………

This is numpy solution w/ much faster processing

Michael Fitzgerald - 2 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...