A number n is vesuvian if it is the sum of two different pairs of squares. For example, 50 is vesuvian because 50 == 25 + 25 and 1 + 49. The numbers 65 (1+64, 16+49) and 85 (4+81, 36+49) are also vesuvian. 789 of the first 10,000 integers are vesuvian.
Write a function named isVesuvian that returns 1 if its parameter is a vesuvian number, otherwise it returns 0. Hint: be sure to verify that your function detects that 50 is a vesuvian number!
java代码实现:
package com.zzy;
/**
* 更多请关注: http://huamaodashu.com
* Created by huamaodashu on 15/08/2018.
*/
public class Vesuvian {
public static void main(String[] args) {
System.out.println(isVesuvian(85));
int count =0;
for (int i = 0; i < 10000; i++) {
if(isVesuvian(i)==1){
count++;
}
}
System.out.println("count="+count);
}
public static int isVesuvian(int n){
int max = (int) Math.sqrt(n);
int count = 0;
int tmp = 0;
for (int i = 1; i < max; i++) {
for (int j = 2; j <= max; j++) {
if((i*i + j*j) == n && j != tmp){
tmp = i ;
// System.out.println("i="+i+",j="+j+",max="+max);
count ++;
}
}
}
if (count == 2){
return 1;
}
return 0;
}
}