Euler Project question 51st: Prime digit Replacements

Source: Internet
Author: User

Topic links

Topic:

Of the 9 numbers obtained by the first digit of the permutation, six are prime numbers: 13,23,43,53,73 and 83.

By replacing the third and fourth digits of the 56**3 with the same number, this five-digit number is the first number that can get seven prime numbers, the prime numbers are: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. So the smallest of the 56003 is the smallest prime number with this property.

To find the smallest prime number, you can get eight prime numbers by replacing some of them with the same numbers (not necessarily adjacent ones).

Thinking of solving problems:

This problem is difficult.

You first need to find a template that might replace the match

The only information is that this number is prime, except that 1 and itself cannot be divisible by other integers.

This number can not be divided into three, (according to can not be divided into three, find the template of this number, this is seen on the internet), this number must not be divisible by three.

Assuming these eight prime numbers are 5-digit numbers,

Suppose this 5-digit number is: A, B, C, D, E

(a+b+c+d+e)%3 can only be taken

If only 1 bits are replaced:

E

Discussion on the value classification of the lower face (a+b+c+d)%3

1. (a+b+c+d)%3==0,e%3==1,2.e value: 1,2,4,7,8, can only have 5 such prime numbers

2. (a+b+c+d)%3==1, e%3==0,1 e value: 0,1,3,4,6,7,9, inappropriate

3. (a+b+c+d)%3==2, e%3==0,2. E value: 0,3,5,6,8,9, inappropriate

If only 2 bits are replaced:

D = = E

Discussion on the value classification of the lower face (a+b+c)%3

1. (a+b+c)%3==0,2e%3==1,2.e value: 1,2,4,5,7,8, inappropriate

2. (a+b+c)%3==1, 2e%3==0,1 e value: 0,2,3,5,6,8,9, inappropriate

3. (a+b+c)%3==2, 2e%3==0,2. E value: 0,1,3,4,6,7,9, inappropriate

If only 3 bits are replaced:

C==d = = E

Discussion on the value classification of the lower face (a+b)%3

1. (a+b)%3==0,3e%3==1,2. No value desirable, inappropriate

2. (a+b)%3==1, 3e%3==0,1 e value: 0,1,2,3,4,5,6,7,8,9, suitable

3. (a+b)%3==2, 3e%3==0,2. E value: 0,1,2,3,4,5,6,7,8,9, suitable

According to the above can find the law:

The point of our consideration is that the replacement part is a few numbers, which is not very related to the number.

The replacement part is a three-digit number: The replacement portion can be: 000,111,222,333,444,555,666,777,888,999

The original number may be four digits or five digits.

To replace a few numbers, we know.

The specific replacement to which several have not known ...

This time can only be violent ...

If it is a five-digit number:

The replacement model might be:

// A is to be changed, the same data, B is to remain        unchanged String[] digits5={"Baaab",                          "Abaab",                          "Aabab",                          "Aaabb"};

A can't be in the last one, and the words are not enough for 8 prime numbers.

If it is a six-digit number:

The replacement model might be:

String[] digits6={"Bbaaab",        "Babaab",         "Baabab"         , "Baaabb",          "Abbaab",        "Ababab",        "Abaabb",        "Aabbab",         "Aababb",         "aaabbb"};

The following is pure brute force.

Generate 9 numbers according to the template

Determine if there are 8 prime numbers in these nine numbers

It's OK.

Run the output result:

[109, 111109, 222109, 444109, 555109, 666109, 777109, 888109][121313, 222323, 323333, 424343, 525353, 626363, 828 383, 929393[40609, 141619, 242629, 343639, 444649, 646669, 747679, 949699] [857, 111857, 222857, 333857 , 555857, 666857, 777857, 888857]

Input 109 wrong, input 121313,ok here is required is the number of length is 6

Java code:

 Packageprojecteuler51to60;ImportJava.util.Iterator;ImportJava.util.Set;ImportJava.util.TreeSet;classlevel51{voidsolve0 () {//A is to be changed, the same data, B is to remain unchangedString[] digits5={"Baaab",                          "Abaab",                          "Aabab",                             "Aaabb"}; String[] Digits6={"Bbaaab",                          "Babaab",                          "Baabab",                          "Baaabb",                                              "Abbaab",                       "Ababab",                       "Abaabb",                                              "Aabbab",                       "Aababb",                                              "AAABBB"}; TreeSet<String> ts =NewTreeset<string>();  for(inti=0;i<=9;i++){             for(intj=0;j<=9;j++){                 for(intk=0;k<digits5.length;k++) {TS=combination (i, J, 0, Digits5[k],true); if(Isprimeset (TS)) System.out.println (i+ "" +j+ "" +ts+ "" +Digits5[k]); } }} TreeSet<String> TS2 =NewTreeset<string>();  for(inti=0;i<=9;i++){             for(intj=0;j<=9;j++){                 for(intm=0;m<=9;m++){                 for(intk=0;k<digits6.length;k++) {ts2=combination (i, J, M, Digits6[k],false); if(Isprimeset (TS2)) {//System.out.println (i+ "" +j+ "" +ts2+ "" +digits6[k]);System.out.println (Getprimeset (TS2));//121313} }}}} TreeSet<Integer> Getprimeset (treeset<string>ts) {Iterator<String> it=Ts.iterator (); TreeSet<Integer> Tset =NewTreeset<integer>();  while(It.hasnext ()) {intPrime=integer.parseint (It.next ());//Forcing type conversions            if(IsPrime (prime)) {Tset.add (prime); }        }                returnTset; }        BooleanIsprimeset (treeset<string>ts) {Iterator<String> it=Ts.iterator (); intFlag=0;  while(It.hasnext ()) {intPrime=integer.parseint (It.next ());//Forcing type conversions            if(IsPrime (prime)) {flag+=1; }        }        if(flag>=8)            return true; return false; } TreeSet<String> combination (intAintBintC,string pattern,Booleanflag) {        //The first 1 is replaced with a, the second 1 is B instead, and the third 1 uses C insteadTreeSet<String> tset=NewTreeset<string>(); if(flag==true){//5-digit number             for(inti=0;i<=9;i++) {String combstr=pattern.replacefirst ("B", A + ""); Combstr=combstr.replacefirst ("B", + + ""); Combstr=combstr.replace ("A", i+ "" ");            Tset.add (COMBSTR); }        }Else if(flag==false){//6-digit number             for(inti=0;i<=9;i++) {String combstr=pattern.replacefirst ("B", A + ""); Combstr=combstr.replacefirst ("B", + + ""); Combstr=combstr.replacefirst ("B", c+ "" "); Combstr=combstr.replace ("A", i+ "" ");                            Tset.add (COMBSTR); }        }        returnTset; }    BooleanIsPrime (intnum) {        if(num==2| | num==3 | | num==5| | NUM==7)return true; if(num<2 | | num%2==00)return false;  for(intI=3;I&LT;=MATH.SQRT (num); i++)            if(num%i==0)                return false; return true; }    } Public classProblem51 { Public Static voidMain (string[] args) {Longbegin=System.currenttimemillis (); Newlevel51 (). SOLVE0 (); LongEnd =System.currenttimemillis (); LongTime = end-begin; System.out.println ("Time:" +time/1000+ "S" +time%1000+ "MS"); }}

Euler Project question 51st: Prime digit Replacements

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.