zoukankan      html  css  js  c++  java
  • Project Euler 75:Singular integer right triangles

    题目链接

    原题:

    It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.

    12 cm: (3,4,5)
    24 cm: (6,8,10)
    30 cm: (5,12,13)
    36 cm: (9,12,15)
    40 cm: (8,15,17)
    48 cm: (12,16,20)

    In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.

    120 cm: (30,40,50), (20,48,52), (24,45,51)

    Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can exactly one integer sided right angle triangle be formed?

    翻译:

    唯一的整数边直角三角形

    只能唯一地弯折成整数边直角三角形的电线最短长度是12厘米;当然,还有很多长度的电线都只能唯一地弯折成整数边直角三角形,例如:

    12厘米: (3,4,5)
    24厘米: (6,8,10)
    30厘米: (5,12,13)
    36厘米: (9,12,15)
    40厘米: (8,15,17)
    48厘米: (12,16,20)

    相反地,有些长度的电线,比如20厘米,不可能弯折成任何整数边直角三角形,而另一些长度则有多个解;例如,120厘米的电线可以弯折成三个不同的整数边直角三角形。

    120厘米: (30,40,50), (20,48,52), (24,45,51)

    记电线长度为L,对于L ≤ 1,500,000,有多少种取值只能唯一地弯折成整数边直角三角形?

    翻译来源

    解题思路:

    先参看维基百科,如下:

    image

    对正整数m、n,且m>n

    image

    若a 、b、c能构成直角三角形 ,则当且仅当:m和n互质,m-n是奇数

    同时a、b、c乘以k的整数倍也能够成直角三角形。

    解题方法就很明显的

    先考虑m的取值范围

    a、b是直角边、c是斜边,极端情况下:a=c=L/2,b=0,则n=0,m2  =L/2,则m = sqrt(L/2)

    这样在依靠上面的公式即可

    Java程序:

    package Level3;
    
    public  class PE075{
        
        void run(){
        int L = 1500000;
        int max_m = (int)Math.sqrt(L/2);
        int[] triple = new int[L+1];
        int a,b,c;
        int s;
        for(int m=2;m<=max_m;m++){
            for(int n=1;n<m;n++){
                if(gcd(m,n)==1 && (m+n)%2==1){
                a = m*m-n*n;
                b = 2*m*n;
                c = m*m+n*n;
                s = a+b+c;
    //            if(a*a+b*b==c*c){
                    while(s<=L){
                        triple[s]+=1;
                        s+=a+b+c;
                    }
    //            }
                }
            }
        }
        int count=0;
        for(int i=2;i<=L;i++)
            if(triple[i]==1)
                count++;
        System.out.println(count);
        
        }
        
        int gcd(int m,int n){
            int tmp;
            if(m<n){
                tmp=m;
                m=n;
                n=tmp;
            }
            while(n!=0){
                m = m - n;
                if(m<n){
                    tmp = m;
                    m = n;
                    n = tmp;
                }
            }
            return m;
        }
        
        public static void main(String[] args){
            long t0 = System.currentTimeMillis();
            new PE075().run();
            long t1 = System.currentTimeMillis();
            long t = t1 - t0;
            System.out.println("running time="+t/1000+"s"+t%1000+"ms");
        }
    }

    运行结果:

    161667
    running time=0s72ms

    Python程序:

    import math 
    import time 
    def gcd(m,n):
        if m<n:
            tmp = n
            n = m 
            m = tmp 
        while n:
            m = m%n 
            if m<n:
                tmp = n 
                n = m 
                m = tmp 
        return m  
    
    def PE075():
        L = 1500000
        count = 0
        max_m = int(math.sqrt(L/2))
        triple = [0 for i in range(0,L+1)]
        for m in range(2,max_m+1):
            for n in range(1,m):
                if gcd(m,n)==1 and (m+n)%2==1:
                    a = m*m-n*n 
                    b = 2*m*n 
                    c = m*m+n*n 
                    s = a+b +c 
                    while s<=L:
                        triple[s] +=1 
                        if triple[s]==1:
                            count+=1
                        if triple[s]==2:
                            count-=1
                        s+= a+b+c 
        return count
      
    if __name__=='__main__':
        t0 = time.time()
        print "result={0},running time={1}s".format(PE075(),(time.time()-t0))

    运行结果:

    result=161667,running time=1.09399986267s
  • 相关阅读:
    python spark 求解最大 最小 平均
    python spark 求解最大 最小 平均 中位数
    我的spark python 决策树实例
    python spark 随机森林入门demo
    python spark 决策树 入门demo
    php 使用curl发起https请求
    awk调用shell命令的两种方法:system与print
    js 中 setTimeout()的用法
    Linux里AWK中split函数的用法
    awk substr()函数
  • 原文地址:https://www.cnblogs.com/theskulls/p/4850232.html
Copyright © 2011-2022 走看看