Calculating M(n)

and Optimizing M(n) Algorithms

Dave Purdum

Dr. Jonathan Webster

Definition

Define M(n) to be the number of distinct integers in the n by n multiplication table.

A027424

The number of different products calculated to create an n by n multiplication table.

M(4)

  1     2    3    4           5          6    7                  8                       9

Multiplication table

Set of possible numbers in the multiplication table

= 9

There are 9 different integers in the 4 by 4 multiplication table.

Naive Algorithm

Directly calculate M(n) for a given value of n.

Time Complexity

Space Complexity

O(n^2)
O(n^2)

number of products that are calculated

number of possible values that can be crossed off

Optimizations

Optimizations

1

 1    2           3

1    2    3     4          5                 6

1    2    3     4          5           6    7                  8                       9

M(1) = 1

M(2) = 3

M(3) = 6

M(4) = 9

Tabulation

Repeated Evaluation

Calculating M(1), M(2), M(3), M(4)

Calculate 10 products

Calculate 20 products

Optimizations

Time Complexity

Space Complexity

O(n^2)
O(n^2)

Evaluation

O(n^3)
O(n^2)

Repeated Evaluation

O(n^2)
O(n^2)

Tabulation

\displaystyle\sum_{i=1}^n i^2 = 1 + 4 + 9 + \dots + n^2= \frac{n(n-1)(2n-1)}{6}
= O(n^3)

Calculations

M(n)
M(1)...M(n)
M(1)...M(n)

Memory Issues

n
n^2

Space

4

16

16 Bytes

100

10 000

9 KB

1 000 000

1 000 000 000 000

931 GB

10 000

100 000 000

1 MB

100 000

10 000 000 000

9 GB

Time

<< .001sec

30 min

.002 sec

20 sec

<< .001sec

Memory

Bottleneck

M(4) = 9

M(5) = ?

M(11)

M(12)

+ number of new integers =

M(n)=\hspace{1.5cm}+\; M(n-1)
n-\delta(n)
n

Number of new distinct integers

Total integers in nth column

M(12)= \hspace{2.5cm}+\; M(11)
\delta(n)

Number of integers in M(n-1) table

number of new integers in 12th row

n-\delta(n)
\delta(n)
n
n-\delta(n)

number of integers in previous table

number of new integers in column

total number of integers in column

The number of distinct integers in a lattice bounded by the factorization of n.

clever algebra

M(12)= \hspace{2.5cm}+\; M(11)

number of new integers in 12th row

M(n)=n - \delta(n) + M(n-1)

*

It just so happens that this pattern appears for n=12.  This is generally more scattered.

*

\delta(12)

1     2      3      4      5     6

= 6

Bounded by n!

M(12) = 12-\delta(12) + M(11)
M(n)=n-\delta(n) + M(n-1)
= 12-6 + 53
= 59

Differences of M(n) Algorithm

Calculate the cumulative differences between M(n) and M(n-1) by calculating the delta function.

Time Complexity

Space Complexity

O(n \log n)
O(n)

Evaluation

O(n^2\log n)
O(n)

Tabulation

Calculating M(n)

and Optimizing M(n) Algorithms

Dave Purdum

Dr. Jonathan Webster

\delta(n)
\delta(n)
\delta(42)
= 20 + 5 =25

1            2    3     4                        5

Calculate 41 products

Calculate 6 products

No Free Rows

First Row Free

First Row Free Algorithm

Calculate products in the lattice greater than the largest value in the first row.

Time Complexity

Space Complexity

O(n \log n)
O(n)

Evaluation

O(n^2\log n)
O(n)

Tabulation

\delta(105)

Calculate 121 products

Calculate 55 products

No Free Rows

First Row Free

Calculate 15 products

Second Row Free

Getting Rows Free

Working in Modulo Classes

First Row Free

Second Row Free

"Third Row Free"

Modulo 1

Modulo 2

Modulo 6

Evens and Odds

0 mod 2    1 mod 2

Modulo n

Modulus by 2 and modulus by 3

Exploiting patterns in the first n rows

\vdots
Made with Slides.com