// 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) = 10700662663827589367649805844573968850836838966321516650132352 03375314520604694040621889147582489792657804694888177591957484336466672569959512 99603046126274809248218614406943305123477444275027378175308757939166619214925918 67595539664228371489431130746995034395470019854326097230672901928705264472437261 17715821825548491120525013201478612965931381792235559657452039506137551467837543 22911960212993404826070617539770684706820289548690266618543512452190036948064135 74474709117076197669456910700980243934396174741037369125032313655321647736970231 67755051595173518460579954919410967778373229665796581646513903488154256310184224 19025984608800011018625555024549393711365165703944762958471454852342595042858242 53060835444354282126110089928637950480068943303097732178348645431132057656598684 56288616808718693835297350643986297640660000723562917905207051164077614812491885 83094594056668833910935094445657635766615161931775379289166158132715961687748798 3821820492520348473874384736771934512787029218636250627816 Total Milliseconds is 7583.463 By GodMoon */
【ProjectEuler】ProjectEuler_025
最新推荐文章于 2024-04-15 10:46:09 发布