Note that there are some explanatory texts on larger screens.

plurals
  1. POOvercoming heap overflow issues
    primarykey
    data
    text
    <p>I tried to solve problems from Project Euler. I know my method would work logically (it returns answers to the small scale problem almost instantly). However, it scales horribly. I already attempted changing the .ini file, but to no avail.</p> <p>Here's my code:</p> <pre><code>public class Number28 { static int SIZE = 101; //this should be an odd number, i accidentally posted 100 /** * @param args */ public static void main(String[] args) { double start = System.currentTimeMillis(); long spiral[][]= spiral(SIZE); long sum = 0; for(int i = 0; i &lt; SIZE; i++) { sum += spiral[i][i]; sum += spiral[i][SIZE - 1 - i]; } System.out.println(sum - 1); double time = System.currentTimeMillis() - start; System.out.println(time); } public static long[][] spiral(int size){ long spiral[][]= new long[size][size]; if(size == 1){ spiral[0][0] = 1; return spiral; } else{ long subspiral[][]= new long[size - 2][size - 2]; subspiral = spiral(size - 2); for(int r = 0; r &lt; size - 2; r++){ for(int c = 0; c &lt; size - 2; c++){ spiral[r + 1][c + 1] = subspiral[r][c]; } } long counter = subspiral[0][size - 3]; for(int r = 1; r &lt; size ; r++){ counter++; spiral[r][size - 1] = counter; } for(int c = size - 2; c &gt;= 0; c--){ counter++; spiral[size - 1][c] = counter; } for(int r = size - 2 ; r &gt;= 0 ; r--){ counter++; spiral[r][0] = counter; } for(int c = 1; c &lt; size ; c++){ counter++; spiral[0][c] = counter; } return spiral; } } } </code></pre> <p>Here's the edited code, worked like a gem:</p> <pre><code>public class Number28 { static int SIZE = 1001; static long spiral[][]= new long[SIZE][SIZE]; /** * @param args */ public static void main(String[] args) { double start = System.currentTimeMillis(); long spiral[][]= spiral(SIZE); long sum = 0; for(int i = 0; i &lt; SIZE; i++) { sum += spiral[i][i]; sum += spiral[i][SIZE - 1 - i]; } System.out.println(sum - 1); double time = System.currentTimeMillis() - start; System.out.println(time); } public static long[][] spiral(int size){ if(size == 1){ spiral[SIZE / 2][SIZE / 2] = 1; return spiral; } else{ long subspiral[][]= spiral(size - 2); int edge = (SIZE - size) / 2; long counter = subspiral[edge + 1][edge + size - 2]; for(int r = 1; r &lt; size ; r++){ counter++; spiral[edge + r][edge + size - 1] = counter; } for(int c = size - 2; c &gt;= 0; c--){ counter++; spiral[edge + size - 1][edge + c] = counter; } for(int r = size - 2 ; r &gt;= 0 ; r--){ counter++; spiral[edge + r][edge] = counter; } for(int c = 1; c &lt; size ; c++){ counter++; spiral[edge][edge + c] = counter; } return spiral; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload