fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdio.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. class BigInt{
  9. // Array with the parts of the big integer in little endian
  10. vector<int> value;
  11. int base;
  12. void add_most_significant(int);
  13. void add_most_significant_otherbig(BigInt*, int);
  14. public:
  15. BigInt(int begin=0, int _base=100): value({begin}), base(_base){ };
  16. BigInt(BigInt& other): value(other.value), base(other.base){ };
  17. ~BigInt(){ };
  18. void add(int);
  19. void addBig(BigInt*);
  20. void print(ostream& output) const;
  21. static BigInt* fibonacci(int);
  22. };
  23.  
  24. void BigInt::add_most_significant_otherbig(BigInt* other, int m){
  25. int carry = m, sum;
  26. for(int i = value.size(); i < other->value.size(); ++i){
  27. sum = other->value[i] + carry;
  28. value.push_back(sum % base);
  29. carry = sum/base;
  30. }
  31. while(carry){
  32. value.push_back(carry % base);
  33. carry /= base;
  34. }
  35. }
  36.  
  37. void BigInt::add(int a){
  38. int sum = 0, carry = a;
  39. for(int i = 0; i < value.size(); i++){
  40. sum = value[i] + carry;
  41. value[i] = sum % base;
  42. carry = sum/base;
  43. }
  44. if (carry)
  45. add_most_significant(carry);
  46. }
  47.  
  48. void BigInt::add_most_significant(int m){
  49. int carry = m;
  50. while(carry){
  51. value.push_back(carry % base);
  52. carry /= base;
  53. }
  54. }
  55.  
  56. void BigInt::addBig(BigInt* other){
  57. if(base != other->base){ // Won't work if they have different base
  58. cerr << "Addition of BigInt objects with different base not implemented yet!" << endl;
  59. exit(1);
  60. }
  61. int sum = 0, carry = 0, i;
  62. for(i = 0; i < value.size() && i < other->value.size(); ++i){
  63. sum = value[i] + other->value[i] + carry;
  64. value[i] = sum % base;
  65. carry = sum/base;
  66. }
  67. for(; i < value.size() && carry;++i){
  68. sum = value[i] + carry;
  69. value[i] = sum % base;
  70. carry = sum/base;
  71. }
  72. add_most_significant_otherbig(other, carry);
  73. }
  74.  
  75. ostream& operator<<(ostream& output, const BigInt bigint){
  76. bigint.print(output);
  77. return output;
  78. }
  79.  
  80. void BigInt::print(ostream& output) const {
  81. // string of the format depends on the "size" of the base (trailing 0 in format => fill with zeros if needed when printing)
  82. int l_base = to_string(base-1).length();
  83. string format("%0" + to_string(l_base) + "d");
  84.  
  85. // Begin with the most significant part: outside the loop because it doesn't need trailing zeros
  86. output << value[value.size() - 1];
  87. char* buf = new char[l_base+1];
  88. for(int i = value.size() - 2; i >= 0; i-- ){
  89. sprintf(buf, format.c_str(), value[i]);
  90. output << buf;
  91. }
  92. delete[] buf;
  93. }
  94.  
  95. BigInt* BigInt::fibonacci(int n){
  96. if( n <= 0){
  97. return new BigInt(0);
  98. }
  99.  
  100. BigInt* previous[] = {new BigInt(0), new BigInt(1)};
  101. for(int i = 2; i <= n; ++i){
  102. previous[i&1]->addBig(previous[(i-1)&1]);
  103. }
  104. //Result is in previous[i&1]
  105. // but first delete the other one: previous[!(i&1)] -> ! will turn 0 into 1 and 1 into 0
  106. delete previous[!(n&1)];
  107.  
  108. return previous[n&1];
  109. }
  110.  
  111. int main() {
  112. int n = 10000;
  113. BigInt* bigint = BigInt::fibonacci(n);
  114. cout << *bigint << endl;
  115. delete bigint;
  116. return 0;
  117. }
Success #stdin #stdout 0.05s 3472KB
stdin
Standard input is empty
stdout
33644764876431783266621612005107543310302148460680063906564769974680081442166662368155595513633734025582065332680836159373734790483865268263040892463056431887354544369559827491606602099884183933864652731300088830269235673613135117579297437854413752130520504347701602264758318906527890855154366159582987279682987510631200575428783453215515103870818298969791613127856265033195487140214287532698187962046936097879900350962302291026368131493195275630227837628441540360584402572114334961180023091208287046088923962328835461505776583271252546093591128203925285393434620904245248929403901706233888991085841065183173360437470737908552631764325733993712871937587746897479926305837065742830161637408969178426378624212835258112820516370298089332099905707920064367426202389783111470054074998459250360633560933883831923386783056136435351892133279732908133732642652633989763922723407882928177953580570993691049175470808931841056146322338217465637321248226383092103297701648054726243842374862411453093812206564914032751086643394517512161526545361333111314042436854805106765843493523836959653428071768775328348234345557366719731392746273629108210679280784718035329131176778924659089938635459327894523777674406192240337638674004021330343297496902028328145933418826817683893072003634795623117103101291953169794607632737589253530772552375943788434504067715555779056450443016640119462580972216729758615026968443146952034614932291105970676243268515992834709891284706740862008587135016260312071903172086094081298321581077282076353186624611278245537208532365305775956430072517744315051539600905168603220349163222640885248852433158051534849622434848299380905070483482449327453732624567755879089187190803662058009594743150052402532709746995318770724376825907419939632265984147498193609285223945039707165443156421328157688908058783183404917434556270520223564846495196112460268313970975069382648706613264507665074611512677522748621598642530711298441182622661057163515069260029861704945425047491378115154139941550671256271197133252763631939606902895650288268608362241082050562430701794976171121233066073310059947366875