fork(1) download
  1. #include <cstdint>
  2.  
  3. // List of factors
  4. template<std::intmax_t...>
  5. struct factors { };
  6.  
  7. // Declaration
  8. template<bool no_remainder, std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  9. struct factorization_remove_repeated;
  10. template<bool no_remainder, std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  11. struct factorization_check;
  12.  
  13. // Remove repeated factors without appending to primes list
  14. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  15. struct factorization_remove_repeated<false, Value, Divisor, Primes...>
  16. {
  17. typedef typename factorization_check<Value % (Divisor + 1) == 0, Value, Divisor + 1, Primes...>::type type;
  18. };
  19.  
  20. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  21. struct factorization_remove_repeated<true, Value, Divisor, Primes...>
  22. {
  23. typedef typename factorization_remove_repeated<(Value / Divisor) % Divisor == 0, Value / Divisor, Divisor, Primes...>::type type;
  24. };
  25.  
  26. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  27. struct factorization_check<false, Value, Divisor, Primes...>
  28. {
  29. typedef typename factorization_check<Value % (Divisor + 1) == 0, Value, Divisor + 1, Primes...>::type type;
  30. };
  31.  
  32. template<std::intmax_t Value, std::intmax_t Divisor, std::intmax_t ... Primes>
  33. struct factorization_check<true, Value, Divisor, Primes...>
  34. {
  35. typedef typename factorization_remove_repeated<(Value / Divisor) % Divisor == 0, Value / Divisor, Divisor, Primes..., Divisor>::type type;
  36. };
  37.  
  38. template<std::intmax_t Divisor, std::intmax_t ... Primes>
  39. struct factorization_remove_repeated<false, 1, Divisor, Primes...>
  40. {
  41. typedef factors<Primes...> type;
  42. };
  43.  
  44. // Initial specialization
  45. template<std::intmax_t Value>
  46. struct factorization
  47. {
  48. typedef typename factorization_check<Value % 2 == 0, Value, 2>::type type;
  49. };
  50.  
  51. #include <typeinfo>
  52. #include <iostream>
  53. // Main
  54. int main()
  55. {
  56. typename factorization<18>::type x;
  57. std::cout << typeid(x).name();
  58. return 0;
  59. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
7factorsIILx2ELx3EEE