How to resolve the algorithm Factorions step by step in the Java programming language
How to resolve the algorithm Factorions step by step in the Java programming language
Table of Contents
Problem Statement
A factorion is a natural number that equals the sum of the factorials of its digits.
145 is a factorion in base 10 because:
It can be shown (see talk page) that no factorion in base 10 can exceed 1,499,999.
Write a program in your language to demonstrate, by calculating and printing out the factorions, that:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Factorions step by step in the Java programming language
The provided Java code contains a main method that executes a series of operations on different bases, including 9, 10, 11, and 12. It identifies "factorial numbers," which are numbers that are equal to the sum of the factorials of their digits in the given base.
Here's a step-by-step breakdown of the code:
-
The main method initializes four loops, each ranging from 1 to 1499999, to search for factorial numbers in different bases.
-
Within each loop, it converts the integer i to a string iStri and calls the operate method with iStri and the corresponding base.
-
The operate method takes a string representing a number in a specific base and calculates the sum of the factorials of its digits.
-
Inside the operate method, another method called factorialRec is used to calculate the factorial of each digit in the given base. The factorialRec method uses recursion to compute the factorial of a number.
-
The operate method also uses a helper method called fromDeci to convert the given number from the base 10 to the specified base. The fromDeci method is adapted from Geeks for Geeks and converts a decimal number to any arbitrary base.
-
The operate method iterates through the string representation of the number in the given base and calculates the sum of the factorials of its digits.
-
If the calculated sum is equal to the original number, the number is identified as a "factorial number" and is printed to the console.
Overall, the code searches for "factorial numbers" in bases 9, 10, 11, and 12 and prints the identified numbers to the console.
Source code in the java programming language
public class Factorion {
public static void main(String [] args){
System.out.println("Base 9:");
for(int i = 1; i <= 1499999; i++){
String iStri = String.valueOf(i);
int multiplied = operate(iStri,9);
if(multiplied == i){
System.out.print(i + "\t");
}
}
System.out.println("\nBase 10:");
for(int i = 1; i <= 1499999; i++){
String iStri = String.valueOf(i);
int multiplied = operate(iStri,10);
if(multiplied == i){
System.out.print(i + "\t");
}
}
System.out.println("\nBase 11:");
for(int i = 1; i <= 1499999; i++){
String iStri = String.valueOf(i);
int multiplied = operate(iStri,11);
if(multiplied == i){
System.out.print(i + "\t");
}
}
System.out.println("\nBase 12:");
for(int i = 1; i <= 1499999; i++){
String iStri = String.valueOf(i);
int multiplied = operate(iStri,12);
if(multiplied == i){
System.out.print(i + "\t");
}
}
}
public static int factorialRec(int n){
int result = 1;
return n == 0 ? result : result * n * factorialRec(n-1);
}
public static int operate(String s, int base){
int sum = 0;
String strx = fromDeci(base, Integer.parseInt(s));
for(int i = 0; i < strx.length(); i++){
if(strx.charAt(i) == 'A'){
sum += factorialRec(10);
}else if(strx.charAt(i) == 'B') {
sum += factorialRec(11);
}else if(strx.charAt(i) == 'C') {
sum += factorialRec(12);
}else {
sum += factorialRec(Integer.parseInt(String.valueOf(strx.charAt(i)), base));
}
}
return sum;
}
// Ln 57-71 from Geeks for Geeks @ https://www.geeksforgeeks.org/convert-base-decimal-vice-versa/
static char reVal(int num) {
if (num >= 0 && num <= 9)
return (char)(num + 48);
else
return (char)(num - 10 + 65);
}
static String fromDeci(int base, int num){
StringBuilder s = new StringBuilder();
while (num > 0) {
s.append(reVal(num % base));
num /= base;
}
return new String(new StringBuilder(s).reverse());
}
}
You may also check:How to resolve the algorithm Yin and yang step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/Wrong ranges step by step in the Java programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Sidef programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Pascal programming language
You may also check:How to resolve the algorithm Pascal matrix generation step by step in the CLU programming language