鱼C论坛

 找回密码
 立即注册
查看: 2481|回复: 4

[技术交流] 小练习:20170925 齐肯多夫表示

[复制链接]
发表于 2017-9-24 19:56:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 冬雪雪冬 于 2017-10-3 19:02 编辑

从现在开始我们要开展一批欧拉计划的习题练习。

其实在我们论坛中已有欧拉计划的板块,可能有些鱼油还没注意到。

什么是欧拉计划:http://bbs.fishc.com/thread-60405-1-1.html

我们欧拉板块现已给出了200余题,这批练习将从欧拉计划中选题。其实用python语言完成有很多的优势,可以更简洁更方便的实现。

如果大家有兴趣也可浏览欧拉的英文网站:https://projecteuler.net/archives

这里已经有了500余题。


                               
登录/注册后可看大图


要求:

以python语言完成,如果是python2请注明。

程序以代码文字格式发帖。

注重程序效率和创意。

答题在一周内完成,即10.2 10:00之前,其后将公开大家的答案,并评比成绩。

另程序和答案可以在网上搜到,希望大家独立完成。题目不难,大家看看谁的效率高。

-- 回帖需写明解题思路,鼓励在程序中加上注释 --

-- 一些鱼油反映题目有些过难,为此略过一部分偏难的题目 --


                               
登录/注册后可看大图
297



齐肯多夫表示
斐波那契数列的每一项都由前两项相加而得。
从1和2开始,前10项是:1、2、3、5、8、13、21、34、55、89。
每一个正整数可以唯一地写成斐波那契数列中非连续项的和。例如,100 = 3 + 8 + 89。
这样的和被称为数的齐肯多夫表示
对于任意整数n>0,记z(n)为n的齐肯多夫表示中的项数。
因此,z(5) = 1,z(14) = 2,z(100) = 3,等等。
此外,对于0<n<106,∑&#8201;z(n)&#8201;=&#8201;7894453。
对于0<n<1017,求∑&#8201;z(n)。

Zeckendorf Representation
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
Starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
Every positive integer can be uniquely written as a sum of nonconsecutive terms of the Fibonacci sequence. For example, 100 = 3 + 8 + 89.
Such a sum is called the Zeckendorf representation of the number.
For any integer n>0, let z(n) be the number of terms in the Zeckendorf representation of n.
Thus, z(5)&#8201;=&#8201;1, z(14)&#8201;=&#8201;2, z(100)&#8201;=&#8201;3 etc.
Also, for 0<n<106, ∑&#8201;z(n)&#8201;=&#8201;7894453.
Find ∑&#8201;z(n) for 0<n<1017.

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-25 09:48:00 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2017-9-25 11:50 编辑

这题还是蛮简单的,求解思路是分治思想,把一个大数拆分成2个小数,递归求解,加上functools的lru_cache加速。
  1. from functools import lru_cache
  2. @lru_cache(maxsize=None)
  3. def euler297(n, record={1: 0}):
  4.     if n not in record:
  5.         fib1 = fib2 = 1
  6.         while fib1 < n:
  7.             fib2, fib1 = fib1, fib1 + fib2
  8.         record[n] = n - fib2 + euler297(n - fib2) + euler297(fib2)
  9.     return record[n]
  10. print(euler297(10 ** 17))
复制代码

2252639041804718029

评分

参与人数 1荣誉 +10 鱼币 +10 贡献 +10 收起 理由
冬雪雪冬 + 10 + 10 + 10

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-26 23:11:32 | 显示全部楼层
答案:2252639041804718029
  1. import time
  2. maxn = 10**17-1 #10^6 SigmaZ=7894453
  3. st = time.time()

  4. def generatorFeb(maxn):
  5.         r = [1, 2]
  6.         a = 1
  7.         b = 2
  8.         c = a + b
  9.         while c < maxn:
  10.                 r.append(c)
  11.                 a, b = b, c
  12.                 c = a + b
  13.         return r

  14. #∑Z(n) = Z(1)+Z(2)+...+Z(n)
  15. #SigmaZ(1, feblist[i]-1) + SigmaZ(1, n-feblist[i]) + n-feblist[i]+1
  16. #(n-feblist[i]+1) * Z(feblist[i]) = (n-feblist[i]+1) * 1

  17. def calSigmaZ(maxn):
  18.         def addSigmaZ(n, multiple, t):
  19.                 for i in range(len(t)):
  20.                         if t[i][0] < n:
  21.                                 t.insert(i, (n,multiple))
  22.                                 return t
  23.                         elif t[i][0] == n:
  24.                                 t[i] = (n, t[i][1]+multiple)
  25.                                 return t
  26.                 t.append((n, multiple))
  27.                 return t

  28.         feblist = generatorFeb(maxn)
  29.         sigmaZlist = [(maxn,1)] #include maxn
  30.         r = 0
  31.         for i in range(len(feblist)-1, 1, -1):
  32.                 t = []
  33.                 for z in sigmaZlist:
  34.                         if z[0] >= feblist[i]:
  35.                                 r += (z[0]-feblist[i]+1) * z[1]
  36.                                 t = addSigmaZ(feblist[i]-1, z[1], t)
  37.                                 t = addSigmaZ(z[0]-feblist[i], z[1], t)
  38.                                 #t.append(feblist[i]-1) #must merge
  39.                                 #t.append(z-feblist[i])
  40.                                
  41.                         else:
  42.                                 t = addSigmaZ(z[0], z[1], t)
  43.                                 #t.append(z)
  44.                 sigmaZlist = t
  45.         for z in sigmaZlist: #z(1) z(2) z(3)
  46.                 r += z[0] * z[1]
  47.         return r
  48. r = calSigmaZ(maxn)
  49. print(maxn, r) #2252639041804718029       
  50. print('cost {0}s'.format(time.time()-st)) #0.0004961490631103516s
复制代码

评分

参与人数 1荣誉 +10 鱼币 +10 贡献 +10 收起 理由
冬雪雪冬 + 10 + 10 + 10

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 22:53:23 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-8 21:48:31 | 显示全部楼层
写了一个最基础的解题方法,


  1. def fio(number):
  2.     x, y = 0, 1
  3.     result = []
  4.     while y <= number:
  5.         result.append(y)
  6.         x, y = y, x+y
  7.     return result
  8. #print(fio(20))#调用函数

  9. def qiken1(mid):
  10.     #计算一个齐肯多夫数,由几个数组成,
  11.     a = fio(mid)
  12.     count = 0
  13.     #计算和为某个数的个数。计算前10**6-1次方
  14.     for i in range(len(a)-1, 0 ,-1):
  15.         if mid - a[i] == 0:
  16.             count += 1
  17.             break
  18.         elif mid - a[i] > 0:
  19.             mid -= a[i]
  20.             count += 1
  21.         else:
  22.             i -= 1
  23.     return count

  24. def sum1(n):
  25.     a = []
  26.     for i in range(1, n+1):
  27.         a.append(qiken1(i))

  28.     return sum(a)

  29. data = 10**6 - 1
  30. print(sum1(data))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 15:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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