java

Find Strong Numbers in Range()

Parameters: int start, int end

Start and end values for the range within which to find strong numbers

Returns: A list of strong numbers between the given range

This Java function takes in a range as input, and returns all the strong numbers within this range. A strong number is a number in which the sum of the factorial of its digits is equal to the number itself.

Variables
Loops (For Loop)
Conditionals (If Statement)
Mathematical Operations
Functions
Medium dificulty

Writing a Java Function to Find Strong Numbers in a Range

Hello there, programming enthusiast! This post will guide you through creating a function in Java that finds strong numbers within a given range. Strong numbers are those whose sum of factorial of digits equals the number itself. The concepts we'll touch on are loops, condition checks and factorization. Let's get to the fun part of coding!

Step 1

The first step is defining the helper function, factorial, which will find the factorial of a given number. Factorial of a number is the product of all positive integers less than or equal to that number.

public static int factorial(int num) {
    int fact = 1;
    for(int i = 1; i <= num; i++) {
        fact = fact * i;
    }
    return fact;
}

Step 2

Next, we define another helper function called isStrongNumber. This function will determine if a given number is a strong number. A strong number is a number in which the sum of the factorial of its digits is equal to the number itself.

public static boolean isStrongNumber(int num) {
    int temp = num, sum = 0;
    while(temp != 0) {
        int remainder = temp % 10;
        sum = sum + factorial(remainder);
        temp = temp / 10;
    }
    return sum == num;
}

Step 3

Now, we need to build the main function findStrongNumbersInRange which will find all the strong numbers within a given range. We traverse from the start to the end of the range and for each number, we check if it is a strong number using the isStrongNumber function. If it is, we add it to the array of strong numbers.

public static ArrayList<Integer> findStrongNumbersInRange(int start, int end) {
    ArrayList<Integer> strongNumbers = new ArrayList<>();
    for(int i = start; i <= end; i++) {
        if(isStrongNumber(i)) {
            strongNumbers.add(i);
        }
    }
    return strongNumbers;
}

Step 4

The final step would be to write a main function to test the above code. Here we will simply call the findStrongNumbersInRange function with a test range and print out the results.

public static void main(String[] args) {
    System.out.println(findStrongNumbersInRange(1, 500));
}

Conclusion

This approach uses a helper function to calculate the factorial of numbers, and another helper function to determine if a number is a strong number. Then a main function iterates over the given range and finds all the strong numbers within it. Here's the full implementation:

public class StrongNumber {
  public static int factorial(int num) {
    int fact = 1;
    for(int i = 1; i <= num; i++) 
        fact = fact * i;
    return fact;
  }

  public static boolean isStrongNumber(int num) {
    int temp = num, sum = 0;
    while(temp != 0) {
        int remainder = temp % 10;
        sum = sum + factorial(remainder);
        temp = temp / 10;
    }
    return sum == num;
  }

  public static ArrayList<Integer> findStrongNumbersInRange(int start, int end) {
    ArrayList<Integer> strongNumbers = new ArrayList<>();
    for(int i = start; i <= end; i++) {
        if(isStrongNumber(i)) 
            strongNumbers.add(i);
    }
    return strongNumbers;
  }

  public static void main(String[] args) {
    System.out.println(findStrongNumbersInRange(1, 500));
  }
}

So, following this approach, you can find all the strong numbers within a given range. You can also modify this code to suite your specific needs.

Learn function in:

Strong Number Identification

Identification of strong numbers within a provided range in java

Learn more

Mathematical principle

The concept of Strong Numbers involves Factorials, a key mathematical concept. A factorial of a number `n`, denoted as `n!`, is the product of all positive integers less than or equal to `n`. In the context of Strong Numbers, each digit in the number is replaced with its factorial, and if the sum of these factorials equals the original number, then it is a Strong Number. For example, `145` is a Strong Number as `1! + 4! + 5! = 145`.

Learn more