Project Euler:Problem 34 Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
#include <iostream> #include <vector> using namespace std; vector<int> int_vet(int a) { vector<int> res; while (a) { int tmp = a % 10; a /= 10; res.push_back(tmp); } return res; } int a[10] = { 0 }; void p() { a[0] = 1; a[1] = 1; for (int i = 2; i <= 9; i++) a[i] = a[i - 1] * i; } int main() { p(); int res = 0; for (int i = 3; i < 10000000; i++) { int count = 0; vector<int> num = int_vet(i); for (int j = 0; j < num.size(); j++) count += a[num[j]]; if (count == i) res += count; } cout << res << endl; system("pause"); return 0; }