Project Euler #34: Digit factorials

Sort by

recency

|

51 Discussions

|

  • + 0 comments

    JavaCode

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            int[] facts = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
            Scanner s = new Scanner(System.in);
            int N = s.nextInt();  
            int count = 0;
    
            for (int i = 10; i <= N; i++) {
                int number = i;
                int sum = 0;
    
                while (number != 0) {
                    int temp = number % 10;  
                    sum += facts[temp]; 
                    number /= 10;  
                }
    
                if (sum % i == 0) {
                    count+=i;
                }
            }
    
            System.out.println(count);
        }
    }
    
  • + 0 comments

    100% python

    import math
    n = int(input())
    ans = 0
    
    for num in range(10, n):
        s = [math.factorial(int(i)) for i in " ".join(str(num)).split()]
        if sum(s) % num == 0:
            ans += num
    
    print(ans)
    
  • + 0 comments
    def fact(n):
        if n==0 or n==1:
            return 1
        else:
            return n*fact(n-1)
    sum=0
    for i in range(10, int(input())+1):
        sum1=0
        for j in str(i):
            sum1+=fact(int(j))
        if(sum1%i==0):
            sum+=i
    print(sum)
    
  • + 0 comments

    Here is my code : 100 points

    def factorial(x):
        if x==0:
            return 1
        return x*factorial(x-1)
    lookup=[0]*1000001
    sum_=0
    for i in range(10,100000+1):
        s=0
        t=i
        while t>0:
            r=t%10
            s+=factorial(r)
            t=t//10
        if s%i==0:
            sum_+=i
        lookup[i]=sum_
    n=int(input().strip())
    print(lookup[n])
    
  • + 0 comments

    JAva code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
                 Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int sum = 0;
    
            if (n < 19) {
                sum = 0;
            } else if (n < 21) {
                sum = 19;
            } else {
                sum = 19;
                Map<Integer, Integer> dic = new HashMap<>();
    
                for (int j = 0; j < 10; j++) {
                    dic.put(j, factorial(j));
                }
    
                for (int i = 21; i < n; i++) {
                    int tmpSum = 0;
                    int oddSum = 0;
                    String strI = Integer.toString(i);
    
                    for (char s : strI.toCharArray()) {
                        if (s == '0' || s == '1') {
                            oddSum += 1;
                        }
                    }
    
                    if (oddSum % 2 == 1 && i % 2 == 0) {
                        continue;
                    }
    
                    for (char s : strI.toCharArray()) {
                        tmpSum += dic.get(Character.getNumericValue(s));
                    }
    
                    if (tmpSum % i == 0) {
                        sum += i;
                    }
                }
            }
    
            System.out.println(sum);
            scanner.close();
        }
    
        static int factorial(int n) {
            if (n <= 1) {
                return 1;
            }
            return n * factorial(n - 1);
        
        }
    }