Project Euler 25 1000-digit Fibonacci number
题意:在斐波那契数列( 1 ,1,2,3,5 ...... )中,第一个有1000位数字的是第几项?
思路:****滚动数组 + 大数加法
/*************************************************************************
> File Name: euler025.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月25日 星期日 11时24分33秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
int32_t main() {
int32_t fib[3][1100] = {0}; // fib[][0] 记录该数组所代表数字的最大位数
fib[1][0] = fib[1][1] = 1;
fib[2][0] = fib[2][1] = 1;
int32_t number = 2 , idx1 , idx2 , idx3;
while( fib[ number % 3 ][0] < 1000 ) {
number++;
idx1 = (number - 2) % 3; // 依靠取模运算进行“滚动”,fib[idx1] < fib[idx2] < fib[idx3]
idx2 = (number - 1) % 3;
idx3 = number % 3;
for(int32_t i = 1 ; i <= fib[idx2][0] ; i++)
fib[idx3][i] = fib[idx2][i] + fib[idx1][i];
fib[idx3][0] = fib[idx2][0];
for(int32_t i = 1 ; i <= fib[idx3][0] ; i++) {
if( fib[idx3][i] >= 10 ){
fib[idx3][i+1] += fib[idx3][i] / 10;
fib[idx3][i] %= 10;
if( fib[idx3][0] < i + 1 ) fib[idx3][0] = i + 1;
}
}
}
printf("%d\n",number);
return 0;
}
Project Euler 25 1000-digit Fibonacci number的更多相关文章
- project euler 25 fibonacci
数学方法: Saying that a number contains 1000 digits is the same as saying that it's greater than 10**999 ...
- project euler 12 Highly divisible triangular number
Highly divisible triangular number Problem 12 The sequence of triangle numbers is generated by addin ...
- Project Euler 92:Square digit chains C++
A number chain is created by continuously adding the square of the digits in a number to form a new ...
- Project Euler 20 Factorial digit sum( 大数乘法 )
题意:求出100!的各位数字和. /************************************************************************* > Fil ...
- Project Euler 16 Power digit sum( 大数乘法 )
题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...
- Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)
题目链接: https://projecteuler.net/problem=435 题意: The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined rec ...
- Project Euler 51: Prime digit replacements
通过替换*3这样一个两位数的第一位,我们可以发现形成的九个数字有六个是质数,即13, 23,43,53,73,83.类似的,如果我们用同样的数字替换56**3这样一个五位数的第三位和第四位,会生成56 ...
- Project Euler 56: Powerful digit sum
一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...
- Project Euler 63: Powerful digit counts
五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...
随机推荐
- i=i+1,i+=1与i++的区别
1. i=i+1 a.读取右i的地址 b,i=1 c.读取左i的地址 d. 值赋给左i 2.i+=1 a.读取左i的地址 b.i+1 c.值给i 3.i++ a.读取右i的地址 b.值加1
- ExtJs之Ext.Model的MemoryProxy
书上的代码已完全不可参考,只好按知识点从网上查资料一个一个实例 了. <!DOCTYPE html> <html> <head> <title>ExtJ ...
- ASP.NET--常用ORM框架
subsonic massive dapper 性能不错,接近原声的ADO.NET 这个是大家推荐的,stackoverflow用的就是这个框架 petapoco 这些都是ORM框架
- Spring MVC-页面重定向示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_page_redirection.htm 说明:示例基于Spring MVC 4. ...
- u-boot学习(五):u-boot启动内核
u-boot的目的是启动内核.内核位于Flash中,那么u-boot就要将内核转移到内存中.然后执行命令执行之.这些操作是由bootcmd命令完毕的. bootcmd=nand read.jffs2 ...
- WndProc函数参数列表
protected override void WndProc(ref Message m) 实现了这一点. 重写WndProc函数,可以捕捉所有窗口发生的消息.这样,我们就可以"篡改&qu ...
- php与java语法的区别
php与java语法的区别 个人觉得在学习语言时,可以通过比较来进行学习.由于长时间写java代码,对java的基本语法还算熟悉,现在转学php,就php中基本语法与java基本语法差异进行比较. 1 ...
- Intellij IDEA社区版打包Maven项目成war包,并部署到tomcat上
转自:https://blog.csdn.net/yums467/article/details/51660683 需求分析 我们利用 Intellij idea社区版IDE开发了一个maven的sp ...
- Springboot 之 引入Thymeleaf
转自:https://segmentfault.com/a/1190000011149325 前言 Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置 ...
- 虚基类——(1)定义人员类Person: 公有成员:姓名(Name); 保护成员:性别(Gender),年龄(Age); 构造函数和析构函数
题目描述: (1)定义人员类Person: 公有成员:姓名(Name): 保护成员:性别(Gender),年龄(Age): 构造函数和析构函数 (2) 从人员类Person派生学生记录类Student ...