鱼C论坛

 找回密码
 立即注册
查看: 3239|回复: 6

题目104:找出前九位和后九位数字为pandigital的斐波那契数

[复制链接]
发表于 2016-8-17 21:58:36 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 欧拉计划 于 2016-8-17 22:24 编辑
Pandigital Fibonacci ends

The Fibonacci sequence is defined by the recurrence relation:

QQ20160817-1@2x.png

It turns out that QQ20160817-2@2x.png 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 QQ20160817-3@2x.png which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.

Given that QQ20160817-4@2x.png is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.


题目:

斐波那契数列可以用一个递归关系来定义:

QQ20160817-7@2x.png 其中 QQ20160817-5@2x.png QQ20160817-6@2x.png

事实证明, QQ20160817-2@2x.png 一个包含 113 位数字的数,是第一个后九位数字都是 pandigital(包含 1 到 9 的所有数字,但未必按照顺序)的斐波那契数。 而 QQ20160817-3@2x.png 包含 575 位数字,是第一个前九位数字是 pandigital的斐波那契数。

QQ20160817-4@2x.png 是第一个前九位数和后九位数都是pandigital的斐波那契数,求 k。


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-28 00:25:35 | 显示全部楼层
程序是有了并通过了验算,但是我算到斐波那契数列的第14000位了都还没找到。。。
有兴趣的同学可以继续算下去,只需修改程序中的倒数第4行的值就能直接从数列的后XX位继续。
  1. def isPandigital(num):
  2.         flag = 0
  3.         if len(str(num)) > 9:
  4.                 lst1 = str(num)[:9]
  5.                 lst2 = str(num)[-9:]
  6.                 lst1 = set(lst1)
  7.                 lst2 = set(lst2)
  8.                 if len(lst1) == 9 and '0' not in lst1:
  9.                         if len(lst2) == 9 and '0' not in lst2:
  10.                                 flag = 1
  11.         return flag

  12. a = 1
  13. b = 1
  14. count = 2
  15. while 1:
  16.         c = a + b
  17.         a = b
  18.         b = c
  19.         count += 1
  20.         if count % 10000 == 0:
  21.                 print(count)
  22.         if count > 140000:
  23.                 if isPandigital(c) == 1:
  24.                         print (count)
  25.                         exit()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-10-21 20:59:39 | 显示全部楼层
还是上面的代码,稍微做了点改动,优化了算法。
  1. 329468
  2. [Finished in 5.1s]
复制代码
  1. def isPandigital(num):
  2.         s = set(str(num))
  3.         if len(s) == 9 and '0' not in s:
  4.                 return True
  5.         else:
  6.                 return False

  7. a = 1
  8. b = 1
  9. count = 2
  10. while 1:
  11.         c = a + b
  12.         a = b
  13.         b = c
  14.         count += 1
  15.         if count > 300000:
  16.                 clast = c%(10**9)
  17.                 if isPandigital(clast):
  18.                         if isPandigital(str(c)[:9]):
  19.                                 print (count)
  20.                                 exit()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-17 16:46:02 | 显示全部楼层
算的好慢...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-24 01:22:16 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-2-11 16:24 编辑
  1. from time import process_time as t
  2. t1=t()
  3. def ip(n):
  4.         if '0' in n or len(set(n))!=9:return False
  5.         return True
  6. def fib():
  7.         a,b=1,1
  8.         while 1:
  9.                 a,b=b,a+b
  10.                 yield str(b)
  11. for i in fib():
  12.         if len(i)<=575:continue
  13.         if ip(i[:9])and ip(i[-9:]):break
  14. print(f'运行结果:{str(count)}\n运行时间:{t()-t1}s')
复制代码

结果不明








[code]#include<iostream>
#include<string>



bool judge(const vector<unsigned char>& s)noexcept {
    char* str;
    unsigned char n;
    union {
        unsigned char digits[10];
        struct {
            unsigned long long a;
            unsigned short b;
        } x;
    } x;


    str = s.c_str();
    x.x.b = x.x.a = 0;

    for (n = 9; n; n--) {
        x.digits[*str]++;
        str++;
    }

    if ((x.x.a - (unsigned long long)0b0000000100000001000000010000000100000001000000010000000100000001) & (x.x.b - (unsigned short)0b100000001)) {
        return false;
    }


    str = s.c_str();
void add(vector<unsigned char>& a, const vector<unsigned char>& b)noexept {
    size_t n;

    for (n = b.size() - a.size(); n; n--) {
        a.push_back('0');
    }

    for (n = 0; n < a.size() - 1;) {
        a[n] += b[n];

        if (a[n] > 9) {
            a[n] -= 10;
            a[++n]++;
    }
}

   
int main() {
    using namespace std;
    ios::sync_with_stdio(false);

    vector<unsigned char> a = { 1 }, b = { 1 };


    while (a.size() <= 18) {
        add(a, b);
        swap(a, b);
    }

    for (;;) {
    }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-7 10:18:54 | 显示全部楼层
调用mpir库
//答案:329468
#include <iostream>
#include <algorithm>
#include <mpirxx.h>
#include <string>
using namespace std;

int main(void)
{
  mpz_class f1 = 1, f2 = 1, f3;
  int k = 3;
  while (true)
  {
    f3 = f2 + f1;
    string str = f3.get_str(10);
    if (str.length() >= 18)
    {
      string s = str.substr(str.length() - 9, 9);
      sort(s.begin(), s.end());
      if (s.compare("123456789") == 0)
      {
        s = str.substr(0, 9);
        sort(s.begin(), s.end());
        if (s.compare("123456789") == 0)
          break;
      }
    }
    f1 = f2;
    f2 = f3;
    ++k;
  }
  cout << k << endl;
  return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-31 03:16:13 | 显示全部楼层
  1. ah, al = 1, 1
  2. bh, bl = 1, 1

  3. for i in range(3, 9999999999999999):
  4.   bl += al
  5.   al = bl - al
  6.   bl %= int(1e9)
  7.   al %= int(1e9)

  8.   bh += ah
  9.   ah = bh - ah
  10.   sh, sl = str(bh), str(bl)
  11.   if len(sh) > 20:
  12.     bh //= 10
  13.     ah //= 10

  14.   if ''.join(sorted(sl)) == '123456789' and ''.join(sorted(sh[:9])) == '123456789':
  15.     print(i)
  16.     break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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