欧拉工程第43题:Sub-string divisibility

题目链接:https://projecteuler.net/problem=43

0到9的排列所有形成的数中,求满足下来条件的数的和
Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:

d2d3d4=406 is divisible by 2
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17

Java写的,代码戳的一比,开始还是输出所有结果,然后,然后用R算的,后来搞个全局变量,这样就可以求和,但是输出来的有中间的和,最后一个才符合要求。

求排列的方法整理网上的,内涵还是没有理解。

Java代码:

package projecteuler41to50;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;
class OOOO { 
     public static long sum=0;
    }
class level43{


    void solve(){
        String[] numStr={"1","2","3","4","5","6","7","8","9","0"};
        permutation(numStr,0,9,0);
    }

    boolean divisible(String[] numStr,int[] div){
        for(int i=1;i<=7;i++){
            int d=Integer.valueOf(numStr[i]+numStr[i+1]+numStr[i+2]);
            if(d%div[i-1]!=0){
                return false;
            }
        }
        return true;
    }

    void permutation(String[] str,int start,int end,int sum1){
        int[] div={2,3,5,7,11,13,17};
        if(start==end){

             if(divisible(str,div)){
                 StringBuffer sb=new StringBuffer();
                 for(int i=0;i<=9;i++)
                     sb.append(str[i]);
                 String res=sb.toString();
// Set set=new TreeSet();
// set.add(res);

                 OOOO.sum+=Long.valueOf(res);
                 System.out.println(OOOO.sum+" "+res);
             }

        }
        for(int i=start;i<=end;i++){//交换数组第一个元素与后续的元素 
            String temp=str[start];
            str[start]=str[i];
            str[i]=temp;
            permutation(str,start+1,end,0);//后续元素递归全排列 

            temp=str[start];//将交换后的数组还原 
            str[start]=str[i];
            str[i]=temp;
        }
    }
}
public class Problem43 {

    public static void main(String[] args){
        Date beginTime=new Date();
        new level43().solve();
        Date endTime=new Date();
        Long Time=endTime.getTime()-beginTime.getTime();
        System.out.println("Time="+Time/1000+"秒"+Time%1000+"毫秒");
    }
}

结果:

1430952867 1430952867 2891310156 1460357289 4297667445 1406357289 8428620312 4130952867 12588977601 4160357289 16695334890 4106357289 Time=2秒179毫秒

仿照Java代码,写的Python代码
Python 有个好处,就是可以直接导入模块来求出排列

Python代码:

def divis(num):
    primes=[2,3,5,7,11,13,17]
    for i in range(1,8):
        p=str(num)
        digit=int(p[i:(i+3)]) 
        if digit%primes[i-1]!=0:
            return False
    return True


from itertools import permutations
from time import time 
t1=time()
digits=[ int(''.join(p))  for p in itertools.permutations("0123456789") ]

result=0
for x in digits:
    if divis(int(x)):
        result+=int(x)

t2=time()   
elapsed = t2 - t1

print("Found " + str(result)+" in " + str(round(elapsed, 5)) + " seconds")

结果:

Found 16695334890 in 24.038 seconds

24s。。。。。什么也不说了

你可能感兴趣的:(java,python,欧拉工程,欧拉工程-java)