Project Euler: Problem 34

By | 24. April 2013

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

public class Project34 {
 
	public static void main(String[] args) {
		String number = "";
		int tempresult = 0;
		int result = 0;
		for (int i = 3; i < 1000000; i++) {
			number = Integer.toString(i);
			for (int j = 0; j < number.length(); j++) {
				tempresult += fac(Integer.parseInt(String.valueOf(number.charAt(j))));
			}
			System.out.println(i + " " + tempresult);
			if (i==tempresult) {
				System.out.println(i);
				result += i;
			}
			tempresult = 0;
		}
		System.out.println("Ergebnis: " + result);	 
	}
	
	public static int fac(int number){
		if (number==1 || number == 0) {
			return 1;
		}
		number = number*fac(number-1);
		return number;	 
	} 
}

Lösung: 40730

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert