Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips for Project Euler Problem #78

This is the problem in question: Problem #78

This is driving me crazy. I've been working on this for a few hours now and I've been able to reduce the complexity of finding the number of ways to stack n coins to O(n/2), but even with those improvements and starting from an n for which p(n) is close to one-million, I still can't reach the answer in under a minute. Not at all, actually.

Are there any hints that could help me with this?

Keep in mind that I don't want a full solution and there shouldn't be any functional solutions posted here, so as not to spoil the problem for other people. This is why I haven't included any code either.

like image 705
Marc Müller Avatar asked Nov 19 '09 18:11

Marc Müller


3 Answers

Wikipedia can help you here. I assume that the solution you already have is a recursion such as the one in the section "intermediate function". This can be used to find the solution to the Euler problem, but isn't fast.

A much better way is to use the recursion based on the pentagonal number theorem in the next section. The proof of this theorem isn't straight forward, so I don't think the authors of the problem expect that you come up with the theorem by yourself. Rather it is one of the problems, where they expect some literature search.

like image 92
Accipitridae Avatar answered Nov 12 '22 01:11

Accipitridae


This problem is really asking to find the first term in the sequence of integer partitions that’s divisible by 1,000,000.

A partition of an integer, n, is one way of describing how many ways the sum of positive integers, ≤ n, can be added together to equal n, regardless of order. The function p(n) is used to denote the number of partitions for n. Below we show our 5 “coins” as addends to evaluate 7 partitions, that is p(5)=7.

5 = 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1

We use a generating function to create the series until we find the required n. The generating function requires at most 500 so-called generalized pentagonal numbers, given by n(3n – 1)/2 with 0, ± 1, ± 2, ± 3…, the first few of which are 0, 1, 2, 5, 7, 12, 15, 22, 26, 35, … (Sloane’s A001318).

We have the following generating function which uses our pentagonal numbers as exponents:

1 - q - q^2 + q^5 + q^7 - q^12 - q^15 + q^22 + q^26 + ...

my blog at blog.dreamshire.com has a perl program that solves this in under 10 sec.

like image 3
Mike Avatar answered Nov 12 '22 01:11

Mike


Have you done problems 31 or 76 yet? They form a nice set that is an generalization of the same base problem each time. Doing the easier questions may give you insight into a solution for 78.

like image 2
Vic E Avatar answered Nov 12 '22 01:11

Vic E