可以写成两个平方之和的数字

Uma*_*nth 6 java numbers number-theory

从数学原理:

数字N可表示为2个平方的总和,当且仅当在N的素数因子分解中,形式的每个素数都(4k+3)出现偶数次!

我所做的是预先计算所有4k+3数字并通过连续分割检查它的频率.

该程序是根据约束编写的:

1 < T <100
0 < N < 10 ^ 12
Run Code Online (Sandbox Code Playgroud)
import java.util.Scanner;

public class TwoSquaresOrNot {
    static int max = 250000;
    static long[] nums = new long[max];

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < max; ++i)
            nums[i] = 4 * i + 3;
        while (T-- > 0) {
            long n = sc.nextLong();
            System.out.println((canWrite(n) ? "Yes" : "No"));
        }
    }

    private static boolean canWrite(long n) {
        // TODO Auto-generated method stub
        for (int i = 0; i < nums.length; ++i) {//loop through all the numbers
            if (nums[i] > n)
                return true;
            int count = 0;
            while (n % nums[i] == 0) {
                count++;
                n /= nums[i];
            }
            if (count % 2 != 0)//check for odd frequency
                return false;
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这似乎不适用于SPOJ网站.

我错过了什么吗?或者我做错了什么?

0 也被认为是这个.

Some valid cases are:

1 = 1^2 + 0^2
8 = 2^2 + 2^2
Run Code Online (Sandbox Code Playgroud)

Die*_*oia 2

根据OP的评论进行编辑。

夫妇的事情。首先:如果你正在寻找质因数分解,你可以在 > sqrt(n) 时停止,不必一直到 n。

所以你的代码应该变成这样:

private static boolean canWrite(long n) {
    // TODO Auto-generated method stub
    for (int i = 0; i < nums.length; ++i) {//loop through all the numbers
        //FIRST CHANGE: Sqrt
        if (nums[i] > Math.sqrt(n))
            break;
        int count = 0;
        while (n % nums[i] == 0) {
            //SECOND CHANGE: count as an array
            count[i]++;
            n /= nums[i];
        }
    }
    //SECOND CHANGE: count as an array
    for (int i=0; i<count.length; i++) {
      if (count[i] %2 != 0) return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)