Pandigital Fibonacci ends

The Fibonacci sequence is defined by the recurrence relation:

F[n] = F[n-1] + F[n-2], where F[1] = 1 and F[2] = 1.

It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.

Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.


两端为全数字的斐波那契数

斐波那契数列由如下递归关系生成:

F[n] = F[n-1] + F[n-2], where F[1] = 1 and F[2] = 1.

可以发现,包含有113位数字的F541是第一个后9位数字是1至9全数字(包含1至9所有的数字,但不一定按照从小到大的顺序)的斐波那契数,而包含有575位数字的F2749是第一个前9位数字是1至9全数字的斐波那契数。

若Fk是第一个前9位数字和后9位数字都是1至9全数字的斐波那契数,求k。

解题

直接暴力,可取,时间过长,没有那么多时间跑

mathblog 算了下4.6天,完全不能容忍的

想到直接根据公式计算,但是也不是很好的,Mathblog中提到了,只求fib的第九位,判断是否是0-9的数

利用 ,估算fib的值,或者说,当n很大的时候这个数和fib对于位的数是相等的,在题解中也看到好多都是根据这个思想计算的

其推到过程

Python
运行结果不对

不知道为什么

# coding=gbk

import time as time
import re
import math
import numpy as np
import math
def run():
f1 = 1
f2 = 1
index = 1
while True:
f = (f1 + f2)%1000000000
f1 = f2
f2 = f
index +=1
if isPandigital(f):
if first9(index):
print index
break def first9(n):
t = n *0.20898764024997873 - 0.3494850021680094
last9 = int(10**(t - int(t)+8 ))
return isPandigital(last9)
def isPandigital(s):
if len(str(s)) !=9:return False
return set(str(s)) == set('') t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"

Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数的更多相关文章

  1. HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)

    HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意:  求第n个斐波那契数的 ...

  2. [Swift]LeetCode509. 斐波那契数 | Fibonacci Number

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  3. Python练习题 030:Project Euler 002:偶数斐波那契数之和

    本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...

  4. HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】

    Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  5. Colossal Fibonacci Numbers(巨大的斐波那契数)UVA 11582

    评测地址:http://acm.hust.edu.cn/vjudge/problem/41990 The i'th Fibonacci number f (i) is recursively de n ...

  6. UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数

    大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...

  7. 842. Split Array into Fibonacci Sequence能否把数列返回成斐波那契数列

    [抄题]: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacc ...

  8. UVA - 11582 Colossal Fibonacci Numbers! (巨大的斐波那契数!)

    题意:输入两个非负整数a.b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负 ...

  9. (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

    根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...

随机推荐

  1. TList,TObjectList 使用——资源释放

    TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...

  2. 推荐一款系统软件:Unity tweak tool

    功能很多慢慢体会 在软件中心搜索unity tweak tool安装

  3. Tomcat配置HTTPS方式生成安全证书

    在Tomcat 6中配置SSL双向认证是相当容易的,本文将介绍如何使用JDK的keytool来为Tomcat配置双向SSL认证.并实现批量生成证书 系统需求:JDK 5.0Tomcat 6.0.16启 ...

  4. Cash flow

    Today,we learn about the cash flow. Cash flow summary: 1.The cash flow identity:(现金恒等式) cash flow fr ...

  5. ffmpeg 发布hls流

    本来主要讲述如何利用ffmpeg将输入视频流通过转码的方式转成m3u8文件.如何通过http的方法将切边推送给客户端,不在本文中讲述. 输入视频流可以是rtsp流,也可以是http,还可以是文件等等. ...

  6. HDU 5294 Tricks Device 最短路+最大流

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5294 题意: 给你个无向图: 1.求最少删除几条边就能破坏节点1到节点n的最短路径, 2.最多能删除 ...

  7. eclipse安装androidSDK地址,Android SDK Manager简介

    eclipse安装android插件地址:https://dl-ssl.google.com/android/eclipse 这个和安装其他插件方式一样:Help—Install New Softwa ...

  8. 亚马逊 在线测试题目 amazon (变种的)三叉树的最近公共祖先问题

    题目意思就是找一棵按上面链接所示的树对应的上面的两个点的最小公共祖先(LCP,Least Common Father),按照比较大小来依次返回自己的父亲节点就行了.具体看代码:getfather(a) ...

  9. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...

  10. JS范围

    JS API-->DOM/PoneGap/Cordova/Android/NodeJS JS OOP