Java
 Computer >> コンピューター >  >> プログラミング >> Java

再帰を使用して数値の階乗を見つけるJavaプログラム


この記事では、再帰を使用して数値の階乗を見つける方法を理解します。数値の階乗は、それ自体とその小さい数値のそれぞれの積です。階乗は、ゼロより大きい自然数に適用される関数です。階乗関数の記号は、次のような数字の後の感嘆符です:5!

再帰関数は、特定の条件が満たされるまで、それ自体を複数回呼び出す関数です。再帰は、自己相似的な方法でアイテムを繰り返すプロセスです。プログラミング言語では、プログラムで同じ関数内の関数を呼び出すことができる場合、それは関数の再帰呼び出しと呼ばれます。

多くのプログラミング言語は、スタックを使用して再帰を実装します。一般に、関数(呼び出し元)が別の関数(呼び出し先)またはそれ自体を呼び出し先として呼び出すときはいつでも、呼び出し元関数は実行制御を呼び出し先に移します。この転送プロセスには、発信者から着信者に渡されるデータも含まれる場合があります。

以下は同じのデモンストレーションです-

入力

入力が-

であると仮定します
Enter the number : 7

出力

必要な出力は-

になります
The factorial of 7 is 5040

アルゴリズム

Step 1 - START
Step 2 - Declare an integer values namely ‘my_input’ and a long value namely ‘my_result’
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘factorial’ is defined which takes an integer as input and returns the product of the input and its previous number until the input value is reduced to 1.
Step 5 - The recursive function is called and the value ‘my_input’ is passed to it. Store the return value
Step 6 - Display the result
Step 7 - Stop

例1

ここでは、プロンプトに基づいてユーザーが入力を入力しています。この例は、コーディンググラウンドツールでライブで試すことができます 再帰を使用して数値の階乗を見つけるJavaプログラム

import java.util.Scanner;
public class Factorial {
   public static void main(String[] args) {
      int my_input ;
      long my_result;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      my_result = factorial(my_input);
      System.out.println("The factorial of " + my_input + " is " + my_result);
   }
   public static long factorial(int my_input){
      if (my_input >= 1)
         return my_input * factorial(my_input - 1);
      else
         return 1;
   }
}

出力

Required packages have been imported
A reader object has been defined
Enter the number : 7
The factorial of 7 is 5040

例2

ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。

public class Factorial {
   public static void main(String[] args) {
      int my_input ;
      long my_result;
      my_input = 7;
      System.out.println("The number is defined as " +my_input);
      my_result = factorial(my_input);
      System.out.println("The factorial of " + my_input + " is " + my_result);
   }
   public static long factorial(int my_input){
      if (my_input >= 1)
         return my_input * factorial(my_input - 1);
      else
         return 1;
   }
}

出力

The number is defined as 7
The factorial of 7 is 5040

  1. 再帰を使用して数値が素数であるか素数でないかを検出するPythonプログラム

    数値が素数であるかどうか、または再帰手法を使用していないかどうかを確認する必要がある場合は、メソッドが定義され、「while」条件が使用されます。 再帰は、より大きな問題の小さなビットの出力を計算し、これらのビットを組み合わせて、より大きな問題の解決策を提供します。 例 以下は同じのデモンストレーションです- def check_prime(my_num, my_val = None):    if my_val is None:       my_val = my_num – 1    while my_v

  2. Pythonで再帰を使用して数の階乗を見つける方法は?

    数値の階乗は、1からその数値までのすべての数値の積です。 関数がそれ自体を呼び出す場合、その関数は再帰関数と呼ばれます。 次のプログラムでは、factorial()関数は1つの引数を受け入れ、値が1に達するまで値を1つ減らして、それ自体を呼び出し続けます。 例 def factorial(x):     if x==1:         return 1     else:         return x*factorial(x-1) f=factorial(5) pr