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 #20: Factorial digit sum
Project Euler #20: Factorial digit sum
Contest ends in
+ 0 comments include
include
include
include
include
using namespace std;
unsigned long long factorial(long num) { unsigned long long mult = 1; while (num > 1) { mult *= num; num--; } return mult; }
int main() { long n; cin >> n; for (long i = 0; i < n; i++) { long current; cin >> current;
unsigned long long fact = factorial(current); long sum_of_digits = 0; while (fact > 0) { sum_of_digits += fact % 10; fact /= 10; } cout << sum_of_digits << endl; } return 0;
}
Can anyone explain what's wrong in my code? Only 2/4 test cases have successfully ran, and all remaining test cases were unsuccessful.
+ 0 comments def factorial(num) if num == 0 then return 1 else return num + factorial(num-1) end end
+ 0 comments def sumdigit(n):
ch=str(n) s=0 for e in ch : s+=int(e) return(s)
def factorial(n):
if n == 0 or n == 1: return (1) p = 1 for i in range(2, n + 1): p *= i return (p)
+ 0 comments import sys t = int(input()) for _ in range(t): n = int(input()) fact = 1 for x in range(1,n+1): fact *= x sum = 0 while(fact>0): r = fact%10 sum += r fact = fact//10 print(sum)
+ 0 comments def fact(n): r=1 for i in range(1,n+1): r*=i return r t= int(input()) for _ in range(t): n=int(input()) print(sum([int(x) for x in list(str(fact(n)))]))
Load more conversations
Sort 104 Discussions, By:
Please Login in order to post a comment