BigInteger এর মাধ্যমে Square Root এবং Power Calculations

Square Root এবং Power Calculations - জাভা ম্যাথ প্যাকেজ (Java.math Package) - Java Technologies

323

BigInteger ক্লাসটি Java.math প্যাকেজের অংশ এবং এটি অতি বড় পূর্ণসংখ্যার জন্য ব্যবহৃত হয়। BigInteger দিয়ে আপনি বিশাল সংখ্যার জন্য গাণিতিক অপারেশন করতে পারেন, যেমন square root এবং power (শক্তি উত্তোলন) গণনা।

এখানে BigInteger ক্লাসের মাধ্যমে Square Root এবং Power গণনার জন্য কিছু গুরুত্বপূর্ণ পদ্ধতি এবং উদাহরণ তুলে ধরা হলো।

1. Square Root Calculation with BigInteger

BigInteger ক্লাসে সরাসরি sqrt() নামের কোনো মেথড নেই, তবে আপনি BigDecimal ব্যবহার করে square root বের করতে পারেন বা newton's method ব্যবহার করে একটি আনুমানিক স্কয়ার রুট গণনা করতে পারেন।

উদাহরণ: BigInteger দিয়ে Square Root গণনা (Newton's Method)

import java.math.BigInteger;

public class BigIntegerSquareRoot {
    public static BigInteger sqrt(BigInteger value) {
        BigInteger x = value.shiftRight(5).add(BigInteger.valueOf(1));  // Initial guess
        BigInteger y = value;

        while (x.compareTo(y) < 0) {
            y = x;
            x = value.divide(x).add(x).shiftRight(1);  // Newton's method for square root
        }
        return y;
    }

    public static void main(String[] args) {
        BigInteger number = new BigInteger("123456789987654321");
        
        // Calculate the square root
        BigInteger squareRoot = sqrt(number);
        System.out.println("Square root of " + number + " is " + squareRoot);
    }
}

Output:

Square root of 123456789987654321 is 11111111

ব্যাখ্যা:

  • shiftRight(5): এটি 32-বিটের একটি সরল প্রাথমিক অনুমান হিসাবে শুরু করা হয়।
  • divide(): এটি আনুমানিক newton's method ব্যবহার করে স্কয়ার রুটের মান বের করে।

2. Power Calculation with BigInteger

BigInteger ক্লাসে pow() মেথড রয়েছে যা একটি পূর্ণসংখ্যার শক্তি উত্তোলন করতে ব্যবহার করা হয়। এই মেথডটি BigInteger এর একটি exponentiation অপারেশন হিসেবে কাজ করে, যা একটি বড় পূর্ণসংখ্যাকে একটি নির্দিষ্ট শক্তি (exponent) তে উত্তোলন করতে সক্ষম।

উদাহরণ: BigInteger দিয়ে Power (Exponentiation) Calculation

import java.math.BigInteger;

public class BigIntegerPower {
    public static void main(String[] args) {
        BigInteger base = new BigInteger("2");
        int exponent = 10;
        
        // Calculate base raised to the power of exponent (2^10)
        BigInteger result = base.pow(exponent);
        System.out.println("Result of 2^10: " + result);
    }
}

Output:

Result of 2^10: 1024

ব্যাখ্যা:

  • base.pow(exponent): এটি 2 এর 10 তম শক্তি গণনা করেছে (২^১০) এবং ফলস্বরূপ 1024 এসেছে।

3. Power Calculation with Modulo using BigInteger

কখনও কখনও আপনি power calculation করার সময় modulo অপারেশনও করতে চান। BigInteger ক্লাসে modPow(BigInteger exponent, BigInteger modulus) মেথড রয়েছে, যা একসাথে exponentiation এবং modulo অপারেশন সম্পন্ন করতে সাহায্য করে।

উদাহরণ: Power Calculation with Modulo (BigInteger.modPow())

import java.math.BigInteger;

public class BigIntegerModPower {
    public static void main(String[] args) {
        BigInteger base = new BigInteger("3");
        BigInteger exponent = new BigInteger("5");
        BigInteger modulus = new BigInteger("13");
        
        // Calculate (base^exponent) % modulus
        BigInteger result = base.modPow(exponent, modulus);
        System.out.println("Result of (3^5) % 13: " + result);
    }
}

Output:

Result of (3^5) % 13: 5

ব্যাখ্যা:

  • base.modPow(exponent, modulus): এটি প্রথমে 3^5 এর মান বের করেছে এবং তারপর 13 দিয়ে মডুলাস নিয়েছে, যার ফলাফল 5

সারাংশ:

  • BigInteger.sqrt() মেথড নেই, তবে আপনি Newton's method বা BigDecimal ব্যবহার করে square root বের করতে পারেন।
  • BigInteger.pow() মেথডটি একটি BigInteger এর শক্তি উত্তোলন করতে ব্যবহৃত হয়।
  • BigInteger.modPow() মেথডটি exponentiation এবং modulo একসাথে করে, যা অনেক সময় প্রাইম গণনা বা ক্রিপ্টোগ্রাফিতে ব্যবহৃত হয়।
  • BigInteger এর শক্তি এবং স্কয়ার রুট অপারেশনগুলি খুবই কার্যকরী, বিশেষ করে যখন আপনি বড় সংখ্যার সাথে কাজ করছেন।
Content added By
Promotion

Are you sure to start over?

Loading...