Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. //#include <boost/multiprecision/cpp_int.hpp>
  4. #include <boost/multiprecision/gmp.hpp>
  5.  
  6. namespace mp = boost::multiprecision;
  7.  
  8. //typedef mp::cpp_int mpint;
  9. typedef mp::mpz_int mpint;
  10.  
  11. mpint fib(uint32_t n);
  12. std::tuple<mpint, mpint> fib2(uint32_t n);
  13.  
  14. int main()
  15. {
  16. std::cout << "Hello World!\n";
  17. uint32_t n = ~(uint32_t)0;
  18. std::cout << "msb(fib(" << n << ")) = " << mp::msb(fib(n)) << std::endl;
  19. }
  20.  
  21. mpint fib(uint32_t n)
  22. {
  23. if (n == 0)
  24. {
  25. return 0;
  26. }
  27. auto tmp = fib2(n >> 1);
  28. auto m = std::get<0>(tmp);
  29. auto m1 = std::get<1>(tmp);
  30.  
  31. if ((n & 1) == 0)
  32. {
  33. return ((m1 << 1) + m) * m;
  34. }
  35. else
  36. {
  37. return (m * (m + m1) << 1) + m1 * m1;
  38. }
  39. }
  40.  
  41. std::tuple<mpint, mpint> fib2(uint32_t n)
  42. {
  43. if (n == 0)
  44. {
  45. return std::make_tuple(0, 1);
  46. }
  47.  
  48. auto tmp = fib2(n >> 1);
  49. auto m = std::get<0>(tmp);
  50. auto m1 = std::get<1>(tmp);
  51.  
  52. return (n & 1) == 0
  53. ? std::make_tuple<mpint, mpint>(((m1 << 1) + m) * m, m * m + m1 * m1)
  54. : std::make_tuple<mpint, mpint>((m * (m + m1) << 1) + m1 * m1, ((m1 << 1) + m) * m);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement