fork download
  1. #include <algorithm>
  2. #include <iostream>
  3.  
  4. template<int N>
  5. struct num
  6. {
  7. char x[N + 1];
  8.  
  9. num()
  10. {
  11. std::fill(x, x + N, '0');
  12. x[N] = '\0';
  13. }
  14. };
  15.  
  16. template<int N>
  17. num<N> operator+(num<N> a, const num<N>& b)
  18. {
  19. int carry = 0;
  20. for (int i = N - 1; i >= 0; --i)
  21. {
  22. int sum = a.x[i] - '0' + b.x[i] + carry;
  23. if (sum <= '9')
  24. {
  25. a.x[i] = sum;
  26. carry = 0;
  27. }
  28. else
  29. {
  30. a.x[i] = sum - 10;
  31. carry = 1;
  32. }
  33. }
  34. return a;
  35. }
  36.  
  37. template<int N>
  38. num<N> fib(int n)
  39. {
  40. num<N> a;
  41. num<N> b;
  42. b.x[N - 1] = '1';
  43. for (int i = 0; i < n; ++i)
  44. {
  45. std::cout << a.x << '\n';
  46. num<N> c = a + b;
  47. a = b;
  48. b = c;
  49. }
  50. return a;
  51. }
  52.  
  53. int main()
  54. {
  55. std::cout << '\n' << fib<30>(100).x << '\n';
  56. }
  57.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
000000000000000000000000000000
000000000000000000000000000001
000000000000000000000000000001
000000000000000000000000000002
000000000000000000000000000003
000000000000000000000000000005
000000000000000000000000000008
000000000000000000000000000013
000000000000000000000000000021
000000000000000000000000000034
000000000000000000000000000055
000000000000000000000000000089
000000000000000000000000000144
000000000000000000000000000233
000000000000000000000000000377
000000000000000000000000000610
000000000000000000000000000987
000000000000000000000000001597
000000000000000000000000002584
000000000000000000000000004181
000000000000000000000000006765
000000000000000000000000010946
000000000000000000000000017711
000000000000000000000000028657
000000000000000000000000046368
000000000000000000000000075025
000000000000000000000000121393
000000000000000000000000196418
000000000000000000000000317811
000000000000000000000000514229
000000000000000000000000832040
000000000000000000000001346269
000000000000000000000002178309
000000000000000000000003524578
000000000000000000000005702887
000000000000000000000009227465
000000000000000000000014930352
000000000000000000000024157817
000000000000000000000039088169
000000000000000000000063245986
000000000000000000000102334155
000000000000000000000165580141
000000000000000000000267914296
000000000000000000000433494437
000000000000000000000701408733
000000000000000000001134903170
000000000000000000001836311903
000000000000000000002971215073
000000000000000000004807526976
000000000000000000007778742049
000000000000000000012586269025
000000000000000000020365011074
000000000000000000032951280099
000000000000000000053316291173
000000000000000000086267571272
000000000000000000139583862445
000000000000000000225851433717
000000000000000000365435296162
000000000000000000591286729879
000000000000000000956722026041
000000000000000001548008755920
000000000000000002504730781961
000000000000000004052739537881
000000000000000006557470319842
000000000000000010610209857723
000000000000000017167680177565
000000000000000027777890035288
000000000000000044945570212853
000000000000000072723460248141
000000000000000117669030460994
000000000000000190392490709135
000000000000000308061521170129
000000000000000498454011879264
000000000000000806515533049393
000000000000001304969544928657
000000000000002111485077978050
000000000000003416454622906707
000000000000005527939700884757
000000000000008944394323791464
000000000000014472334024676221
000000000000023416728348467685
000000000000037889062373143906
000000000000061305790721611591
000000000000099194853094755497
000000000000160500643816367088
000000000000259695496911122585
000000000000420196140727489673
000000000000679891637638612258
000000000001100087778366101931
000000000001779979416004714189
000000000002880067194370816120
000000000004660046610375530309
000000000007540113804746346429
000000000012200160415121876738
000000000019740274219868223167
000000000031940434634990099905
000000000051680708854858323072
000000000083621143489848422977
000000000135301852344706746049
000000000218922995834555169026

000000000354224848179261915075