Project Euler:Problem 25 1000-digit Fibonacci number

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn?1 + Fn?2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1

F2 = 1

F3 = 2

F4 = 3

F5 = 5

F6 = 8

F7 = 13

F8 = 21

F9 = 34

F10 = 55

F11 = 89

F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the index of the first term in the Fibonacci sequence to contain 1000 digits?

找到第一个1000位的斐波那契数的下标。

涉及到大数的加法。

两个string类型的数做加法,把短的那个数前面补上一定数量的‘0’使得做加法的两个数长度一致,然后从低位开始做加法。

#include <iostream>
#include <string>
using namespace std;

string pl(string a, string b)
{
	string res = "";
	if (a.length() < b.length())
	{
		string t = a;
		a = b;
		b = t;
	}
	int lo = a.length();
	int sh = b.length();
	string s(lo - sh, '0');
	b = s + b;
	int flag = 0;
	for (int i = lo - 1; i >= 0; i--)
	{
		int tmp = a[i] + b[i] - '0' - '0' + flag;
		int low = tmp % 10;
		flag = tmp / 10;
		char a1 = low + '0';
		res = a1 + res;
	}

	if (flag != 0)
		res = "1" + res;
	return res;
}

int main()
{
	string s[10000];
	s[1] = "1";
	s[2] = "1";
	for (int i = 3; i <= 10000; i++)
	{
		s[i] = pl(s[i-1], s[i-2]);
		if (s[i].length() >= 1000)
		{
			cout << i << endl;
			break;
		}
	}

	system("pause");
	return 0;
}
时间: 2024-10-05 12:08:55

Project Euler:Problem 25 1000-digit Fibonacci number的相关文章

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 56 Powerful digit sum

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. Considering natural numbers of the fo

Project Euler:Problem 16 Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? #include <iostream> #include <string> using namespace std; int main() { string s = "1"; for (int i = 1; i <= 1000;

Project Euler:Problem 63 Powerful digit counts

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power. How many n-digit positive integers exist which are also an nth power? 这样的数字满足以下条件: 对于数位为x的数S=k^x 有 10^(x-1)<=k^x<=10^x-1 #include &qu

Project Euler:Problem 92 Square digit chains

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

Project Euler:Problem 20 Factorial digit sum

n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. Find the sum of the digits in the number 100! #include <iostream> #include &

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 39 Integer right triangles

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. {20,48,52}, {24,45,51}, {30,40,50} For which value of p ≤ 1000, is the number of solutions maximised? #include <iostre