Think of a new solution, and find a pair of less than 2 billionCubeIt takes only one second.
Recently I saw several articles in the garden about this dual-cubeArticleI am also very interested in finding a goodAlgorithm.
Subject requirement: the number of double supercubes is a positive integer that can be split into two different a ^ 3 + B ^ 3 methods, where, B is an integer and 0 <A <= B. For any specified int N, the number of all supercubes less than or equal to N is returned.
The algorithm we first came up with was as follows:
1. Find all the cubes that are less than the given maximum integer N, that is, y that satisfies y = x * X (X, Y is a positive integer, and they all exist in a hash table.
2. Traverse every integer N from 1 to n and process it as follows:
2.1 traverse each cube y smaller than or equal to n/2 in the hash table. If n-y exists in the hash table, N can be expressed as the sum of two cubes.
2.2 If two y conditions exist and the 2.1 condition is met, N meets the Question Setting condition.
3. Output The number of n that meet the condition.
However, no matter how optimized, once n> 10 ^ 8, it will not be less than one minute.
Later, I had a whimsy. Why should I test whether every n meets the conditions? Why don't I use y to calculate n?
So with the current algorithm:
1. Find all the cubes that are less than the given maximum integer N, that is, y that satisfies y = x * X (X, Y is a positive integer, and store them in a linked list (OR array) list.
2. Perform the following processing on each cube Y1 in the list:
Calculate Y1 + y2 for each cubic number Y2 in the list that is greater than or equal to Y1, and save the calculation result in a dictionary DIC. The key is Y1 + y2, and the value is 1, if the dictionary already contains key = Y1 + y2,
Then the value of DIC [Y1 + y2] is added with 1.
3. Find the keys whose values are 2 in DIC and output the number of keys.
Because the number of elements in the list (the number of open cubes smaller than N) is much smaller than N, the algorithm efficiency is improved. When n = 2*10 ^ 9, less than 1 second.
The following is an implementation:
1 Private Static Int Getdoublecubecount ( Int N)
2 {
3 Int [] Arcubes = New Int [( Int ) Math. Floor (math. Pow (n, 1.0 / 3 )];
4 Dictionary < Int , Int > Dicdbltubes = New Dictionary < Int , Int > ();
5
6 Int T = 1 ;
7 Int Index = 0 ;
8 Int Doublecube = 0 ;
9
10 While (Index < Arcubes. length)
11 {
12 Arcubes [Index ++ ] = T * T * T;
13 T ++ ;
14 }
15
16 For ( Int I = 0 ; I < Arcubes. length; I ++ )
17 {
18 For ( Int J = I; j < Arcubes. length; j ++ )
19 {
20 Doublecube = Arcubes [I] + Arcubes [J];
21 If (Doublecube > N) Break ;
22
23 If (Dicdbltubes. containskey (doublecube ))
24 {
25Dicdbltubes [doublecube]++;
26}
27 Else
28 {
29Dicdbltubes. Add (doublecube,1);
30}
31 }
32 }
33
34 N = 0 ;
35
36 Foreach ( Int Value In Dicdbltubes. values)
37 {
38 If (Value = 2 )
39 {
40N++;
41}
42 }
43 Return N;
44 }
The above implementation is only a complete implementation of ideas, and there should be a lot of room for optimization. If any of you have a better idea or an optimization method, you may wish to have more exchanges. Because my computer is single-core, there is no optimization algorithm to adapt to multi-core, multi-core, it should be faster.