This is were Metapost comes into play. I found this a long time ago while searching for a tool to build nice graphics for my master thesis. Being used to LaTeX for typesetting math, it was a natural way to draw using code. I do not claim it is the best or the simplest way, but for me it works. Most importantly, it allows me to build high quality, vectorized graphics, which are memory efficient. The advantage of vector graphics is that you can zoom as much as you want and you’ll never see the pixels.

There are a bunch of places where you can learn Metapost. I’ll put here some references I use a lot. The easiest way to start drawing in Metapost is to take a look at some existing codes and modify them. You can find lots of examples in this tutorial. If you have a linux system, you can compile metapost .mp files to obtain high quality pdfs using the command mptopdf. If not, then you can use the online Metapost editor found here. In any case, here are some of my codes for some drawings in Metapost.

Here is the code for my website logo: If you want to see the lossless vectorized pdf click to see the following file: logo-0 You can see that it has a border which changes from one color to another. This was done using a loop and a parameter to vary the color as we go along the boundary.

prologues:=3; verbatimtex %&latex \documentclass{minimal} \begin{document} etex beginfig(0); % copy from here to use the online previewer u:=25; % 25 = 25bp = 25 PostScript points = 25/72 in wi:=10; % width in units u he:=7; % height in units u hoehe:=he*u; % height breite:=wi*u; %for i=0 upto he: %draw (0, i*u)--(breite, i*u) withcolor .7white; %endfor %for j=0 upto wi: %draw (j*u, 0)--(j*u, hoehe) withcolor .7white; %endfor; path p,q; p:=(6u,0.5u)--(6u,0)--(0,0)--(0,6u)--(6u,6u)--(6u,5.5u); pickup pensquare scaled 20; draw p; h=length(p); numeric c,d,detail; color a,b,co; a:=(1,0.45,0); b:=.2white; co:=.2white; detail:=500; for i=1 upto (detail/2): q := subpath (h*(i-1)/detail,h*i/detail) of p; draw q withcolor (2*i/detail)[a,b]; endfor; for i=detail downto (1+detail/2): q := subpath (h*(i-1)/detail,h*i/detail) of p; draw q withcolor (2*(detail-i)/detail)[a,b]; endfor; label ("B",(1/15)*(2.6u,2.7u)) scaled 15 withcolor co ; label ("2" infont defaultfont scaled 8,(4.9u,4.1u)) withcolor co; % end copy here for online previewer endfig; end;

Here’s another example of a figure where I needed a grid of disks aligned on top of another figure. Using loops in Metapost allowed me to get what I wanted:

For the high quality pdf click here: cioranescu-murat-0 and see the code below

prologues:=3; verbatimtex %&latex \documentclass{minimal} \begin{document} etex beginfig(0); u:=25; % 25 = 25bp = 25 PostScript points = 25/72 in wi:=10; % width in units u he:=7; % height in units u hoehe:=he*u; % height breite:=wi*u; path p,pa; p:=(0,4u)..(0,2u)..(2u,2u)..(3u,u)..(3u,0)..(6u,u)..(7u,3u)..(9u,5u)..cycle; pa:=(3u,5u)..(5u,6u)..(8u,6u)..(8u,3.8u)..(7.5u,3.4u)..(7u,3u)..cycle; draw p; fill p withcolor .8white; pair a,b; h=length(p); i=5; numeric c,d; c:=0.65*h; d:=0.7*h; a:= point c of p; b:= point d of p; pickup pencircle scaled 1.5; %draw a; %draw b; path q,r,s; q:= subpath (c,d) of p; %draw q withcolor red; %r:= b..((a+b)/2 shifted (.5(a-b) rotated -90))..a; %draw r; %fill buildcycle(r,q) withcolor .5red; %fill pa withcolor .9999blue; %label.rt(btex $\Omega$ etex,.5(u,3u)) scaled 2; draw (-u,9u)-- (10u,9u)--(10u,-2u)--(-u,-2u)--cycle; for i=0 upto 9: for j=-1 upto 8: draw fullcircle scaled 0.3u shifted (i*u, j*u); fill fullcircle scaled 0.3u shifted (i*u, j*u) withcolor white; endfor endfor; endfig; end;

There are many ways today to draw what you want. Metapost is one of the tools available, if you like coding.

]]>

**Problem 6.** We have that

Suppose for . Then for we have

This shows that . In the same way, having for leads to .

Now all we need to do is to apply the above procedure to inequalities of the form for and for , which come from the definition of the limit when . The case infinite is similar.

**Problem 7.** If all roots of are real then the sum of their square is non-negative. Suppose is a polynomial of degree . If we denote are the roots of then

If then the previous inequality translates to

which is equivalent to . Now if we compute the coefficients for we find something which for contradicts the previous inequality.

**Problem 8.** We start by observing that the eigenvalues of are and . Next, using the properties of block matrix determinants, we have the following equalities

The block determinant formula holds if is invertible. This happens for all but finitely many values of (the eigenvalues of ). Moreover, since the above equalities are polynomial equalities, this implies equality for every . Therefore, for each eigenvalue of (multiplicity accounted) we have and as eigenvalues for .

Therefore the eigenvalues of are

Note that a phenomenon similar to the Pascal triangle appears, which makes that adjacent eigenvalues of (which have a difference equal to ) with multiplicities and generate an eigenvalue of with multiplicity

**Problem 9.** Elementary techniques in ODE show that . We note that and . Therefore on . This implies, using a recurrence argument, that . This shows that exists for , but, for now, might be infinite.

In order to find an upper bound we may look what is the equation satisfied by an eventual limit of as . We arrive at the ODE . It is immediate to see that . Now we would like to prove that on , and for this we define . Using and the hypothesis we get . Using this we have

and . This implies that

This implies easily that on , which means that on . Thus the pointwise limit of exists. Let’s denote it by . Since using the monotone convergence theorem we have the convergence of integrals . Thus the limit satisfies

Therefore satisfies and which means that the limit is indeed for .

]]>

Prove that

**Problem 7.** Let be a nonconstant polynomial with real coefficients. For every positive integer let

Prove that there are only finitely many numbers such that all roots of are real.

**Problem 8.** Define the sequence of matrices by the following recurrence

where is the identity matrix.

Prove that has distinct integer eigenvalues with multiplicities , respectively.

**Problem 9.** Define the sequence of continuously differentiable functions by the following recurrence

Show that exists for every and determine the limit function.

**Problem 10.** Let be an equilateral triangle in the plane. Prove that for every there exists an with the following property: If is a positive integer and are non-overlapping triangles inside such that each of them is homothetic to with a negative ratio and

then

]]>

**Problem 2.** Let be a differentiable function and suppose that there exists a constant such that

for all . Prove that

holds for all .

**Problem 3.** For any positive integer denote by the product of positive divisors of . For every positive integer define the sequence

Determine whether for every set there exists a positive integer such that the following condition is satisfied:

*For every with the number is a perfect square if and only if .*

**Problem 4.** There are people in a city and each of them has exactly friends (friendship is mutual). Prove that it is possible to select a group of people such that at least persons in have exactly two friends in .

**Problem 5.** Let and be positive integers with and let

be a polynomial with complex coefficients such that

Prove that and have at most common roots.

]]>

- 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 , where is a positive integer, not necessarily a square… It is possible to enumerate all solutions to the following equation for fixed , simply by looping over and . This helps you find all lattice points on the circle of radius .

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.

I hope this will help you get towards the result.

]]>

]]>

for all

*Solution:* Note that we obviously have

and using the hypothesis we obtain that

for every . Now we have two options. Suppose the image of is unbounded. Then, since if we fix it would follow that has arbitrarily large divisor, which is not possible unless . This is one solution, as it can easily be checked.

The other alternative is that is bounded. If this is true, then there is a value of which is repeated for an infinite increasing sequence : . It follows that

for every and for defined as above. Since the fraction

is an integer and it converges to as it follows that for large enough we have . This implies that , and this is independent of . Therefore is constant. From the previous relation we have so the only possible constant is .

]]>

**Problem 2.** Consider an acute-angled triangle with and let be its circumscribed circle. Let and be the tangents to the circle at points and , respectively, and let be their intersection. The straight line passing through the point and parallel to intersects in point . The straight line passing through the point and parallel to intersects in point . The circumcircle of the triangle intersects in , where is located between and . The circumcircle of the triangle intersects the line (or its extension) in , where is located between and .

Prove that , , and are concurrent.

**Problem 3.** Let denote the set of positive integers. Find all functions such that

for all

**Problem 4.** On a circular table sit 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

]]>

*Mathematical Mind-Benders, Peter Winkler*

**Hint: **Work with remainders modulo 9.

]]>

Recently, I decided to try and see if I can do some more of these problems. I cannot say that I’ve acquired some new techniques between 2012-2016 concerning the mathematics involved in these problems. My research topics are usually quite different and my day to day programming routines are more about constructing new stuff which works fast enough than optimizing actual code. Nevertheless, I have some experience coding in Matlab, and I realized that nested loops are to be avoided. Vectorizing the code can speed up things 100 fold.

So the point of Project Euler tasks is making things go well for large numbers. Normally all problems are tested and should run within a minute on a regular machine. This brings us to choosing the right algorithms, the right simplifications and finding the complexity of the algorithms involved.

For example if you need to compute a certain sum what is the best way to proceed? Computing each in turn and summing them up in a loop gives us an algorithm. This means that computation time is roughly proportional to (this depends also on , but let’s suppose is something which we can compute in time, that is, instantly). Therefore, if is of order , this can be done on a regular machine. However, if , it may take, literally, ages for the algorithm to finish. For an algorithm of complexity , which scales with the square root of , should work. However let’s say you have in mind. Then either we find some algorithm with large enough or, even better, find a algorithm, which scales like . Roughly, if the algorithm works for , it should only take times more time to reach . Best of all, if you can find a direct formula for the sum, then you have an “instant” algorithm (whether this is instant or not depends on the formula you got…).

Maybe this could become more clear using some examples.

1. . The basic summation is time, but using the formula gives an algorithm. Basically, you can compute this sum for any you want by doing a couple of elementary operations.

2. Let’s complicate things a bit. What if is : the sum of all the divisors of ? First let’s note that computing alone is not immediately computable. One can imagine a basic algorithm, testing all integers if and summing them. If we know the factorization of we have a formula for in terms of prime factors and their powers. Prime factorization’s complexity is provided we have already made a sieve of primes (that’s other thing to think about). So we reach a complexity for . This is not enough if we want to sum things up to , for example. It turns out there’s a simple idea for a less complex algorithm.

We want to compute

Now what if we invert the sums? How do we do this? The summation indices satisfy . Thus, if we want to sum over instead of , then would be a multiple of all the time. So we find the alternative sum

where the last equality is valid since the number of multiples of less than is exactly . This already gives an complexity. Better than before, no factorizations involved, quite nice.

It turns out we can do even better. Looking at terms of the form we see that they take the same value multiple times. For example implies

Thus, summing on this interval we have

where the last sum has an explicit value since we sum consecutive terms of an arithmetic progression. Now we can switch to a summation over instead of . Note that we only need to go to high enough such that between and there is at least one integer. This is equivalent to so is . For larger than this value, we cannot guarantee the existence of an integer in our interval, so we’ll just compute the sums up to this and then sum the remaining terms which are again of order . This gives us a final algorithm.

Now, it is clear that if we need to compute our sum of sums of divisors up to , for example, only an algorithm like the last one can lead us to the result in reasonable time. Now you can solve Problem 401 in no time (replacing the sum of divisors with sum of squares doesn’t make much difference here, or does it?)

I hope this will motivate others to gain or regain interest in Project Euler problems. I solved over 120 problems in the last two months by learning to adapt algorithms and doing the necessary research when it is needed. Almost always, after solving a problem, you’ll find someone in the solution thread which has a better solution than yours. Look it up and continue improving your skills.

Project Euler problems don’t require only maths… Sometime more complex computer science stuff is needed. I learned the basics of dynamic programming and recursion solving some of the problems. I learned using lists, queues, combinatorial stuff, probabilities (still learning that though; lots of probability related stuff among PE problems). Not to mention that I learned to work quite well in some different languages, like Pari GP or Julia. It also forced me to compile and use some Java or Python codes.

It is a globally enriching quest: keep up with your maths, learn to do research online, improve programming skills, find new algorithms, choose right programming language to solve your problem efficiently, etc. It’s a hobby worth having.

]]>