使用三重嵌套循环暴力查找勾股数及不同算法的比较(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

    原文作者:查找算法
    原文地址: https://blog.csdn.net/hpdlzu80100/article/details/51731083
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞