Project Euler 86:cuboid Route box Path

Source: Internet
Author: User

Cuboid Route

A spider, S, sits in one corner of a cuboid, measuring 6 by 5 by 3, and a fly, F, sits in the opposite corner. By travelling on the surfaces of the shortest "straight line" distance from S to F are ten and the path is shown on The diagram.

However, there is up to three "shortest" path candidates for any given cuboid and the shortest route doesn ' t always has Integer length.

It can is shown that there is exactly 2060 distinct cuboids, ignoring rotations, with integer dimensions, up to a maximum Size of M by M by M, for which the shortest route have integer length when m = 100. This is the least value of M for which the number of solutions first exceeds and the thousand; The number of solutions when M = 1975.

Find the least value of M such that number of solutions first exceeds one million.

Box path

The Spider S is located in a corner of a rectangular room that is 6 times 5 by 3 in size, while the fly F is precisely diagonally. Along the surface of the room, the shortest "straight" distance from S to f is 10, as shown in the path:

However, each cube has three possible shortest paths, and the final shortest path is not necessarily an integer.

Consider all integer sides of the cube room, maximum no more than MXMXM, when m=100 a total of 2,060 cubes The shortest path is an integer, and this is the smallest m;m=99 solution over 2000 and the shortest path of 1975 cubes is an integer.

Find the smallest m of the solution over 1 million.

Solving

can be directly asked.

In order to prevent the appearance of cube duplication in the process of calculation, it is assumed that a<=b <=c

The shortest path has three types:

path1 = (a+b) ^2 + c^2

path2 = (a+c) ^2 + b^2

Path3 = (c+b) ^2 + a^2

The above three values can be found to contain the square term of a B C, the difference is: 2AB 2 ac 2BC

It is obvious that 2ab is the minimum, that is to say path1 is the minimum path value, it is very simple to judge whether it is an integer.

Java Key Program

    Static voidrun () {intLimit = 1000000; intCount =0; intM = 1;  for(M = 1;; m++){//when a<= b <= c The minimum path is (a+b) * (a+b) + c*c open root             for(intA = 1;a<= M; a++){                 for(intb =a; b<= m;b++){                    intc =M; intPath = (a+b) * (a+b) + c*C; intTMP = (int) math.sqrt (path); if(Tmp*tmp = =path) {Count++; }                }            }            if(count>limit)                {System.out.println (M);  Break; }        }    }

Another way, refer to Links

Also assume: a<=b<=c

The minimum value is: (a+b) ^2 + c^2

You can think of a+b as a value AB.

Obviously the range of AB is [2,2m]

I can't read it at the back.

The above two kinds of amplification are fixed C values, C is the maximum value, to find the corresponding C to meet the conditions of the number of cubes, c+1 is obviously included in the case of C solution.

 PackageLevel3; Public classpe086{Static voidrun () {intLimit = 1000000; intCount =0; intM = 1;  for(M = 1;; m++){//when a<= b <= c The minimum path is (a+b) * (a+b) + c*c open root             for(intA = 1;a<= M; a++){                 for(intb =a; b<= m;b++){                    intc =M; intPath = (a+b) * (a+b) + c*C; intTMP = (int) math.sqrt (path); if(Tmp*tmp = =path) {Count++; }                }            }            if(count>limit)                {System.out.println (M);  Break; }        }    }    Static voidrun2 () {intLimit = 1000000; intc = 1; intCount = 0;  while(Count <limit) {C++;  for(intAB = 2;ab<= 2*c;ab++){                intPath = Ab*ab + c*B; intTMP = (int) math.sqrt (path); if(tmp*tmp==path) {Count+ = (ab>=c) 1+ (C-(ab+1)/2): AB/2; }            }//if (c ==100)//System.out.println (count);} System.out.println (c); }     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis ();        Run2 (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}

1818
Running Time=0s39ms

Python time is a bit long

#CODING=GBKImportTime as time t0=time.time ()Print3**2PrintInt (8**0.5) defrun (): Limit= 1000000Count=0 M= 1 whileCount <Limit: forAinchRange (1,m+1):             forBinchRange (a,m+1): C=M Path= (a+b) **2 + c**2tmp= Int (path) **0.5)                ifTmp**2 = =Path:count+=1M+ = 1PrintM-1#1818#running time= 1062.27400017 srun () T1=time.time ()Print "running Time=", (T1-T0),"s"            

Project Euler 86:cuboid Route box Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.