四方定理(递归) --java

四方定理

 数论中有著名的四方定理:所有自然数至多只要用四个数的平方和就可以表示。

 我们可以通过计算机验证其在有限范围的正确性。

import java.*;
import java.util.*;
public class Main121 {

     
    public static int f(int n, int a[], int idx) {  
        if (n==0) // 填空1   
            return 1;  
        if (idx == 4)  
            return 0;  
  
        for (int i = (int) Math.sqrt(n); i >= 1; i--) {  
            a[idx] = i;  
  
            if (f(n-i*i, a, idx+1) == 1) // 填空2   
                return 1;  
        }  
        return 0;  
    }  
    public static void main(String[] args) {  
        Scanner scan = new Scanner(System.in);  
        for (;;) {  
            int number;  
            System.out.printf("输入整数(1~10亿):");  
            number = scan.nextInt();  
            int a[] = { 0, 0, 0, 0 };  
            int r = f(number, a, 0);  
            System.out.printf("%s: %d %d %d %d\n", r==1?"有结果":"无结果", a[0], a[1], a[2], a[3]);  
        }  
    }  


}

猜你喜欢

转载自www.cnblogs.com/ls-pankong/p/10468567.html