In number theory, a ‘factorion’ in a given number base b is a natural number that equals the sum of the factorials of its digits. For example, 145 is a factorion because 1! + 4! + 5! = 1 + 24 + 120 = 145.
Write a program to find out the number of factorions (in base 10) in the entire range of integers.
The code below is the usual logic pursued by the students. This is begin provided so that one does not have to start from scratch.
class Factorion {
public static int getFactorial(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
public static boolean isFactorian(int n) {
int sum = 0;
for (int temp = n; temp > 0; temp /= 10) {
sum += getFactorial(temp % 10);
}
return sum == n;
}
public static int getFactorionCount() {
int count = 0;
for (int i = 1; i <= Integer.MAX_VALUE; i++) {
if (isFactorian(i))
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(getFactorionCount());
}
}
Food for thought #1