@@ -30,9 +30,9 @@ public String run() {
30
30
31
31
// Process every integer in range
32
32
solutions = new IntArrayArray (LIMIT / 2 + 1 ); // Uses about 2 GiB of memory
33
- solutions .append (new int []{} );
34
- solutions .append (new int []{} );
35
- solutions .append (new int []{ 1 } );
33
+ solutions .append ();
34
+ solutions .append ();
35
+ solutions .append (1 );
36
36
long sum = 0 ;
37
37
for (int i = 3 ; i <= LIMIT ; i ++) {
38
38
int [] sols = getSolutions (i );
@@ -50,6 +50,8 @@ private int[] getSolutions(int n) {
50
50
if (smallestPrimeFactor [n ] == n ) // n is prime
51
51
return new int []{1 , n - 1 };
52
52
else {
53
+ // Note: Changing the ArrayList<Integer> to an implementation
54
+ // based on int[] does not yield meaningful speed improvement
53
55
List <Integer > temp = new ArrayList <>();
54
56
int p = smallestPrimeFactor [n ];
55
57
int [] sols = solutions .get (n / p );
@@ -60,30 +62,26 @@ private int[] getSolutions(int n) {
60
62
temp .add (k );
61
63
}
62
64
}
63
- return toIntArray (temp );
65
+
66
+ // Convert List<Integer> to int[]
67
+ int [] result = new int [temp .size ()];
68
+ for (int i = 0 ; i < result .length ; i ++)
69
+ result [i ] = temp .get (i );
70
+ return result ;
64
71
}
65
72
}
66
73
67
74
68
- private static int [] toIntArray (List <Integer > list ) {
69
- int [] result = new int [list .size ()];
70
- for (int i = 0 ; i < result .length ; i ++)
71
- result [i ] = list .get (i );
72
- return result ;
73
- }
74
-
75
-
76
75
77
- // Conceptually like int[][], but having elements all packed into one int[]
78
- private static class IntArrayArray {
76
+ // Conceptually like int[][], but having elements all packed into one int[].
77
+ private static final class IntArrayArray {
79
78
80
79
private int [] data ;
81
80
private int dataLength ;
82
81
private int [] starts ;
83
82
private int index ;
84
83
85
84
86
-
87
85
public IntArrayArray (int len ) {
88
86
data = new int [1 ];
89
87
dataLength = 0 ;
@@ -95,13 +93,12 @@ public IntArrayArray(int len) {
95
93
}
96
94
97
95
98
-
99
96
public int [] get (int i ) {
100
97
return Arrays .copyOfRange (data , starts [i ], starts [i + 1 ]);
101
98
}
102
99
103
100
104
- public void append (int [] arr ) {
101
+ public void append (int ... arr ) {
105
102
while (dataLength + arr .length > data .length )
106
103
data = Arrays .copyOf (data , data .length * 2 );
107
104
0 commit comments