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.
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscanner=newScanner(System.in);intn=scanner.nextInt();intsum=0;if(n<19){sum=0;}elseif(n<21){sum=19;}else{sum=19;Map<Integer,Integer>dic=newHashMap<>();for(intj=0;j<10;j++){dic.put(j,factorial(j));}for(inti=21;i<n;i++){inttmpSum=0;intoddSum=0;StringstrI=Integer.toString(i);for(chars:strI.toCharArray()){if(s=='0'||s=='1'){oddSum+=1;}}if(oddSum%2==1&&i%2==0){continue;}for(chars:strI.toCharArray()){tmpSum+=dic.get(Character.getNumericValue(s));}if(tmpSum%i==0){sum+=i;}}}System.out.println(sum);scanner.close();}staticintfactorial(intn){if(n<=1){return1;}returnn*factorial(n-1);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #34: Digit factorials
You are viewing a single comment's thread. Return to all comments →
JAva code