Solving the a7 + b7 = c7 + d7 Equation Using Heap Algorithm

  • Thread starter NATURE.M
  • Start date
  • Tags
    Algorithm
In summary, the algorithm has worst case runtime of O(n2 log n), and uses a heap of at most n elements.
  • #1
NATURE.M
301
0

Homework Statement


We have four integers a,b,c,d each of which are >= 1 and <= n, and are asked to write an algorithm to find all possible solutions to the following equation: a7 + b7 = c7 + d 7

The algorithm should use a heap of at most n elements, and should have worst case runtime of O(n2 log n).

The Attempt at a Solution


Some basic math will tell us that there are n4 possible combinations of a,b,c,d for some input n. So if we go through each possible combination it would be very costly. This is where the heap is suppose to come into play. But I'm having particular difficulty determining how we would actually use it for this question.
 
Physics news on Phys.org
  • #2
It is certainly useful to have some way to quickly look up "is x the seventh power of some integer?". I see an implementation with O(n3 log n), needs some improvement.
 
  • #3
mfb said:
It is certainly useful to have some way to quickly look up "is x the seventh power of some integer?". I see an implementation with O(n3 log n), needs some improvement.

Right now I'm thinking the only possible solutions are when a=c, b=d or when a=d, b=c. So this is what I have, but it doesn't use a heap. I'm still not entirely sure what I would want to store in the heap.

for (i : 1,...,n)
...for(j : 1,...,n)
...insert into array (i, j, i, j)
...if (i != j)
...insert into array (i, j, j, i)

The two for loops I believe are required regardless and give O(n2). I think this accounts for the n2 term in the desired complexity. The insertion into the array takes O(# of elements) in the worst case. However, this is not correct. I'm just not sure how to use a heap here.
 
  • #4
There's enough memory on a typical PC to solve this brute force.

Since 566^7 > 2^64, assuming you're using 64 bit unsigned integers (unsigned long long), then you could create an array of size 566 where array[ i ] = i^7 (I assume [0] is not used, so the array only contains 565 entries for 1^7 to 565^7). So now the problem is reduced to finding all combinations of array[ a ] + array[ b ] = array[ c ] + array[ d ]. An array of structures, size 320,356 (566^2) could contain the sums of all combinations of table[ x ] + table [ y ] and x and y, taking O(n^2). The table could be sorted using a counting / radix sort in 4 or 8 passes (sorting 64 bit values 16 bits or 8 bits at a time), independent of n, but since the value size of 64 bits depends on the maximum value of n, this could be considered O(n^2 log(maximum n)) time complexity.

It's not clear to me what is supposed to be stored in the heap.
 
Last edited:
  • #5
NATURE.M said:
Right now I'm thinking the only possible solutions are when a=c, b=d or when a=d, b=c.
That would be easy, but I don't think it is true, and you would have to prove it.
rcgldr said:
There's enough memory on a typical PC to solve this brute force.
There is no reason to limit N to 565. If you use unsigned long long it's your own fault that the algorithm cannot handle larger numbers.

Also, you are not allowed to use a data structure with n2 entries. That would allow an O(n2 log n) algorithm easily, but it is not allowed. Sorting this would still need at least O(n2) as you have n2 entries, but I guess that factor 8 is actually the logarithm, so you have O(n2 log n) sorting steps. Fine. But the array is too large.
 
  • #6
mfb said:
Also, you are not allowed to use a data structure with n2 entries. That would allow an O(n2 log n) algorithm easily, but it is not allowed. Sorting this would still need at least O(n2) as you have n2 entries, but I guess that factor 8 is actually the logarithm
Indirectly, the factor 8 is based on sorting 64 bit values 8 bits at a time. The 64 bit values could be sorted in 4 passes 16 bits at a time. The number of passes is based on the value size, independent of n, but since the value size depends on the maximum possible value of n, the number of passes = O(log((maximum n)^2)) = O(log(maximum n)) (the 2 in O(2 log(maximum n)) drops out with O() notation). So the sort would take O(n^2 log(maximum n)), if the integer size was a function of n, as opposed to limiting the maximum value of n. If the integer size is fixed in the algorithm, limiting the maximum value of n, then the time complexity is O(n^2).

Memory usage would be around 10 MB, but on most PC's, 1 GB of memory is usually available.

It wasn't stated what type heap would be used, perhaps a binary heap that is balanced as much as possible?
 
Last edited:
  • #7
The sorting algorithm has to be able to handle arbitrary lengths.
You have n2 values from 1 to n7 to sort, you cannot sort them in linear time. Your description sounds like bucket sort, that is still O(m log m) where m=n2 is the number of entries as you cannot make n7 buckets.

Anyway, that sub-discussion is pointless because we cannot use a data structure with n2 entries.
 
  • #8
mfb said:
Your description sounds like bucket sort, that is still O(m log m) where m=n2 is the number of entries as you cannot make n7 buckets.
It's a multi-pass bucket sort, but counts are generated for the bucket sizes (in this case a 8 x 256 matrix of counts converted into offsets), so only a temporary array of the same size as the original array is needed, in this case, n^2 x sizeof(structure containing 64 bit n^7 sum, and two 16 or 32 bit values for n0 and n1). Wiki articles:

http://en.wikipedia.org/wiki/Counting_sort

http://en.wikipedia.org/wiki/Radix_sort

mfb said:
Anyway, that sub-discussion is pointless because we cannot use a data structure with n2 entries.
It could be used to confirm the results of a heap based algorithm.
 
Last edited:
  • #9
If you scale that up to arbitrary length, you still get the factor of log n for the number of passes.
rcgldr said:
It could be used to confirm the results of a heap based algorithm.
Yes, but it does not help solving the original problem.
 
  • #10
I think there is an issue with the original question (maybe it should have been 4th power, not 7th power?). This is a Diophantine Equation (integer equation) for 7th powers with 2 terms on each side of the equation, described as 7.2.2 (7th power, 2 terms on one side of the equation, 2 terms on the other side). As noted in this document, there are no known solutions for 7.2.2 but there are solutions for 7.4.4 (4 terms on each side of the equation):

http://mathworld.wolfram.com/DiophantineEquation7thPowers.html

For an example of what was involved to find any 7.4.4 solution:

http://www.ams.org/journals/mcom/1996-65-216/S0025-5718-96-00768-5/S0025-5718-96-00768-5.pdf

There are solutions for 2.2.2, 3.2.2, and 4.2.2, but no known solutions for 5.2.2, 6.2.2, 7.2.2, 8.2,2, 9.2.2, 10.2.2.
 
Last edited:
  • Like
Likes mfb
  • #11
rcgldr said:
I think there is an issue with the original question (maybe it should have been 4th power, not 7th power?). This is a Diophantine Equation (integer equation) for 7th powers with 2 terms on each side of the equation, described as 7.2.2 (7th power, 2 terms on one side of the equation, 2 terms on the other side). As noted in this document, there are no known solutions for 7.2.2 but there are solutions for 7.4.4 (4 terms on each side of the equation):

http://mathworld.wolfram.com/DiophantineEquation7thPowers.html

For an example of what was involved to find any 7.4.4 solution:

http://www.ams.org/journals/mcom/1996-65-216/S0025-5718-96-00768-5/S0025-5718-96-00768-5.pdf

There are solutions for 2.2.2, 3.2.2, and 4.2.2, but no known solutions for 5.2.2, 6.2.2, 7.2.2, 8.2,2, 9.2.2, 10.2.2.

Interesting, well this would confirm that there are only trivial solutions that exist to the original equation. But the original problem is as stated, that is 7.2.2
 
  • #12
The first link posted above is part of a series that starts here:

http://mathworld.wolfram.com/topics/DiophantineEquations.html

For forth powers, 4.2.2 solutions exist. The article mentions that "Parametric solutions to the 4.2.2 equation are known ... but no "general" solution is known", and that "general (but incomplete) solutions are given by ..."

http://mathworld.wolfram.com/DiophantineEquation4thPowers.html

So where did the question about seventh powers (7.2.2) originate, and if not a mistake, I'm wondering if the originator was aware of the fact that there are no known solutions to 7.2.2?
 
  • #13
Well, you can still search for them even if no solutions are known. Actually, that's the point of "no solutions are known": someone searched for them. Probably with an efficient algorithm.
 
  • #14
mfb said:
Well, you can still search for them even if no solutions are known. Actually, that's the point of "no solutions are known": someone searched for them. Probably with an efficient algorithm.
If you look at the details for the 7.4.4 case, it's a somewhat optimized brute force approach, in that case using an AVL tree. I think once at 4.x.x or beyond, to get all solutions, there are no elegant algorithms, and there's no way to prove non-existance of solutions for cases like 7.2.2, so all that can be stated is that there are no known solutions.
 
  • #15
At least there is no known way to prove it.
rcgldr said:
so all that can be stated is that there are no known solutions.
Sure, but you still want an efficient algorithm for that. As efficient as possible.
 

FAQ: Solving the a7 + b7 = c7 + d7 Equation Using Heap Algorithm

1. What is a heap?

A heap is a data structure that is used to store and manage data in a specific order. It is a binary tree structure where the parent node is always larger (or smaller) than its child nodes, depending on whether it is a max heap or a min heap. This allows for efficient insertion, deletion, and retrieval of the data elements.

2. How do you solve an algorithm using a heap?

To solve an algorithm using a heap, you first need to build the heap by inserting the data elements into the heap in the correct order. Then, depending on the specific algorithm, you may need to perform operations such as heapify or heap sort to rearrange the elements in the heap. Finally, you can retrieve the data elements from the heap in the desired order.

3. What is the time complexity of solving an algorithm using a heap?

The time complexity of solving an algorithm using a heap depends on the specific operation being performed. For building a heap, the time complexity is O(n), where n is the number of elements in the heap. For other operations such as insertion, deletion, and retrieval, the time complexity is O(log n), where n is the number of elements in the heap.

4. What are the advantages of using a heap to solve algorithms?

Using a heap to solve algorithms has several advantages. Firstly, it allows for efficient insertion, deletion, and retrieval of data elements in logarithmic time. This makes it a suitable data structure for solving problems that involve large datasets. Additionally, heaps are also useful for sorting data in a specific order, such as finding the largest or smallest elements in a dataset.

5. What are some common applications of using a heap to solve algorithms?

Heaps are commonly used in various applications such as priority queues, sorting algorithms, and graph algorithms. They are also used in operating systems for managing memory allocation and in network routing protocols. Additionally, heaps are useful in solving problems related to scheduling, task assignment, and data compression.

Back
Top