2 Queens on N x N board I am trying to write an algorithm to solve a variation of the N queens problem. There are only 2 queens on an N x N board. I simply need to find the number of solutions, don't need to know what the solutions are.

Below is the code I have so far, and it works fine for N &lt; 100000. However I need to solve it for N > 130456789 so I'm looking for feedback on how to make it more efficient.

public class Chess {

    long size;
    long n;
    long board;
    long cRow;
    long cCol;
    long maxRow;
    long maxCol;
    long safes = 0;

    public static void main(String[] args) {
        Chess a = new Chess(6);
        a.calculateMoves();
    }


public Chess(long n) {
        size = n * n;
        this.n = n;
        maxRow = n - 1;
        maxCol = n - 1;
    }

    public void calculateMoves() {
        iterate();
    }

    public void iterate() {
        for (long i = 0; i &lt; n; i++) {
            for (long j = 0; j &lt; n; j++) {
                cRow = i;
                cCol = j;
                calculateSafePlaces();
            }
        }
        System.out.println(safes/2);
    }

    private void calculateSafePlaces() {
        long notSafePlaces = (2 * n) - 1;

        notSafePlaces += Nw();
        notSafePlaces += Ne();
        notSafePlaces += Se();
        notSafePlaces += Sw();

        safes += (n * n) - notSafePlaces;
    }

    public long Nw() {
        long x = cRow;
        long y = cCol;

        if (x == 0) {
            return 0;
        }

        if (y == 0) {
            return 0;
        }

        if (x == y) {
            return x;
        }

        if (x &gt; y) {
            return y;
        }

        if (y &gt; x) {
            return x;
        }
        return 0;
    }

    public long Ne() {
        long x = cRow;
        long y = cCol;

        long count = 0;
        boolean condition = true;

        if (x == 0 || y == maxCol) {
            return 0;
        }

        long distance_to_east = maxCol - y;
        long distance_to_north = x;
        if (distance_to_north &gt; distance_to_east) {
            return distance_to_east;
        }

        if (distance_to_east &gt; distance_to_north) {
            return distance_to_north;
        }

        if (distance_to_north == distance_to_east) {
            return distance_to_east;
        }
        return 0;
    }

    public long Se() {
        long x = cRow;
        long y = cCol;

        if (x == maxRow) {
            return 0;
        }

        if (y == maxCol) {
            return 0;
        }

        long distance_to_south = maxRow - x;
        long distance_to_east = maxCol - y;

        if (distance_to_south &gt; distance_to_east) {
            return distance_to_east;
        }

        if (distance_to_east &gt; distance_to_south) {
            return distance_to_south;
        }

        if (distance_to_south == distance_to_east) {
            return distance_to_south;
        }
        return 0;
    }

    public long Sw() {
        long x = cRow;
        long y = cCol;

        long count = 0;
        boolean condition = true;

        if (x == maxRow) {
            return 0;
        }

        if (y == 0) {
            return 0;
        }

        long distance_to_south = maxRow - x;
        long distance_to_west = y;

        if (distance_to_south &gt; distance_to_west) {
            return distance_to_west;
        }

        if (distance_to_west &gt; distance_to_south) {
            return distance_to_south;
        }

        if (distance_to_south == distance_to_west) {
            return distance_to_west;
        }
        return 0;
    }
}
This can be done by hand in constant time using a bit of math. Just break the board up into two regions to place the first queen in:


The queen is in a corner/edge. For each corner, this eliminates the n-1 squares on the same row, column, and one or two diagonals, plus one for the one the queen is on. There are 4*(n-1) edge pieces, so there are 4*(n-1)*(n^2-(3(n-1)+1)) possibilities.
The queen is in the middle, not on an edge. Each of the (n-2)^2 positions that the queen can be in eliminates 4*(n-1)+1 squares (row, column, two diagonals, and the one square for the queen), for a total of (n-2)^2*(n^2-4*(n-1)+1).


Add these two and you should have your answer (assuming I didn't make any math mistakes).