Project Euler #34: Digit factorials | Hackerrank | Project Euler

 Digit factorials 


19 is a curious number, as 1!+9!=1+362880=362881 which 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!,...,9! are not sums they are not included.

Input Format

Input contains an integer N

Constraints

10<=N<=10^5

Output Format

Print the answer corresponding to the test case.

Sample Input

20

Sample Output

19

Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    long long n;
    scanf("%lld",&n);
    long long total=0;
    for(long long i=10;i<=n;i++)
    {
        long long m,k,sum=0;
        k=i;
        while(k>0)    
         {   
         long long fact=1;
         m=k%10
         if(m==0)
         fact=1;
         else{
         for(long long j=1;j<=m;j++)  
         fact*=j;
         }
         sum+=fact;
         k=k/10;
         }
         //cout<<sum<<endl;
         if(sum%i==0)
          total+=i;
    }
     cout<<total;
    return 0;
}




You can also run it on an online IDE :


Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!

No comments:

Post a Comment

Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...