Sub-string divisibility

Problem 43

The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.

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

Find the sum of all 0 to 9 pandigital numbers with this property.

Answer:

16695334890

python code:

from functools import reduce

def helpfunc1(x,y):
return 10*x+y def helpfunc2(lst):
return reduce(helpfunc1,lst) def Descriminent(k,value):
if k==3:
if value%17==0:
return True
else:
return False
if k==4:
if value%13==0:
return True
else:
return False
if k==5:
if value%11==0:
return True
else:
return False
if k==6:
if value%7==0:
return True
else:
return False
if k==7:
if value%5==0:
return True
else:
return False
if k==8:
if value%3==0 and (value//10)%2==0:
return True
else:
return False
return True def func(result,charlist):
total=0
length=len(result)
if length>2 and length<9:
if Descriminent(length,helpfunc2(result[0:3]))==False:
return 0
if length==10:
return helpfunc2(result)
if len(charlist)>0:
for i in range(0,len(charlist)):
resultNext=result.copy()
charlistNext=charlist.copy()
resultNext.insert(0,charlistNext[i])
del charlistNext[i]
total+=func(resultNext,charlistNext)
return total charlist=[0,1,2,3,4,5,6,7,8,9]
result=[]
print(func(result,charlist))

解题思路:使用递归

提高效率点:从后往前递归

计算时间<1s

由于可以整除2的数比整除17的多的太多了,其它的一样道理。因此相比于从后往前。从前往后递归的话无用功将成阶乘阶数添加。

EularProject论坛上的一个非递归版本号

digits = ['1','2','3','4','5','6','7','8','9','0']
divisors = [13, 11, 7, 5, 3, 2, 1]
res = []
res1 = [] j = 1
while j*17 < 1000:
N = j*17
Nstr = str(N)
if len(set(Nstr)) == len(Nstr):
if N < 100: res.append('0' + str(N))
else: res.append(str(N))
j += 1 for div in divisors:
for Nstr in res:
for d in digits:
if d not in Nstr:
Newstr = d + Nstr[:2]
if int(Newstr)%div == 0:
res1.append(d + Nstr)
res = res1
res1 = [] tot = 0
for Nstr in res:
print(Nstr)
tot += int(Nstr)
print(tot)

EularProject 43: 带条件约束的排列组合挑选问题的更多相关文章

  1. Laravel 5.2数据库--多个关联关系,带条件约束的渴求式加载的问题

    ### 今天在连表获取数据的时候,老是获取不到想要的,确实有点无力适从的感觉. 归根到底,还是对laravel不够熟悉,至少是数据库操作那块. ### 问题是这样的: 我想要通过连表中间表,拿中间表的 ...

  2. python自带的排列组合函数

    需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯. 分析:提炼 ...

  3. 【专题】计数问题(排列组合,容斥原理,Prufer序列)

    [容斥原理] 对于统计指定排列方案数的问题,一个方案是空间中的一个元素. 定义集合x是满足排列中第x个数的限定条件的方案集合,设排列长度为S,则一共S个集合. 容斥原理的本质是考虑[集合交 或 集合交 ...

  4. DataFactory使用和注意,排列组合

    DataFactory使用和注意 mysql 连接ODBC开放数据库连接(Open Database Connectivity,ODBC)驱动程序 生成数据:int不能用 Build a compos ...

  5. hdu1521 排列组合(指数型母函数)

    题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数.         (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...

  6. 排列 && 组合

    最近编程经常遇到需要 排列&&组合(求子集) 的问题:遂整理一下. 1. 数字的排列与组合(递归):O(n!),O(nC(n,k)) * O(n) #include <stdio ...

  7. js 排列 组合 的一个简单例子

    最近工作项目需要用到js排列组合,于是就写了一个简单的demo. 前几天在网上找到一个写全排列A(n,n)的code感觉还可以,于是贴出来了, 排列的实现方式: 全排列主要用到的是递归和数组的插入 比 ...

  8. HDU 1521 排列组合 指数型母函数

    排列组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  9. 【排列组合】ZSC1076: 数学、不容易系列之三——考新郎

    国庆期间,省城刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的: 首先,给每位新娘打扮得几乎一模一样,并 ...

随机推荐

  1. Python的property装饰器的基本用法

    Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...

  2. winscp连接虚拟机Linux被拒绝的问题解决方案

    输入了正确的账号密码还出现这个错误 我们需要在虚拟机中配置一下,改成这样就行了

  3. C#操作Excel知识点

    近期在使用C#操作excel,主要是读取excel模板,复制其中的模板sheet页,生成多个sheet页填充相应数据后另存到excel文件,所用到的知识点如下. 一.添加引用和命名空间 添加Micro ...

  4. c#控件攻略宝典之ListBox控件

    ListBox控件的使用: 1)控件属性 Items SelectedItems SelectioModes 2)数据绑定 DataSoure DisplayMember ValueMenber 3) ...

  5. vue打包之后生成一个配置文件修改接口

    前言: 我们的vue代码打包上传到服务器之后, 要是数据接口 以后换了域名什么的,是不是需要重新去vue文件里修改接口. 能不能生成一个配置文件,里面可以配置域名或其它什么字段之类的,这样以后换了域名 ...

  6. 基础拾遗-----mongoDB操作

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  7. ajax异步传送数据的方法

    1, 此方法为ajax异步发送后台数据的方法 var payment_id=$(this).attr("name"); alert(payment_id); $('.label') ...

  8. Angular i18n的技术分享、踩过的坑

    1.安装 npm @ngx-translate/core --save npm @ngx-translate/http-loader 2.配置(文本背景部分为该模块新增的)~app.module.ts ...

  9. EasyNVR-流媒体服务详解

    1.什么是流媒体       所谓流媒体是指采用流式传输的方式在Internet播放的媒体格式. 流媒体又叫流式媒体,它是指商家用一个视频传送服务器把节目当成数据包发出,传送到网络上.   2.Eas ...

  10. react-router 3 中的 useRouterHistory(createHistory) 到了 react-router 4 变成了什么?

    react-router 3 文档: https://github.com/ReactTraining/react-router/blob/v3/docs/API.md react-router 4 ...