【ProjectEuler】ProjectEuler_025
来源:互联网 发布:用java写一个爬虫 编辑:程序博客网 时间:2024/06/10 22:39
// Problem 25// 30 August 2002// // The Fibonacci sequence is defined by the recurrence relation:// // Fn = Fn1 + Fn2, 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 first term in the Fibonacci sequence to contain 1000 digits?using System;using System.Collections.Generic;using System.Text;namespace projecteuler025{ class Program { static string[] data = new string[1000000]; static void Main(string[] args) { F1(); } private static void F1() { Console.WriteLine(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod()); DateTime timeStart = DateTime.Now; int i = 1; while (Fibonacci(i).Length < 1000) { i++; } Console.WriteLine("Fibonacci(" + i + ") = " + data[i]); Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds + "\n\n"); } private static string Fibonacci(int n) { if (n<=2) { data[n] = "1"; } else { data[n] = longAdd(data[n - 1], data[n - 2], 0); } return data[n]; } /// <summary> /// 超大数字表达式加法 /// </summary> /// <param name="s1">数1</param> /// <param name="s2">数2</param> /// <param name="c">后面的数的进位,范围0~2</param> /// <returns></returns> private static string longAdd(string s1, string s2, int c) { //s1,s2的末位Index int l1 = s1.Length - 1; int l2 = s2.Length - 1; int x; //如果2个数都不为空 if (l1 >= 0 && l2 >= 0) { x = s1[l1] - '0' + s2[l2] - '0' + c; return longAdd(s1.Substring(0, l1), s2.Substring(0, l2), x / 10) + (x % 10).ToString(); } //下面的情况,s1和s2中至少有一个为空,我们要做的是找到那个不为空的进行下一步操作 //把不为空的值放到s1里 if (l2 >= 0) { s1 = s2; l1 = l2; } else if (l1 == -1) //表示全为空,判断最后的进位,如果没进位,返回空白,结束递归 { if (c != 0) { return c.ToString(); } return ""; } x = s1[l1] - '0' + c; //如果s1只有1位 if (l1 == 0) { return x.ToString(); } //如果s1有不止一位 return longAdd(s1.Substring(0, l1), "", x / 10) + (x % 10).ToString(); } }}/*Void F1()Fibonacci(4782) = 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816Total Milliseconds is 7583.463By GodMoon*/