Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PROBLEM12_H
- #define PROBLEM12_H
- #include <cstdio>
- #include <vector>
- #include "math.h"
- using namespace std;
- vector<int> getfactors(int n) {
- vector<int> result;
- int end = int(sqrt(n));
- for (int i = 1; i < end; ++i) {
- if (n % i == 0) {
- result.push_back(i);
- result.push_back(n / i);
- }
- }
- if (n % end == 0) {
- result.push_back(n / end);
- }
- return result;
- }
- void run() {
- int nfactors_mintarget = 500;
- int nfactors = 0;
- int i = 0;
- int curtrinum = 0;
- while (nfactors < nfactors_mintarget) {
- i++;
- curtrinum += i;
- vector<int> factors = getfactors(curtrinum);
- nfactors = factors.size();
- }
- printf("The triangle number 1 + 2 + ... + %u = %u has %u factors.\n", i, curtrinum, nfactors);
- }
- #endif // PROBLEM12_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement