hwangnk1004 / Algorithm

0 stars 0 forks source link

StepByStep #lagrange #252

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    int count = 0;

    for (long a = 0; a * a <= n; a++) {
        long a2 = a * a;
        if (a2 > n) {
            break;
        }
        for (long b = a; b * b <= n; b++) {
            long b2 = b * b;
            if (b2 > n) {
                break;
            }
            for (long c = b; c * c <= n; c++) {
                long c2 = c * c;
                long d2 = (int)Math.sqrt(n - (a2 + b2 + c2));

                if (c2 > n) {
                    break;
                }

                if (a2 + b2 + c2 + d2 * d2 == n && d2 >= 0 && d2 <= (int) Math.sqrt(n)) {
                    if (a <= b && b <= c && c <= d2) {
                        count++;
                    }
                }

            }
        }
    }
    System.out.println(count);

}

}