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. php匿名函数小示例

    <?php //$fun = function($params){ // echo $params; //}; // //$fun('aa'); //例一 //在普通函数中定义一个匿名函数 // ...

  2. QT实现软件重启

    //重启软件 void MainWindow::on_pushButton_UI_reboot_clicked() { //方式1 需要主函数中事件循环判断 //qApp->exit(773); ...

  3. 2.1孙鑫C++

    0.vc++6.0 工程---win32控制台程序            文件---c++ 1.建立结构体 #include <iostream.h> struct Point { int ...

  4. pagebar

    public static string GetPageBarStr(int pageIndex, int pageSize, int pageCount, int recordCount)     ...

  5. IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    ... 首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Con ...

  6. Liferay IDE 3.1 M1发布啦

    很嗨森,以后就再也不用SDK和下载.ivy啦 新增功能主要有: 1.Liferay Workspace(用来存放Liferay Module项目) 2. Liferay Gradle Module P ...

  7. Careercup - Google面试题 - 4699414551592960

    2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...

  8. C#使用Socket登陆WordPress源码

    就在昨晚,在本屌丝刚刚发布屌丝与女神的回忆史<C#外挂QQ找茬辅助源码,早期开发>后,在苏飞大哥的技术讨论群有个群友提出一个问题.使用http协议模拟工具可以登录成功Wordpress但是 ...

  9. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  10. mac下phpstorm左侧的project列表找不到了

    早上开机,发现左侧的project列表没有了,用着不方便,当然了,是可以调出来的. View-Tool Windows-Project,就出来来. 快捷键:command+1. 问题解决.