当前位置: 代码迷 >> 综合 >> 欧拉工程第38题:Pandigital multiples
  详细解决方案

欧拉工程第38题:Pandigital multiples

热度:76   发布时间:2023-12-13 07:14:48.0

题目链接:https://projecteuler.net/problem=38
一个数,分别与1-n相乘,n>1,并把n个结果链接起来,如果连接后的数字是9位数字且各位是在都在1-9,1-9中的每个数字也只能出现一次。求满足这个条件的最大链接数。
例如:
92 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

思路:
1.先考虑一个数是不是只包含1-9的数字
1.1 长度是9
1.2不能包含0
1.3不能有重复数字
2.这个数乘以1-n,结果链接后的值,长度大于9时候结束
3.如果确定上界?

num*1链接上num*2 是 一个1到9只出现一次的九位数 ,此时num最大就是上界。
显然num最大是4位数,最大的4位数是9999

感觉不能再缩小了,其实最大的数是:9327

Java程序:

package projecteuler31to40;import java.util.Date;
import java.util.Set;
import java.util.TreeSet;class level38{
    void solve(){int Max_Value=9999;int num=0;int max=0;for(int i=2;i<Max_Value;i++){int digitnum=Mulitnum(i);if(digitnum>max && JudgeNineDigit(digitnum) ){max=digitnum;num=i;}}System.out.println(max+","+num);}int Mulitnum(int num){String str=String.valueOf(num);for(int i=2;i<=9;i++){if(str.length()<9){int res=num*i;str=str.concat(String.valueOf(res));}}if(str.length()>9) return 0;return Integer.parseInt(str);}boolean JudgeNineDigit (int num){String str=String.valueOf(num);Set<Character> set=new TreeSet<Character>();if(str.length()!=9) return false;if(str.indexOf("0")>-1) return false;for(int i=0;i<9;i++){set.add(str.charAt(i));}if(set.size()==9) return true;return false;}}
public class Problem38 {
    public static void main(String[] args){Date beginTime=new Date();new level38().solve();//932718654Date endTime=new Date();long Time=endTime.getTime()-beginTime.getTime();System.out.println("Time:"+Time/1000+"s"+Time%1000+"ms");}}

结果:

932718654,9327
Time:0s69ms

Python代码

def mulitnum(num):p=str(num)for i in range(2,10):if len(p)<9:res=num*ip=p+str(res)if len(p)>9:return 0return pfrom time import time
t0=time()
digits=set('123456789')
maxnum=0for i in range(2,9999):product=str(mulitnum(i))if len(product)==9 and digits==set(product):if int(product)>maxnum:maxnum=int(product)print maxnum
print("time is {0}".format(time() - t))   

结果:

>>> print maxnum
932718654
>>> 
>>> print("time is {0}".format(time() - t))   
time is 3.35899996758

Python Help Set

help(set)
Help on class set in module __builtin__:class set(object)|  set() -> new empty set object|  set(iterable) -> new set object|  |  Build an unordered collection of unique elements.|  建立一个无须唯一的元素集合|  Methods defined here:|  方法:|  __and__(...) |      x.__and__(y) <==> x&y|  |  __cmp__(...)|      x.__cmp__(y) <==> cmp(x,y)|  |  __contains__(...)|      x.__contains__(y) <==> y in x.|  |  __eq__(...)|      x.__eq__(y) <==> x==y|  |  __ge__(...)|      x.__ge__(y) <==> x>=y|  |  __getattribute__(...)|      x.__getattribute__('name') <==> x.name|  |  __gt__(...)|      x.__gt__(y) <==> x>y|  |  __iand__(...)|      x.__iand__(y) <==> x&=y|  |  __init__(...)|      x.__init__(...) initializes x; see help(type(x)) for signature|  |  __ior__(...)|      x.__ior__(y) <==> x|=y|  |  __isub__(...)|      x.__isub__(y) <==> x-=y|  |  __iter__(...)|      x.__iter__() <==> iter(x)|  |  __ixor__(...)|      x.__ixor__(y) <==> x^=y|  |  __le__(...)|      x.__le__(y) <==> x<=y|  |  __len__(...)|      x.__len__() <==> len(x)|  |  __lt__(...)|      x.__lt__(y) <==> x<y|  |  __ne__(...)|      x.__ne__(y) <==> x!=y|  |  __or__(...)|      x.__or__(y) <==> x|y|  |  __rand__(...)|      x.__rand__(y) <==> y&x|  |  __reduce__(...)|      Return state information for pickling.|  |  __repr__(...)|      x.__repr__() <==> repr(x)|  |  __ror__(...)|      x.__ror__(y) <==> y|x|  |  __rsub__(...)|      x.__rsub__(y) <==> y-x|  |  __rxor__(...)|      x.__rxor__(y) <==> y^x|  |  __sizeof__(...)|      S.__sizeof__() -> size of S in memory, in bytes|  |  __sub__(...)|      x.__sub__(y) <==> x-y|  |  __xor__(...)|      x.__xor__(y) <==> x^y|  |  add(...)|      Add an element to a set.|      |      This has no effect if the element is already present.|  |  clear(...)|      Remove all elements from this set.|  |  copy(...)|      Return a shallow copy of a set.|  |  difference(...)|      Return the difference of two or more sets as a new set.|      |      (i.e. all elements that are in this set but not the others.)|  |  difference_update(...)|      Remove all elements of another set from this set.|  |  discard(...)|      Remove an element from a set if it is a member.|      |      If the element is not a member, do nothing.|  |  intersection(...)|      Return the intersection of two or more sets as a new set.|      返回两个或更多的交集集作为一个新的集合。|      (i.e. elements that are common to all of the sets.)|  |  intersection_update(...)|      Update a set with the intersection of itself and another.|  |  isdisjoint(...)|      Return True if two sets have a null intersection.|  |  issubset(...)|      Report whether another set contains this set.|  |  issuperset(...)|      Report whether this set contains another set.|  |  pop(...)|      Remove and return an arbitrary set element.|      Raises KeyError if the set is empty.|  |  remove(...)|      Remove an element from a set; it must be a member.|      |      If the element is not a member, raise a KeyError.|  |  symmetric_difference(...)|      Return the symmetric difference of two sets as a new set.|      |      (i.e. all elements that are in exactly one of the sets.)|  |  symmetric_difference_update(...)|      Update a set with the symmetric difference of itself and another.|  |  union(...)|      Return the union of sets as a new set.|      |      (i.e. all elements that are in either set.)|  |  update(...)|      Update a set with the union of itself and others.|  |  ----------------------------------------------------------------------|  Data and other attributes defined here:|  |  __hash__ = None|  |  __new__ = <built-in method __new__ of type object>|      T.__new__(S, ...) -> a new object with type S, a subtype of T