#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <utility> #include <vector> using namespace std; class Factor { vector<pair<unsigned int, size_t>> factors; void factor(const unsigned int input) { auto i = 2U; while (input % i != 0U) { ++i; } const auto second = input / i; factors[input] = make_pair(i, second); if (second >= 2 && factors[second].first == 0U) { factor(second); } } public: vector<unsigned int> getFactors(unsigned int input) { if (input <= 1) { return vector<unsigned int>{}; } else if (input >= factors.size()) { factors.resize(input + 1U); factor(input); } else if (factors[input].first == 0U) { factor(input); } vector<unsigned int> result; do { result.push_back(factors[input].first); input = factors[input].second; } while (factors[input].first != 0U); return result; } size_t getFactorCount(const unsigned int input) { return getFactors(input).size(); } int compareFactors(const unsigned int lhs, const unsigned int rhs) { return getFactorCount(lhs) - getFactorCount(rhs); } unsigned int findNextPrime(unsigned int input) { if (++input == 1U) { return 1U; } do { if (input >= factors.size()) { factors.resize(input + 1U); } if (factors[input].first == 0U) { factor(input); } } while (factors[input++].second != 1U); return input - 1U; } unsigned int gcd(const unsigned int lhs, const unsigned int rhs) { vector<unsigned int> result; const vector<unsigned int> lhsFactors = getFactors(lhs); const vector<unsigned int> rhsFactors = getFactors(rhs); set_intersection(cbegin(lhsFactors), cend(lhsFactors), cbegin(rhsFactors), cend(rhsFactors), back_inserter(result)); return accumulate(cbegin(result), cend(result), 1U, multiplies<unsigned int>()); } unsigned int lcm(const unsigned int lhs, const unsigned int rhs) { const vector<unsigned int> lhsFactors = getFactors(lhs); const vector<unsigned int> rhsFactors = getFactors(rhs); const auto it = find_first_of(cbegin(lhsFactors), cend(lhsFactors), cbegin(rhsFactors), cend(rhsFactors)); return it == cend(lhsFactors) ? 1U : *it; } }; int main() { Factor factor; for (auto i = 0U; i < 256U; ++i) { const auto factors = factor.getFactors(i); cout << "\n\t" << i << '(' << factors.size() << "): "; copy(cbegin(factors), cend(factors), ostream_iterator<unsigned int>(cout, " ")); } cout << "\n\ngetFactorCount(128): " << factor.getFactorCount(128U) << "\ncompareFactors(64, 128): " << factor.compareFactors(64U, 128U) << "\ncompareFactors(128, 64): " << factor.compareFactors(128U, 64U) << "\ncompareFactors(8, 27): " << factor.compareFactors(8U, 27U) << "\ngcd(12, 15): " << factor.gcd(12U, 15U) << "\nlcm(512, 13): " << factor.lcm(512U, 13U) << "\nfindNextPrime(11)" << factor.findNextPrime(11U) << endl; }
Standard input is empty
0(0): 1(0): 2(1): 2 3(1): 3 4(2): 2 2 5(1): 5 6(2): 2 3 7(1): 7 8(3): 2 2 2 9(2): 3 3 10(2): 2 5 11(1): 11 12(3): 2 2 3 13(1): 13 14(2): 2 7 15(2): 3 5 16(4): 2 2 2 2 17(1): 17 18(3): 2 3 3 19(1): 19 20(3): 2 2 5 21(2): 3 7 22(2): 2 11 23(1): 23 24(4): 2 2 2 3 25(2): 5 5 26(2): 2 13 27(3): 3 3 3 28(3): 2 2 7 29(1): 29 30(3): 2 3 5 31(1): 31 32(5): 2 2 2 2 2 33(2): 3 11 34(2): 2 17 35(2): 5 7 36(4): 2 2 3 3 37(1): 37 38(2): 2 19 39(2): 3 13 40(4): 2 2 2 5 41(1): 41 42(3): 2 3 7 43(1): 43 44(3): 2 2 11 45(3): 3 3 5 46(2): 2 23 47(1): 47 48(5): 2 2 2 2 3 49(2): 7 7 50(3): 2 5 5 51(2): 3 17 52(3): 2 2 13 53(1): 53 54(4): 2 3 3 3 55(2): 5 11 56(4): 2 2 2 7 57(2): 3 19 58(2): 2 29 59(1): 59 60(4): 2 2 3 5 61(1): 61 62(2): 2 31 63(3): 3 3 7 64(6): 2 2 2 2 2 2 65(2): 5 13 66(3): 2 3 11 67(1): 67 68(3): 2 2 17 69(2): 3 23 70(3): 2 5 7 71(1): 71 72(5): 2 2 2 3 3 73(1): 73 74(2): 2 37 75(3): 3 5 5 76(3): 2 2 19 77(2): 7 11 78(3): 2 3 13 79(1): 79 80(5): 2 2 2 2 5 81(4): 3 3 3 3 82(2): 2 41 83(1): 83 84(4): 2 2 3 7 85(2): 5 17 86(2): 2 43 87(2): 3 29 88(4): 2 2 2 11 89(1): 89 90(4): 2 3 3 5 91(2): 7 13 92(3): 2 2 23 93(2): 3 31 94(2): 2 47 95(2): 5 19 96(6): 2 2 2 2 2 3 97(1): 97 98(3): 2 7 7 99(3): 3 3 11 100(4): 2 2 5 5 101(1): 101 102(3): 2 3 17 103(1): 103 104(4): 2 2 2 13 105(3): 3 5 7 106(2): 2 53 107(1): 107 108(5): 2 2 3 3 3 109(1): 109 110(3): 2 5 11 111(2): 3 37 112(5): 2 2 2 2 7 113(1): 113 114(3): 2 3 19 115(2): 5 23 116(3): 2 2 29 117(3): 3 3 13 118(2): 2 59 119(2): 7 17 120(5): 2 2 2 3 5 121(2): 11 11 122(2): 2 61 123(2): 3 41 124(3): 2 2 31 125(3): 5 5 5 126(4): 2 3 3 7 127(1): 127 128(7): 2 2 2 2 2 2 2 129(2): 3 43 130(3): 2 5 13 131(1): 131 132(4): 2 2 3 11 133(2): 7 19 134(2): 2 67 135(4): 3 3 3 5 136(4): 2 2 2 17 137(1): 137 138(3): 2 3 23 139(1): 139 140(4): 2 2 5 7 141(2): 3 47 142(2): 2 71 143(2): 11 13 144(6): 2 2 2 2 3 3 145(2): 5 29 146(2): 2 73 147(3): 3 7 7 148(3): 2 2 37 149(1): 149 150(4): 2 3 5 5 151(1): 151 152(4): 2 2 2 19 153(3): 3 3 17 154(3): 2 7 11 155(2): 5 31 156(4): 2 2 3 13 157(1): 157 158(2): 2 79 159(2): 3 53 160(6): 2 2 2 2 2 5 161(2): 7 23 162(5): 2 3 3 3 3 163(1): 163 164(3): 2 2 41 165(3): 3 5 11 166(2): 2 83 167(1): 167 168(5): 2 2 2 3 7 169(2): 13 13 170(3): 2 5 17 171(3): 3 3 19 172(3): 2 2 43 173(1): 173 174(3): 2 3 29 175(3): 5 5 7 176(5): 2 2 2 2 11 177(2): 3 59 178(2): 2 89 179(1): 179 180(5): 2 2 3 3 5 181(1): 181 182(3): 2 7 13 183(2): 3 61 184(4): 2 2 2 23 185(2): 5 37 186(3): 2 3 31 187(2): 11 17 188(3): 2 2 47 189(4): 3 3 3 7 190(3): 2 5 19 191(1): 191 192(7): 2 2 2 2 2 2 3 193(1): 193 194(2): 2 97 195(3): 3 5 13 196(4): 2 2 7 7 197(1): 197 198(4): 2 3 3 11 199(1): 199 200(5): 2 2 2 5 5 201(2): 3 67 202(2): 2 101 203(2): 7 29 204(4): 2 2 3 17 205(2): 5 41 206(2): 2 103 207(3): 3 3 23 208(5): 2 2 2 2 13 209(2): 11 19 210(4): 2 3 5 7 211(1): 211 212(3): 2 2 53 213(2): 3 71 214(2): 2 107 215(2): 5 43 216(6): 2 2 2 3 3 3 217(2): 7 31 218(2): 2 109 219(2): 3 73 220(4): 2 2 5 11 221(2): 13 17 222(3): 2 3 37 223(1): 223 224(6): 2 2 2 2 2 7 225(4): 3 3 5 5 226(2): 2 113 227(1): 227 228(4): 2 2 3 19 229(1): 229 230(3): 2 5 23 231(3): 3 7 11 232(4): 2 2 2 29 233(1): 233 234(4): 2 3 3 13 235(2): 5 47 236(3): 2 2 59 237(2): 3 79 238(3): 2 7 17 239(1): 239 240(6): 2 2 2 2 3 5 241(1): 241 242(3): 2 11 11 243(5): 3 3 3 3 3 244(3): 2 2 61 245(3): 5 7 7 246(3): 2 3 41 247(2): 13 19 248(4): 2 2 2 31 249(2): 3 83 250(4): 2 5 5 5 251(1): 251 252(5): 2 2 3 3 7 253(2): 11 23 254(2): 2 127 255(3): 3 5 17 getFactorCount(128): 7 compareFactors(64, 128): -1 compareFactors(128, 64): 1 compareFactors(8, 27): 0 gcd(12, 15): 3 lcm(512, 13): 1 findNextPrime(11)13