BigInteger ক্লাসটি Java.math প্যাকেজের একটি অংশ এবং এটি অত্যন্ত বড় পূর্ণসংখ্যার (integer) সঙ্গে কাজ করার জন্য ব্যবহৃত হয়। এটি এমন গাণিতিক গণনা সম্পাদন করতে সাহায্য করে যা int বা long টাইপের সীমা অতিক্রম করতে পারে। এর মধ্যে power, GCD (Greatest Common Divisor), এবং modulus operation এর মতো শক্তিশালী গাণিতিক অপারেশন সঞ্চালন করার জন্য বিশেষ মেথড রয়েছে।
এখানে আমরা BigInteger ক্লাস ব্যবহার করে Power, GCD, এবং Modulus Operation কিভাবে করা যায় তা উদাহরণসহ আলোচনা করব।
1. Power (Exponential Operation)
Power বা গুণনফল বের করার জন্য BigInteger.pow() মেথডটি ব্যবহৃত হয়, যা একটি BigInteger-কে একটি নির্দিষ্ট শক্তি বা এক্সপোনেন্টের মাধ্যমে গুণিত করে।
Syntax:
BigInteger pow(int exponent)
এটি BigInteger অবজেক্টের মানকে দেওয়া exponent (এক্সপোনেন্ট) এর শক্তিতে পরিণত করে।
Example:
import java.math.BigInteger;
public class BigIntegerPowerExample {
public static void main(String[] args) {
// Create BigInteger object
BigInteger base = new BigInteger("5");
// Power operation (5 raised to the power 3)
BigInteger result = base.pow(3);
System.out.println("5^3 = " + result);
}
}
Output:
5^3 = 125
ব্যাখ্যা:
BigInteger.pow(3)মেথড ব্যবহার করে ৫ এর ৩ শক্তিতে গুণ করা হয়েছে, যা ১২৫ প্রাপ্ত হয়েছে।
2. GCD (Greatest Common Divisor)
GCD বা Greatest Common Divisor দুটি পূর্ণসংখ্যার মধ্যে সর্বোচ্চ সাধারণ গুণনীয়ক বের করতে ব্যবহৃত হয়। BigInteger.gcd() মেথডটি দুইটি BigInteger-এর মধ্যে GCD বের করার জন্য ব্যবহৃত হয়।
Syntax:
BigInteger gcd(BigInteger val)
এটি দুইটি BigInteger এর মধ্যে সর্বোচ্চ সাধারণ গুণনীয়ক (GCD) বের করে।
Example:
import java.math.BigInteger;
public class BigIntegerGCDExample {
public static void main(String[] args) {
// Create BigInteger objects
BigInteger num1 = new BigInteger("56");
BigInteger num2 = new BigInteger("98");
// GCD operation
BigInteger gcdResult = num1.gcd(num2);
System.out.println("GCD of 56 and 98 is: " + gcdResult);
}
}
Output:
GCD of 56 and 98 is: 14
ব্যাখ্যা:
BigInteger.gcd(num2)মেথডের মাধ্যমে ৫৬ এবং ৯৮ এর GCD বের করা হয়েছে, যা ১৪।
3. Modulus Operation
Modulus অপারেশন, যা BigInteger.mod() মেথডের মাধ্যমে করা হয়, দুটি পূর্ণসংখ্যার মধ্যে ভাগফলে অবশিষ্টাংশ বের করে। এটি গণনা করে দেখে কতটুকু ভাগ বাকি থাকে।
Syntax:
BigInteger mod(BigInteger val)
এটি BigInteger এর মানের উপর modulus operation কার্যকর করে এবং একটি নতুন BigInteger রিটার্ন করে।
Example:
import java.math.BigInteger;
public class BigIntegerModExample {
public static void main(String[] args) {
// Create BigInteger objects
BigInteger num1 = new BigInteger("29");
BigInteger num2 = new BigInteger("5");
// Modulus operation (29 % 5)
BigInteger modulusResult = num1.mod(num2);
System.out.println("29 mod 5 = " + modulusResult);
}
}
Output:
29 mod 5 = 4
ব্যাখ্যা:
BigInteger.mod(num2)মেথডের মাধ্যমে ২৯ কে ৫ দ্বারা ভাগ করলে অবশিষ্টাংশ ৪ পাওয়া গেছে।
- Power Calculation:
BigInteger.pow()মেথড ব্যবহার করে বড় সংখ্যার শক্তি বের করা যায়, যেমন গাণিতিক কাজ যেখানে এক্সপোনেন্ট বা পাওয়ার প্রয়োজন।
- GCD (Greatest Common Divisor):
BigInteger.gcd()মেথডটি দুটিBigIntegerএর মধ্যে সর্বোচ্চ সাধারণ গুণনীয়ক বের করতে ব্যবহৃত হয়, যা গাণিতিক বিশ্লেষণ এবং সংখ্যা তত্ত্বে ব্যবহৃত হয়।
- Modulus Operation:
BigInteger.mod()মেথডের মাধ্যমে সংখ্যার ভাগফলে অবশিষ্টাংশ বের করা হয়, যা ক্রিপ্টোগ্রাফি এবং সংখ্যাগত সমস্যায় গুরুত্বপূর্ণ।
BigInteger ক্লাসটি বিশাল সংখ্যার সাথে কাজ করার জন্য অপরিহার্য, এবং এটি গাণিতিক অপারেশনগুলিকে সহজ ও কার্যকরীভাবে করতে সহায়তা করে, বিশেষত যখন খুব বড় বা সুনির্দিষ্ট মান নিয়ে কাজ করতে হয়।
Read more