Project Euler/501: Difference between revisions
From charlesreid1
| Line 3: | Line 3: | ||
This was an extremely interesting problem. When I first read through the problem, the solution came to me almost immediately - if we want to know how many divisors a number has, it relates to its prime factorization, and if we want a number with eight divisors, we want a number whose prime factors can be rearranged in exactly 4 distinct ways (leading to 8 pairings, or 8 factors). | This was an extremely interesting problem. When I first read through the problem, the solution came to me almost immediately - if we want to know how many divisors a number has, it relates to its prime factorization, and if we want a number with eight divisors, we want a number whose prime factors can be rearranged in exactly 4 distinct ways (leading to 8 pairings, or 8 factors). | ||
This is confirmed by examining the examples the problem gives. Several integers that are below 100 and have exactly 8 factors were given. The prime factorizations of each number followed certain patterns. | This is confirmed by examining the examples the problem gives. Several integers that are below 100 and have exactly 8 factors were given. The prime factorizations of each number followed certain patterns. These patterns are: | ||
<math> | <math> | ||
p_1 p_2 p_3 | p_1 p_2 p_3 | ||
</math> | </math> | ||
and | |||
<math> | <math> | ||
| Line 13: | Line 15: | ||
</math> | </math> | ||
Why these patterns? It's because these are the two kinds of groupings of objects that lead to exactly four ways of partitioning the objects. The first is if we have 3 distinct objects, which can be grouped in 4 distinct ways: | |||
<pre> | |||
1 2 3 | 4 | |||
1 2 | 3 4 | |||
1 | 2 3 4 | |||
| 1 2 3 4 | |||
</pre> | |||
and if we have 3 identical items and one unique item, these can also be arranged in exactly four ways: | |||
<pre> | |||
o o o O | | |||
o o o | O | |||
o o | o O | |||
o | o o O | |||
</pre> | |||
The problem statement gives two cases where this happens, and leaves you stumped when your code gives you 179 numbers with exactly 8 divisors, instead of the information given in the problem, which is 180. I struggled over this for a while. I thought it might be a bug in my code, but typically these kinds of bugs are symmetric in some way, so I would be leaving out two or more numbers with 8 divisors. | The problem statement gives two cases where this happens, and leaves you stumped when your code gives you 179 numbers with exactly 8 divisors, instead of the information given in the problem, which is 180. I struggled over this for a while. I thought it might be a bug in my code, but typically these kinds of bugs are symmetric in some way, so I would be leaving out two or more numbers with 8 divisors. | ||
| Line 20: | Line 38: | ||
<math> | <math> | ||
p_1 p_2 p_3 | |||
p_1 p_2 p_3 | </math> | ||
and | |||
<math> | |||
p_A^3 p_B | p_A^3 p_B | ||
</math> | </math> | ||
Revision as of 00:09, 9 July 2017
Breakdown
This was an extremely interesting problem. When I first read through the problem, the solution came to me almost immediately - if we want to know how many divisors a number has, it relates to its prime factorization, and if we want a number with eight divisors, we want a number whose prime factors can be rearranged in exactly 4 distinct ways (leading to 8 pairings, or 8 factors).
This is confirmed by examining the examples the problem gives. Several integers that are below 100 and have exactly 8 factors were given. The prime factorizations of each number followed certain patterns. These patterns are:
$ p_1 p_2 p_3 $
and
$ p_A^3 p_B $
Why these patterns? It's because these are the two kinds of groupings of objects that lead to exactly four ways of partitioning the objects. The first is if we have 3 distinct objects, which can be grouped in 4 distinct ways:
1 2 3 | 4 1 2 | 3 4 1 | 2 3 4 | 1 2 3 4
and if we have 3 identical items and one unique item, these can also be arranged in exactly four ways:
o o o O | o o o | O o o | o O o | o o O
The problem statement gives two cases where this happens, and leaves you stumped when your code gives you 179 numbers with exactly 8 divisors, instead of the information given in the problem, which is 180. I struggled over this for a while. I thought it might be a bug in my code, but typically these kinds of bugs are symmetric in some way, so I would be leaving out two or more numbers with 8 divisors.
The next thing I checked was the form of the numbers I was generating, which was either the product of 3 distinct primes:
$ p_1 p_2 p_3 $
and
$ p_A^3 p_B $
I was generating every combination of these numbers for every prime below the maximum, in this case 1000, and things seemed to be working out okay, except that I was missing one. When I ran it on the case of a 1000000 maximum, I was off from the information given in the problem by four. The challenge, of course, is that when I spot checked my list of 179 numbers, all of them had exactly 8 divisors - it wasn't that I was generating too many numbers. It was that some number not included on my list had 8 divisors, and I had to find it.
This was a cryptic problem. I tried investigating whether the primes I was using could, in fact, be composite numbers. So, if I substitute 4 for pA and 6 for pB, does that result in a number with 8 factors? The answer, of course, is no. These definitely need to be primes. What if pA and pB, or p1 and p2 and p3, are not distinct primes? This led to more numbers with fewer or more than 8 factors.
I tried pA = 2 and pB = 2, which led to 2^4 or 16, which did not have 8 factors. I tried p1 = 4 and p2 = 2 and p3 = 4, totaling 2^5 or 32, which also did not have 8 factors. Then I tried pA = 4 and pB = 2, for a total of 2^7 or 128, and to my surprise, 128 had 8 factors. Here was the missing prime! 128 was not in the list of primes taking the two forms given above. However, the procedure for finding 2^7 and the rule for it are a bit different. The rule relates to how we can use the prime numbers to construct new numbers, as we saw in Problem 500, the prior problem.
Generating composites from primes
Let's go back to that problem for a moment. There, we were trying to construct numbers with a specified number of divisors. To do that, we constructed a table with the prime numbers, with columns arranged in a particular way: n, n^2, n^4, n^8, and so on. Then, to construct new numbers, we found new combinations of numbers from each column. The reason we have only powers of 2 for these primes is, any number can be expressed as the sum of powers of 2 - i.e., can be represented in binary. if we had an odd power of n, say n^5, we could write in terms of powers of 2 like: (n)(n^4). Likewise for n^13, we could write it as (n)(n^4)(n^8).
That is, any integer power of n can be written as a binary number, and that binary number yields the order of selection of the primes from the table.
Generating composites with 8 factors
Back to our problem at hand. We are looking for a third combination of prime numbers that will give us numbers with 8 factors. If, instead of using primes in the above expressions, we instead use even powers of primes, we get a whole new class of numbers with 8 divisors, which do not show up until at least 128.
For example, a number with 8 factors can be formed by letting the primes be distinct powers of the prime 2:
$ \begin{align} p_1 &=& 2 \\ p_2 &=& 2^2 \\ p_3 &=& 2^4 \end{align} $
That forms the candidate number:
$ c = 2^{4+2+1} = 128 $
which, indeed, has 8 factors:
We were only off by 1 (our program predicted 179 numbers with 8 factors below 1,000, instead of the problem statement's 180) below 1,000 because the next such candidate number is:
$ c = 3^{4+2+1} = 2187 $
which has 8 factors:
and here are the values of the seventh power of the first few primes:
2 128 3 2187 5 78125 7 823543 11 19487171 13 62748517 17 410338673 19 893871739
which all have 8 factors: