### Archive

Archive for the ‘Number theory’ Category

Problem 1. A quadrilateral ${ABCD}$ is inscribed in a circle ${k}$, where ${AB>CD}$ and ${AB}$ is not parallel to ${CD}$. Point ${M}$ is the intersection of the diagonals ${AC}$ and ${BD}$ and the perpendicular from ${M}$ to ${AB}$ intersects the segment ${AB}$ at the point ${E}$. If ${EM}$ bisects the angle ${CED}$, prove that ${AB}$ is a diameter of the circle ${k}$.

Problem 2. Let ${q}$ be a positive rational number. Two ants are initially at the same point ${X}$ in the plane. In the ${n}$-th minute ${(n=1,2,...)}$ each of them chooses whether to walk due north, east, south or west and then walks the distance of ${q^n}$ meters. After a whole number of minutes, they are at the same point in the plane (non necessarily ${X}$), but have not taken exactly the same route within that time. Determine all the possible values of ${q}$.

Problem 3. Alice and Bob play the following game: They start with two non-empty piles of coins. Taking turns, with Alice playing first, each player chooses a pile with an even number of coins and moves half of the coins of this pile to the other pile. The came ends if a player cannot move, in which case the other player wins.

Determine all pairs ${(a,b)}$ of positive integers such that if initially the two piles have ${a}$ and ${b}$ coins, respectively, then Bob has a winning strategy.

Problem 4. Find all primes ${p}$ and ${q}$ such that ${3p^{q-1}+1}$ divides ${11^q+17^p}$.

## Sum of the Euler Totient function

Given a positive integer ${n}$, the Euler totient function ${\varphi(n)}$ is defined as the number of positive integers less than ${n}$ which are co-prime with ${n}$ (i.e. they have no common factors with ${n}$). There are formulas for computing ${\varphi(n)}$ starting from the factorization of ${n}$. One such formula is

$\displaystyle \varphi(n) = n \prod_{p|n} \left(1-\frac{1}{p}\right),$

where the product is made over all primes dividing ${n}$.

If you have to compute ${\varphi(n)}$ for all numbers less than a threshold then another property could be useful: ${\varphi}$ is arithmetic, that is, ${\varphi(mn) = \varphi(m)\varphi(n)}$ whenever ${\gcd(m,n)=1}$. Therefore you could store all values computed until ${k}$ and for computing the value ${\varphi(k+1)}$ there are two possibilities: ${k+1=p^\alpha}$ is a prime power and then ${\varphi(k+1) = p^{\alpha}-p^{\alpha-1}}$ or ${k+1}$ is composite and ${k+1 = mn}$ with ${m,n\leq k,\ \gcd(m,n)=1}$. Then use the stored values to compute ${\varphi(k+1)=\varphi(m)\varphi(n)}$.

I now come to the main point of this post: computing the sum of all values of the totient function up to a certain ${N}$:

$\displaystyle \text{Compute } S(N) = \sum_{i=1}^N \varphi(i).$

One approach is to compute each ${\varphi(i)}$ and sum them. I will call this the brute-force approach. For all numerical purposes I will use Pari-GP in this post. On my computer it takes less than a second to compute ${S(10^6)}$ and about ${12}$ seconds to compute ${S(10^7)}$. This is super linear in time, since the algorithm computes the factorization for each ${n}$ and then sums the values. Using the sieve approach could improve the timing a bit, but the algorithm is still super linear.

In some Project Euler problems it is not uncommon to have to compute something like ${S(10^{11})}$ or even larger. Therefore, there must be more efficient ways to compute ${S(N)}$ out there, so let’s study some of the properties of ${S(N)}$. In another post I dealt with the acceleration of the computation of the sum of the divisor function.

We have ${S(N) = \sum_{i=1}^N \varphi(i)}$ which is the number of pairs ${(a,b)}$ with ${1\leq a\leq b \leq N}$ such that ${\gcd(a,b) =1}$. It is not difficult to see that the total number of such pairs is ${n(n+1)/2}$. Moreover, the possible values of ${\gcd(a,b)}$ are ${1,2,...,N}$. Now, if for ${m \leq N}$ we search instead for pairs satisfying ${\gcd(a,b)=m}$ then we have ${a = ma',\ b = mb'}$ with ${\gcd(a',b')=1}$ and we get

$\displaystyle 1 \leq a' \leq b' \leq N/m,\ \gcd(a',b')=1.$

There fore the number of pairs with gcd equal to ${m}$ is ${S(\lfloor N/m\rfloor )}$. Now we arrive at an interesting recursive formula:

$\displaystyle S(N) = \frac{n(n+1)}{2} - \sum_{m=2}^N S(\lfloor N/m \rfloor ).$

At a first sight this looks more complicated, but there is a trick to keep in mind whenever you see a summation over ${m}$ of terms of the form ${\lfloor N/m \rfloor}$: these quantities are constant on large intervals. Indeed,

$\displaystyle \lfloor N/m \rfloor = d \Leftrightarrow md \leq N < m(d+1)\Leftrightarrow N/(d+1)

Therefore we can change the index of summation from ${m}$ to ${d=\lfloor N/m \rfloor}$. The range of ${d}$ for which the interval ${I_d = [N/(d+1),N/d]}$ contains more than one integer is of order ${\sqrt(N)}$. Indeed, ${N/d-N/(d+1) = N/(d(d+1))}$. Therefore for ${d\leq \sqrt(N)}$ we should have at least one integer in the interval ${I_d}$. The part where ${d}$ is larger than ${\sqrt{N}}$ corresponds to ${m}$ smaller than ${\sqrt{N}}$. Therefore, we can split ${S(N)}$ into two sums, each of order ${\sqrt{N}}$. and get that

$\displaystyle S(N) = \frac{n(n+1)}{2}- \sum_{m=2}^{\sqrt{N}} S(\lfloor N/m \rfloor)-\sum_{d=1}^{\sqrt{N}}\left(\lfloor N/d \rfloor-\lfloor N/(d+1)\rfloor\right)S(d),$

where in the last sum we must make sure that ${d \neq \lfloor N/d \rfloor}$ in order to avoid duplicating terms in the sum.

Therefore we replaced a sum until ${N}$ to two sums with upper bound ${\sqrt{N}}$. The complexity is not ${\sqrt{N}}$, but something like ${N^{2/3}}$ since we have a recursive computation. Nevertheless, with this new formula and using memoization, to keep track of the values of ${S}$ already computed, we can compute ${S(N)}$ very fast:

${S(10^6) = 303963552392}$ is computed instantly (vs ${1}$ second with brute force)

${S(10^7) = 30396356427242}$ takes ${1}$ second (vs ${12}$ seconds with brute force)

${S(10^8)}$ takes ${5}$ seconds (vs over ${3}$ minutes with brute force)

${S(10^9)}$ takes ${30}$ seconds

${S(10^{11})}$ takes about ${12}$ minutes

etc. Recall that these computations are done in Pari GP, which is not too fast. If you use C++ you can compute ${S(10^8)}$ in ${0.2}$ seconds, ${S(10^9)}$ in ${1}$ second and ${S(10^{10})}$ in ${6}$ seconds and ${S(10^{11})}$ in under a minute, if you manage to get past overflow errors.

Categories: Number theory, Programming

## Romanian Masters in Mathematics contest – 2018

Problem 1. Let ${ABCD}$ be a cyclic quadrilateral an let ${P}$ be a point on the side ${AB.}$ The diagonals ${AC}$ meets the segments ${DP}$ at ${Q.}$ The line through ${P}$ parallel to ${CD}$ mmets the extension of the side ${CB}$ beyond ${B}$ at ${K.}$ The line through ${Q}$ parallel to ${BD}$ meets the extension of the side ${CB}$ beyond ${B}$ at ${L.}$ Prove that the circumcircles of the triangles ${BKP}$ and ${CLQ}$ are tangent .

Problem 2. Determine whether there exist non-constant polynomials ${P(x)}$ and ${Q(x)}$ with real coefficients satisfying

$\displaystyle P(x)^{10}+P(x)^9 = Q(x)^{21}+Q(x)^{20}.$

Problem 3. Ann and Bob play a game on the edges of an infinite square grid, playing in turns. Ann plays the first move. A move consists of orienting any edge that has not yet been given an orientation. Bob wins if at any point a cycle has been created. Does Bob have a winning strategy?

Problem 4. Let ${a,b,c,d}$ be positive integers such that ${ad \neq bc}$ and ${gcd(a,b,c,d)=1}$. Let ${S}$ be the set of values attained by ${\gcd(an+b,cn+d)}$ as ${n}$ runs through the positive integers. Show that ${S}$ is the set of all positive divisors of some positive integer.

Problem 5. Let ${n}$ be positive integer and fix ${2n}$ distinct points on a circle. Determine the number of ways to connect the points with ${n}$ arrows (oriented line segments) such that all of the following conditions hold:

• each of the ${2n}$ points is a startpoint or endpoint of an arrow;
• no two arrows intersect;
• there are no two arrows ${\overrightarrow{AB}}$ and ${\overrightarrow{CD}}$ such that ${A}$, ${B}$, ${C}$ and ${D}$ appear in clockwise order around the circle (not necessarily consecutively).

Problem 6. Fix a circle ${\Gamma}$, a line ${\ell}$ to tangent ${\Gamma}$, and another circle ${\Omega}$ disjoint from ${\ell}$ such that ${\Gamma}$ and ${\Omega}$ lie on opposite sides of ${\ell}$. The tangents to ${\Gamma}$ from a variable point ${X}$ on ${\Omega}$ meet ${\ell}$ at ${Y}$ and ${Z}$. Prove that, as ${X}$ varies over ${\Omega}$, the circumcircle of ${XYZ}$ is tangent to two fixed circles.

Source: Art of Problem Solving forums

Some quick ideas: For Problem 1 just consider the intersection of the circle ${(BKP)}$ with the circle ${(ABCD)}$. You’ll notice immediately that this point belongs to the circle ${(CLQ)}$. Furthermore, there is a common tangent to the two circles at this point.

For Problem 2 we have ${10\deg P = 21 \deg Q}$. Eliminate the highest order term from both sides and look at the next one to get a contradiction.

Problem 4 becomes easy after noticing that if ${q}$ divides ${an+b}$ and ${cn+d}$ then ${q}$ divides ${ad-bc}$.

In Problem 5 try to prove that the choice of start points determines that of the endpoints. Then you have a simple combinatorial proof.

Problem 6 is interesting and official solutions use inversions. Those are quite nice, but it may be worthwhile to understand what happens in the non-inverted configuration.

I will come back to some of these problems in some future posts.

## Project Euler – Problem 264

Today I managed to solve problem 264 from Project Euler. This is my highest rating problem until now: 85%. You can click the link for the full text of the problem. The main idea is to find all triangles ABC with vertices having integer coordinates such that

• the circumcenter O of each of the triangles is the origin
• the orthocenter H (the intersection of the heights) is the point of coordinates (0,5)
• the perimeter is lower than a certain bound

I will not give detailed advice or codes. You can already find a program online for this problem (I won’t tell you where) and it can serve to verify the final code, before going for the final result. Anyway, following the hints below may help you get to a solution.

The initial idea has to do with a geometric relation linking the points A, B, C, O and H. Anyone who did some problems with vectors and triangles should have come across the needed relation at some time. If not, just search for important lines in triangles, especially the line passing through O and H (and another important point).

Once you find this vectorial relation, it is possible to translate it in coordinates. The fact that points A, B, C are on a circle centered in O shows that their coordinates satisfy an equation of the form $x^2+y^2=n$, where $n$ is a positive integer, not necessarily a square… It is possible to enumerate all solutions to the following equation for fixed $n$, simply by looping over $x$ and $y$. This helps you find all lattice points on the circle of radius $\sqrt{n}$.

Once these lattice points are found one needs to check the orthocenter condition. The relations are pretty simple and in the end we have two conditions to check for the sum of the x and y coordinates. The testing procedure is a triple loop. We initially have a list of points on a circle, from the previous step. We loop over them such that we dont count triangles twice: i from 1 to m, j from i+1 to m, k from j+1 to m, etc. Once a suitable solution is found, we compute the perimeter using the classical distance formula between two points given in coordinates. Once the perimeter is computed we add it to the total.

Since the triple loop has cubic complexity, one could turn it in a double loop. Loop over pairs and construct the third point using the orthocenter condition. Then just check if the point is also on the circle. I didn’t manage to make this double loop without overcounting things, so I use it as a test: use double loops to check every family of points on a given circle. If you find something then use a triple loop to count it properly. It turns out that cases where the triple loop is needed are quite rare.

So now you have the ingredients to check if on a circle of given radius there are triangles with the desired properties. Now we just iterate over the square of the radius. The problem is to find the proper upper bound for this radius in order to get all the triangles with perimeter below the bound. It turns out that a simple observation can get you close to a near optimal bound. Since in the end the radii get really large and the size of the triangles gets really large, the segment OH becomes small, being of fixed length 5. When OH is very small, the triangle is almost equilateral. Just use the upper bound for the radius for an equilateral triangle of perimeter equal to the upper bound of 100000 given in the problem.

Using these ideas you can build a bruteforce algorithm. Plotting the values of the radii which give valid triangles will help you find that you only need to loop over a small part of the radii values. Factoring these values will help you reduce even more the search space. I managed to  solve the problem in about 5 hours in Pari GP. This means things could be improved. However, having an algorithm which can give the result in “reasonable” time is fine by me.

## Balkan Mathematical Olympiad 2017 – Problems

Problem 1. Find all ordered pairs of positive integers ${ (x, y)}$ such that:

$\displaystyle x^3+y^3=x^2+42xy+y^2.$

Problem 2. Consider an acute-angled triangle ${ABC}$ with ${AB and let ${\omega}$ be its circumscribed circle. Let ${t_B}$ and ${t_C}$ be the tangents to the circle ${\omega}$ at points ${B}$ and ${C}$, respectively, and let ${L}$ be their intersection. The straight line passing through the point ${B}$ and parallel to ${AC}$ intersects ${t_C}$ in point ${D}$. The straight line passing through the point ${C}$ and parallel to ${AB}$ intersects ${t_B}$ in point ${E}$. The circumcircle of the triangle ${BDC}$ intersects ${AC}$ in ${T}$, where ${T}$ is located between ${A}$ and ${C}$. The circumcircle of the triangle ${BEC}$ intersects the line ${AB}$ (or its extension) in ${S}$, where ${B}$ is located between ${S}$ and ${A}$.

Prove that ${ST}$, ${AL}$, and ${BC}$ are concurrent.

Problem 3. Let ${\mathbb{N}}$ denote the set of positive integers. Find all functions ${f:\mathbb{N}\longrightarrow\mathbb{N}}$ such that

$\displaystyle n+f(m)\mid f(n)+nf(m)$

for all ${m,n\in \mathbb{N}}$

Problem 4. On a circular table sit ${\displaystyle {n> 2}}$ students. First, each student has just one candy. At each step, each student chooses one of the following actions:

• (A) Gives a candy to the student sitting on his left or to the student sitting on his right.
• (B) Separates all its candies in two, possibly empty, sets and gives one set to the student sitting on his left and the other to the student sitting on his right.

At each step, students perform the actions they have chosen at the same time. A distribution of candy is called legitimate if it can occur after a finite number of steps. Find the number of legitimate distributions.

(Two distributions are different if there is a student who has a different number of candy in each of these distributions.)

Source: AoPS

## Missing digit – short puzzle

The number $2^{29}$ has $9$ digits, all different; which digit is missing?

Mathematical Mind-Benders, Peter Winkler

## Monotonic bijection from naturals to pairs of natural numbers

This is a cute problem I found this evening.

Suppose ${\phi : \Bbb{N}^* \rightarrow \Bbb{N}^*\times \Bbb{N}^*}$ is a bijection such that if ${\phi(k) = (i,j),\ \phi(k')=(i',j')}$ and ${k \leq k'}$, then ${ij \leq i'j'}$.

Prove that if ${k = \phi(i,j)}$ then ${k \geq ij}$.

Proof: The trick is to divide the pairs of positive integers into families with the same product.

$\displaystyle \begin{matrix} (1,1) & (1,2) & (1,3) & (1,4) & (1,5) & (1,6) & \cdots \\ & (2,1) & (3,1) & (2,2) & (5,1) & (2,3) & \cdots \\ & & &( 4,1) & & (3,2) & \cdots \\ & & & & & (6,1) & \cdots \end{matrix}$

Note that the ${M}$-th column contains as many elements as the number of divisors of ${M}$. Now we just just use a simple observation. Let ${\phi(k)=(i,j)}$ be on the ${M}$-th column (i.e. ${ij = M}$). If ${n \geq 1}$ then ${\phi(k+n)=(i',j')}$ cannot be on one of the first ${M-1}$ columns. Indeed, the monotonicity property implies ${M = ij \leq i'j'}$. The fact that ${\phi}$ is a bijection assures us that ${\phi(1),...,\phi(k)}$ cover the first ${M-1}$ columns. Moreover, one element from the ${M}$-th column is surely covered, namely ${(i,j) = \phi(k)}$. This means that

$\displaystyle k \geq d(1)+...+d(M-1)+1 \geq M = ij,$

where we have denoted by ${d(n)}$ the number of positive divisors of ${n}$.