gcd.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * cxxomfort example: gcd
  3. *
  4. * This program provides sample usage of the
  5. * cxxomfort equivalent to the C++17 gcd() and lcm() functions.
  6. *
  7. * As an added point, this also tests that
  8. * CXXO_FOREACH is working correctly.
  9. *
  10. */
  11. #include <cxxomfort/base.hpp>
  12. #include <cxxomfort/numeric.hpp>
  13. #include <cxxomfort/library/numeric.hpp>
  14. #include <cxxomfort/library/type_name.hpp>
  15. #include <numeric>
  16. #include <iostream>
  17. #include <cxxomfort/array.hpp>
  18. namespace cxl= cxxomfort::library;
  19. namespace numeric= cxxomfort::library::numeric;
  20. int main () {
  21. using namespace std;
  22. cout<< "gcd, lcm functions: "<< endl;
  23. const unsigned I= 18, J= 74;
  24. cout<< "I= "<< I<< " J= "<< J<< endl;
  25. cout<< "gcd= "<< gcd(I,J)<< endl;
  26. cout<< "lcm= "<< lcm(I,J)<< endl;
  27. cout<< "static_gcd, static_lcm: "<< endl;
  28. enum { EX=22, EY=33 };
  29. cout<< "using constant-time integers EX, EY : "<< EX<< ","<< EY<< "."<< endl;
  30. unsigned X = numeric::ctmath::static_gcd<I,J>::value;
  31. cout<< "template gcd of I, J: "<< X<< endl;
  32. unsigned Y = numeric::ctmath::static_gcd<EX,EY>::value;
  33. cout<< "template gcd of EX, EY: "<< Y<< endl;
  34. array<int, numeric::ctmath::static_gcd<EX,EY>::value> ai;
  35. cout<< "std::array with size equal to the gcd above: ";
  36. cout<< cxxomfort::typeid_demangle(typeid(ai))<< endl;
  37. // compile-time for C++03
  38. #if (CXXOMFORT_CXX_STD >= 2011)
  39. cout<< "constexpr gcd, lcm functions:"<< endl;
  40. constexpr unsigned GX = gcd(I,J);
  41. static_assert(2==GX, "gcd");
  42. cout<< "constexpr, GX: "<< GX<< endl;
  43. #endif
  44. }