Advertisement
drandreasdr

Project Euler problem #12

Apr 19th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #ifndef PROBLEM12_H
  2. #define PROBLEM12_H
  3.  
  4. #include <cstdio>
  5. #include <vector>
  6. #include "math.h"
  7. using namespace std;
  8.  
  9. vector<int> getfactors(int n) {
  10.     vector<int> result;
  11.     int end = int(sqrt(n));
  12.     for (int i = 1; i < end; ++i) {
  13.         if (n % i == 0) {
  14.             result.push_back(i);
  15.             result.push_back(n / i);
  16.         }
  17.     }
  18.     if (n % end == 0) {
  19.         result.push_back(n / end);
  20.     }
  21.     return result;
  22. }
  23.  
  24. void run() {
  25.     int nfactors_mintarget = 500;
  26.     int nfactors = 0;
  27.     int i = 0;
  28.     int curtrinum = 0;
  29.     while (nfactors < nfactors_mintarget) {
  30.         i++;
  31.         curtrinum += i;
  32.         vector<int> factors = getfactors(curtrinum);
  33.         nfactors = factors.size();
  34.     }
  35.     printf("The triangle number 1 + 2 + ... + %u = %u has %u factors.\n", i, curtrinum, nfactors);
  36. }
  37.  
  38. #endif // PROBLEM12_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement