找回密码
 欢迎注册
楼主: jmyhyu

[提问] 从1-33中,任取6个数,要求和为102,共有几种取法?

[复制链接]
 楼主| 发表于 2010-4-30 18:33:41 | 显示全部楼层
递归即可,我以前写过一个VB代码:http://blog.csdn.net/northwolves/archive/2007/12/03/1912809.aspx
northwolves 发表于 2010-4-24 23:42


写代码的话就简单多了
但是纯数学方法。。。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2019-10-10 08:32:16 | 显示全部楼层
效率最高的代码:
  1. Select[IntegerPartitions[102, {6}, Range[33]], Length[Union[#]] == 6 &]
复制代码

点评

Select的第二个参数可以改成DuplicateFreeQ  发表于 2019-10-10 20:35
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2019-10-10 21:11:44 | 显示全部楼层
如果问题改成从1-33中任取10个数使和为102,用IntegerPartitions速度也不够快了
  1. Select[IntegerPartitions[102,{10},Range[33]],DuplicateFreeQ]//Length//AbsoluteTiming

  2. Compile[{},Sum[Boole[102-a-b-c-d-e-f-g-h-i<=33],{a,5},{b,a+1,(66-a)/9},{c,b+1,1/8 (74-a-b)},{d,c+1,1/7 (81-a-b-c)},{e,d+1,1/6 (87-a-b-c-d)},{f,e+1,1/5 (92-a-b-c-d-e)},{g,f+1,1/4 (96-a-b-c-d-e-f)},{h,g+1,1/3 (99-a-b-c-d-e-f-g)},{i,h+1,1/2 (101-a-b-c-d-e-f-g-h)}]][]//AbsoluteTiming
复制代码

第二种方法如果能写成递归会简洁一些
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2019-10-18 22:57:10 | 显示全部楼层
# !/usr/bin/env python3.6.4
# -*- coding: utf-8 -*-
# @Author:Nicolas TU
# @Date: 2019-10-18 20:57:07
#从1-33中,任取6个不同的数,要求和为102,共有几种取法?
#ANS:20076 &#8203;&#8203;&#8203;&#8203;


import time
time_start=time.time()#计算时间开始

j=0;
for a in range(1,33+1):  # 迭代 1 到 33之间的数.假设a>=b>=c>=d>=e>=f,避免重复问题;
   for b in range(1,a+1):
       for c in range(1,b+1):
           for d in range(1,c+1):
               for e in range(1,d+1):
                   for f in range(1,e+1):
                       
                    # 逻辑关系
                        if a+b+c+d+e+f==102  and\
                                      a!=b and a!=c and a!=d and a!=e and a!=f and\
                                      b!=c and b!=d and b!=e and b!=f and\
                                      c!=d and c!=e and c!=f  and\
                                      d!=e and d!=f and\
                                      e!=f :#也可用列表来去重,len(set(list))==6;
                                      j += 1 # 统计解的个数
                                      #print('-'*60)
                                      #print ('第%d组解为:%d,%d,%d,%d,%d,%d\n.' % (j,a,b,c,d,e,f))
time_end=time.time()#计算时间结束                                
print('-'*60)     
print ('总计存在%d组解。' %j)
print('-'*60)

print('python3.6程序运行',time_end-time_start,'秒。')
print('-'*60)

'''
------------------------------------------------------------
总计存在20076组解。
------------------------------------------------------------
python3.6程序运行 1.4291820526123047 秒。
------------------------------------------------------------'''


'''
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2019-10-19 08:43:42 | 显示全部楼层
markfang2050 发表于 2019-10-18 22:57
# !/usr/bin/env python3.6.4
# -*- coding: utf-8 -*-
# @Author:Nicolas TU

或许你应该跟Mathematica比一比速度……
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2019-10-19 20:47:40 | 显示全部楼层
.·.·. 发表于 2019-10-19 08:43
或许你应该跟Mathematica比一比速度……

不比。么有可比性。MMA效率高。

点评

Chyang说的好,优化一下,你的速度不比一般MMA代码差,Chyang的编译版本当然第一 期待切相信你的功力  发表于 2019-10-20 08:50
早一点剪枝,Python也可以快很多  发表于 2019-10-19 22:30
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-5-3 19:22 , Processed in 0.042256 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表