Problem:
n! means n [×] (n [−] 1) [×] ... [×] 3 [×] 2 [×] 1
For example, 10! = 10 [×] 9 [×] ... [×] 3 [×] 2 [×] 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Solution:
648
Code:
The solution may include methods that will be found here: Library.java .
public interface EulerSolution{ public String run(); }
/* * Solution to Project Euler problem 20 * By Nayuki Minase * * http://nayuki.eigenstate.org/page/project-euler-solutions * https://github.com/nayuki/Project-Euler-solutions */ public final class p020 implements EulerSolution { public static void main(String[] args) { System.out.println(new p020().run()); } public String run() { String temp = Library.factorial(100).toString(); int sum = 0; for (int i = 0; i < temp.length(); i++) sum += temp.charAt(i) - '0'; return Integer.toString(sum); } }
No comments :
Post a Comment