\$19!\$ is a curious number, as \$1!+9!=1+362880=362881\$ is divisible by \$19\$.
Find the sum of all numbers below \$N\$ which divide the sum of the factorial of their digits. Note: as \$1!,2!,\cdots,9!\$ are not sums, so they are not included.
Input Format: Input contains an integer \$N\$
Output Format: Print the answer corresponding to the test case.
Constraints: \$10^1 \le N \le 10^5\$
Sample Input
20
Sample Output
19
In the program below I am getting time limit exceeded error.
#include <iostream>
using namespace std;
int main() {
int remainder;
int sum = 0,belowN=0;
int c,factorial = 1,sumat=0;
int N,i=10;
cin>>N;
while(i<N-1)
{
c=i;
while (i>0) {
remainder = i%10;
sum = sum + remainder;
int digit = i%10;
i = i/10;
// cout<<digit << endl;
for(int j=1;j<=digit;j++)
factorial *= j;
sumat=sumat+factorial;
// cout<<factorial<<endl;
factorial=1;
}
if(sumat%c==0)
belowN= belowN+c;
i++;
}
cout<<belowN;
return 0;
}
i
in the loop. So I don't care how fast it runs, it will give you the wrong answer. And you never usesum
orremainder
\$\endgroup\$