Project Euler > Problem 20 > Factorial digit sum (Java Solution)

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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact