We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Project Euler #34: Digit factorials
Project Euler #34: Digit factorials
Contest ends in
+ 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); } }
+ 0 comments import math as m
def sumfactordigit(n):
ch=str(n) s=0 for e in ch : s+=m.factorial(int(e)) if s % n == 0 : return(True) return(False)
d={10:0} s=0 for e in range(10,10**5 +1): d[e]=s if sumfactordigit(e): s+=e n=int(input()) print(d[n])
+ 0 comments python3 code
import math n = int(input()) if n < 19: sum_ = 0 elif n < 21: sum_ = 19 else: sum_ = 19 dic = dict() for j in range(0, 10): dic[j] = math.factorial(j) for i in range(21, n): tmp_sum = 0 odd_sum = 0 for s in str(i): if s in ('0', '1'): odd_sum += 1 if odd_sum % 2 == 1 and i % 2 == 0: continue for s in str(i): tmp_sum += dic[int(s)] if tmp_sum % i == 0: sum_ += i print(sum_)
+ 0 comments import math def iscurious_no(no): if sum([math.factorial(int(a)) for a in str(no)])%no==0: return True else: return False n=int(input()) req_sum=0 for i in range(10,n): if iscurious_no(i): req_sum+=i print(req_sum)
Load more conversations
Sort 48 Discussions, By:
Please Login in order to post a comment