使用三重嵌套循环暴力查找勾股数及不同算法的比较(Pythagorean Triples)

勾股数即直角三角形的三条边边长对应的一组3个数字,使用三重嵌套for循环可通过不断试错而挖掘出勾股数。

本例中,第一种算法类似于“暴力查找”,第二种算法,基于第一种的结果而优化,性能有了明显的改善。

 

代码如下:

//Java how to program, 10th edtion
//Exercise 5.21, Pythagorean Triples
/**(Pythagorean Triples) A right triangle can have sides whose lengths are all integers. The set
of three integer values for the lengths of the sides of a right triangle is called a Pythagorean triple.
The lengths of the three sides must satisfy the relationship that the sum of the squares of two of the
sides is equal to the square of the hypotenuse. Write an application that displays a table of the
Pythagorean triples for side1, side2 and the hypotenuse, all no larger than 500. Use a triple-nested
for loop that tries all possibilities. This method is an example of “brute-force” computing. You’ll
learn in more advanced computer science courses that for many interesting problems there’s no
known algorithmic approach other than using sheer brute force.*/

import java.util.Date;

public class PythagoreanTriples {
	
	public static void main(String[] args){
		int counter1=0;
		int counter2=0;
		int SIZE=30;
		long timeBegin1=0;
		long timeBegin2=0;
		long timeEnd1=0;
		long timeEnd2=0;
		long timeConsumed1=0;
		long timeConsumed2=0;
		
		timeBegin1=new Date().getTime();
		System.out.printf("算法1开始于:%d\n",timeBegin1);
		System.out.printf("Side1\t\tSide2\t\tHypotenuse\n");
		//Algorithm 1
		for (int i=0;i<=SIZE;i++){
			for (int j=0;j<=SIZE;j++){
				for (int k=0;k<=SIZE;k++)
					if (i*i+j*j==k*k && (i+j>k)&&(i+k>j) &&(k+j>i) &&(i<=j) &&(j<=k)){
						System.out.printf("%d\t\t%d\t\t%d\n",i,j,k);
						counter1++;}
			}
			
		}
		System.out.printf("共%d组数字\n",counter1);
		timeEnd1=new Date().getTime();
		System.out.printf("算法1结束于:%d,用时%d\n",timeEnd1,timeEnd1-timeBegin1);
		
		//Algorithm 2
		timeBegin2=new Date().getTime();
		System.out.printf("\n\n算法2开始于:%d\n",timeBegin2);
		System.out.printf("Side1\t\tSide2\t\tHypotenuse\n");
		for (int i=0;i<=SIZE;i++){
			for (int j=SIZE;j>=i;j--){
				for (int k=SIZE;k>=j;k--)
					if (i*i+j*j==k*k && (i+j>k)&&(i+k>j)){
						System.out.printf("%d\t\t%d\t\t%d\n",i,j,k);
						counter2++;}
			}
			
		}
		System.out.printf("共%d组数字",counter2);
		timeEnd2=new Date().getTime();
		System.out.printf("算法2结束于:%d,用时%d\n",timeEnd2,timeEnd2-timeBegin2);
		
		System.out.printf("\n算法1开始于:%d\n",timeBegin1);
		System.out.printf("算法1结束于:%d,用时%d\n",timeEnd1,timeEnd1-timeBegin1);
		System.out.printf("算法2开始于:%d\n",timeBegin2);
		System.out.printf("算法2结束于:%d,用时%d\n",timeEnd2,timeEnd2-timeBegin2);
		System.out.printf("算法2比算法1用时少%d毫秒,为前者用时的百分之%.2f",(timeEnd1-timeBegin1)-(timeEnd2-timeBegin2),
				(double)(timeEnd2-timeBegin2)/(timeEnd1-timeBegin1)*100);
		}
}


 

运行结果:

算法1开始于:1466525186244
Side1  Side2  Hypotenuse
3  4  5
5  12  13
6  8  10
7  24  25
8  15  17
9  12  15
10  24  26
12  16  20
15  20  25
18  24  30
20  21  29
共11组数字
算法1结束于:1466525186365,用时121


算法2开始于:1466525186365
Side1  Side2  Hypotenuse
3  4  5
5  12  13
6  8  10
7  24  25
8  15  17
9  12  15
10  24  26
12  16  20
15  20  25
18  24  30
20  21  29
共11组数字算法2结束于:1466525186375,用时10

算法1开始于:1466525186244
算法1结束于:1466525186365,用时121
算法2开始于:1466525186365
算法2结束于:1466525186375,用时10
算法2比算法1用时少111毫秒,为前者用时的百分之8.26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值