কম্পিউটার টিউটোরিয়াল

জাভা প্রোগ্রাম: রিকার্শন ব্যবহার করে ফ্যাক্টোরিয়াল (Factorial) বের করা

ফ্যাক্টোরিয়াল (Factorial) কী?

একটি নন-নেগেটিভ ইন্টিজার n-এর ফ্যাক্টোরিয়াল (n!) হলো ১ থেকে n পর্যন্ত সব পূর্ণসংখ্যার গুণফল।

সংজ্ঞা: n! = n × (n-1) × (n-2) × ... × 2 × 1, এবং 0! = ১ (কনভেনশন)

উদাহরণ:

  • 5! = 5 × 4 × 3 × 2 × 1 = ১২০
  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = ৫০৪০
  • 0! = ১

ইনপুট ও আউটপুট উদাহরণ

ইনপুট:

Enter the number : 7

আউটপুট:

The factorial of 7 is 5040

অ্যালগরিদম

  1. প্রোগ্রাম শুরু করুন
  2. একটি ইন্টিজার (my_input) এবং একটি লং (my_result) ভেরিয়েবল ঘোষণা করুন
  3. ব্যবহারকারী থেকে সংখ্যা ইনপুট নিন
  4. রিকার্সিভ ফাংশন factorial(n) ডিফাইন করুন:
    • বেস কেস: যদি n <= 1 হয়, 1 রিটার্ন করুন
    • রিকার্সিভ কেস: n * factorial(n - 1) রিটার্ন করুন
  5. ফাংশন কল করুন এবং ফলাফল স্টোর করুন
  6. ফলাফল প্রদর্শন করুন
  7. প্রোগ্রাম শেষ করুন

উদাহরণ ১: ব্যবহারকারী ইনপুট নেওয়া

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;
    }
}

উদাহরণ ২: পূর্বনির্ধারিত মান ব্যবহার

public class Factorial {
    public static void main(String[] args) {
        int my_input = 7;
        long my_result;
        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;
    }
}

রিকার্শন ট্রেস (5!)

factorial(5)
= 5 * factorial(4)
= 5 * (4 * factorial(3))
= 5 * (4 * (3 * factorial(2)))
= 5 * (4 * (3 * (2 * factorial(1))))
= 5 * (4 * (3 * (2 * 1)))
= 5 * (4 * (3 * 2))
= 5 * (4 * 6)
= 5 * 24
= 120

কোড ব্যাখ্যা

  • long রিটার্ন টাইপ: ফ্যাক্টোরিয়াল খুব দ্রুত বড় হয়। 13! = 6,227,020,800 (Integer.MAX_VALUE অতিক্রম)
  • বেস কেস (n <= 1): 0! = 1 এবং 1! = 1 উভয় হ্যান্ডেল করে
  • স্ট্যাক ডেপথ: n লেভেল (n=20 হলে ২০ লেভেল - নিরাপদ)

Integer vs Long vs BigInteger লিমিট

টাইপম্যাক্স ভ্যালুম্যাক্স ফ্যাক্টোরিয়াল
int (32-bit)2,147,483,64712! = 479,001,600
long (64-bit)9,223,372,036,854,775,80720! = 2,432,902,008,176,640,000
BigIntegerঅনলিমিটেড (মেমোরি লিমিট)অসীম

BigInteger ব্যবহার করে অনলিমিটেড ফ্যাক্টোরিয়াল

import java.math.BigInteger;

public static BigInteger factorialBig(int n) {
    BigInteger result = BigInteger.ONE;
    for (int i = 2; i <= n; i++) {
        result = result.multiply(BigInteger.valueOf(i));
    }
    return result;
}

// রিকার্সিভ BigInteger
public static BigInteger factorialBigRecursive(int n) {
    if (n <= 1) return BigInteger.ONE;
    return BigInteger.valueOf(n).multiply(factorialBigRecursive(n - 1));
}

ইটারেটিভ সমাধান (প্রোডাকশন রেডি)

public static long factorialIterative(int n) {
    if (n < 0) throw new IllegalArgumentException("Negative not allowed");
    long result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

স্টার্লিং এর সन्नিকটন (Stirling's Approximation)

বড় n-এর জন্য অনুমানিক মান:

public static double stirlingApproximation(int n) {
    if (n == 0) return 1;
    return Math.sqrt(2 * Math.PI * n) * Math.pow(n / Math.E, n);
}

মেমোইজেশন (Memoization) দিয়ে অপ্টিমাইজেশন

import java.util.HashMap;
import java.util.Map;

public class FactorialMemoized {
    private static final Map cache = new HashMap<>();
    
    static {
        cache.put(0, 1L);
        cache.put(1, 1L);
    }
    
    public static long factorial(int n) {
        if (n < 0) throw new IllegalArgumentException();
        return cache.computeIfAbsent(n, k -> k * factorial(k - 1));
    }
}

বাস্তব প্রয়োগ

  • কম্বিনেশন (nCr) এবং পারমুটেশন (nPr) ক্যালকুলেশন
  • প্রোবাবিলিটি ও স্ট্যাটিস্টিক্স
  • টেইলর সিরিজ এক্সপ্যানশন (e^x, sin, cos)
  • অ্যালগরিদম কমপ্লেক্সিটি অ্যানালাইসিস

সতর্কতা

  • নেগেটিভ ইনপুট হ্যান্ডল করুন (IllegalArgumentException থ্রো)
  • n > 20 হলে long ওভারফ্লো হবে → BigInteger ব্যবহার করুন
  • রিকার্শন দিয়ে বড় n (১০০০০+) করলে StackOverflowError
  • প্রোডাকশনে ইটারেটিভ বা BigInteger ব্যবহার করুন
  1. পাইথনে বড় সংখ্যার ফ্যাক্টোরিয়াল বের করার প্রোগ্রাম

  2. পাইথন প্রোগ্রাম: রিকার্শন ছাড়া সংখ্যার ফ্যাক্টোরিয়াল বের করা

  3. পাইথন প্রোগ্রাম: রিকার্শন ব্যবহার করে সংখ্যা প্রাইম নাকি নন-প্রাইম চেক করা

  4. Python-এ রিকার্শন ব্যবহার করে ফ্যাক্টোরিয়াল বের করা