Euler Project question 68th: Magic 5-gon Ring

Source: Internet
Author: User

Topic links

The sum of three numbers on any line is equal to 9, clockwise, starting from the smallest outer ring, and the resulting sequence is: 432621213

and sequence

A 9-bit string; The maximum string that a triangular ring can form is 432621513.

Using the numbers 1 through 10, you can get 16-bit or 17-bit strings through different arrangements. What is the largest 16-bit string that can be formed with a five-point ring?

16-bit character, 10 in outer ring

17-bit character, 10 inner ring

Find the largest string

6,7,8,9,10 should be in outer circle, 1,2,3,4,5 inner circle

(1+2+3+4+5) +6+7+8+9+10 = 70, every three digits and 14

The following can actually be done by hand

The smallest number of outside laps begins: 6, in 1-5 find two number so that three number of the and is 14 only have 5, 3, Maximum, 5 to in the middle

Number of outer rings left: 7,8,9,10, number of inner laps left: 1,2,4.3 on the same line as 11, only 10+1=11

Number of outer rings left: 7,8,9, number of inner laps left: 2,4.1 on the same line as 13, only 9+4=11

Number of outer rings left: 7, 8, number of inner laps left: 2

So here's the thing.

The answer is: 6531031914842725

Below with Python expose cracked

Import a package that produces an arrangement

 from Import permutations

You should know what to do next.

Traverse all permutations, select the number that satisfies the above criteria

All Programs:

 fromItertoolsImportpermutationsImport time as timedefrun (): Digits=['1','2','3','4','5','6','7','8','9','Ten'] Perm=permutations (digits) Res=0 forPinchPerm:arr=Map (int,p) candidate= P[0] + p[1] + p[2] +p[3] + p[2] + p[4]+p[5] + p[4] + p[6]+p[7] + p[6] + p[8]+p[9] + p[8] + p[1]; #Print candidate        ifArr[0] > Arr[3]orArr[0]> Arr[5]orArr[0] > Arr[7]orArr[0]> Arr[9]:Continue        ifarr[1]==10orarr[2]==10orarr[4]==10orARR[8] ==10:Continue        if(arr[0]+arr[1]+arr[2])! = (Arr[3] + arr[2] + arr[4]):Continue        if(arr[0]+arr[1]+arr[2])! = (Arr[5] + arr[4] + arr[6]):Continue        if(arr[0]+arr[1]+arr[2])! = (Arr[7] + arr[6] + arr[8]):Continue        if(arr[0]+arr[1]+arr[2])! = (Arr[9] + arr[8] + arr[1]):Continue        ifInt (candidate) >res andLen (candidate) ==16: Res=Int (candidate)#Print res, arr                PrintResif __name__=='__main__': Start=Time.time () run ()Print "Running time:", (Time.time ()-start),'s'

Results

653103191484272530.3180000782 s

It's been a long time.

 Packageproject61; Public classp68{int[] p = {1,2,3,4,5,6,7,8,9,10}; voidrun () { for(inti=1;i<20;i++) {getnextperm (); System.out.println ("" +p[0]+p[1]+p[2]+p[3]+p[4]+p[5]+p[6]+p[7]+p[8]+p[9]); The String result="";  while(true){            if(! Getnextperm ()) Break; if(Checkresult ()) {String candidate= "" + p[0] + p[1] + p[2]                                + p[3] + p[2] + p[4]                                + p[5] + p[4] + p[6]                                + p[7] + p[6] + p[8]                                + p[9] + p[8] + p[1];            SYSTEM.OUT.PRINTLN (candidate); }        }            }     //similar to Quick sort    Booleangetnextperm () {intN =p.length; intI= N-1;  while(p[i-1]>=P[i]) {i--; if(i<1)return false; }        intj =N;  while(p[j-1]<=p[i-1]) {J= J-1; } Swap (I-1,j-1); I++; J=N;  while(i<j) {Swap (I-1,j-1); I++; J--; }                return true; }    BooleanCheckresult () {if(p[1]==10| |p[2]==10| |p[4]==10| |p[8]==10)return false; if(P[0] > p[3]| |p[0] > P[5]| |p[0] > P[7]| |p[0] > P[9])return false; if(P[0] + p[1]+ p[2]! = P[3] + p[2] +p[4])return false; if(P[0] + p[1]+ p[2]! = P[5] + p[4] +p[6])return false; if(P[0] + p[1]+ p[2]! = P[7] + p[6] +p[8])return false; if(P[0] + p[1]+ p[2]! = p[9] + p[8] +p[1])return false; return true; }    voidSwapintIintj) {        intk=P[i]; P[i]=P[j]; P[J]=K; }     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis (); NewP68 (). run (); LongT1 =System.currenttimemillis (); System.out.println ("Running Time:" + (T1-T0) + "MS"); }}

The arrangement of this own parameter, and then find the number that satisfies the condition

The program runs fast.

6531031914842725running Time:77ms

Reference links

Euler Project question 68th: Magic 5-gon Ring

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.