您的位置 首页 编程语言

A number n is vesuvian if it is the sum of two different pairs of squares

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;
    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。