BigInteger এর সাথে Arithmetic Operations গাইড ও নোট

Java Technologies - জাভা ম্যাথ প্যাকেজ (Java.math Package)
385

BigInteger ক্লাসটি Java.math প্যাকেজের একটি গুরুত্বপূর্ণ অংশ, যা বৃহৎ পূর্ণসংখ্যা (large integers) পরিচালনার জন্য ব্যবহৃত হয়। এটি সাধারণ int বা long টাইপের মানের তুলনায় আরও বড় সংখ্যার জন্য উপযুক্ত, কারণ BigInteger ক্লাসটি arbitrary-precision (অসীম সঠিকতা) সংখ্যা পরিচালনা করতে সক্ষম।

BigInteger ক্লাসের মাধ্যমে আপনি বড় সংখ্যার গাণিতিক অপারেশন যেমন যোগ, বিয়োগ, গুণ, ভাগ, মডুলাস ইত্যাদি কার্যকরভাবে পরিচালনা করতে পারেন। এর মধ্যে রয়েছে অনেক পদ্ধতি যা উচ্চ সঠিকতার গাণিতিক হিসাব নিশ্চিত করে।

BigInteger ক্লাসের প্রধান গাণিতিক অপারেশনসমূহ:

  1. Addition (add)
  2. Subtraction (subtract)
  3. Multiplication (multiply)
  4. Division (divide)
  5. Modulus (mod)
  6. Power (pow)
  7. GCD (Greatest Common Divisor) (gcd)
  8. Modular Exponentiation (modPow)

BigInteger এর গাণিতিক অপারেশন উদাহরণ:

1. Addition (যোগ)

import java.math.BigInteger;

public class BigIntegerAddition {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("987654321098765432109876543210");

        // Performing addition
        BigInteger sum = num1.add(num2);
        System.out.println("Sum: " + sum);
    }
}

Output:

Sum: 1111111110111111111011111111100

ব্যাখ্যা:

  • add() মেথডটি দুটি BigInteger অবজেক্টের যোগফল প্রদান করে।

2. Subtraction (বিয়োগ)

import java.math.BigInteger;

public class BigIntegerSubtraction {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("987654321098765432109876543210");
        BigInteger num2 = new BigInteger("123456789012345678901234567890");

        // Performing subtraction
        BigInteger difference = num1.subtract(num2);
        System.out.println("Difference: " + difference);
    }
}

Output:

Difference: 864197532086419753208641975320

ব্যাখ্যা:

  • subtract() মেথডটি দুটি BigInteger অবজেক্টের বিয়োগফল প্রদান করে।

3. Multiplication (গুণফল)

import java.math.BigInteger;

public class BigIntegerMultiplication {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("123456789");
        BigInteger num2 = new BigInteger("987654321");

        // Performing multiplication
        BigInteger product = num1.multiply(num2);
        System.out.println("Product: " + product);
    }
}

Output:

Product: 121932631112635269

ব্যাখ্যা:

  • multiply() মেথডটি দুটি BigInteger অবজেক্টের গুণফল প্রদান করে।

4. Division (ভাগ)

import java.math.BigInteger;

public class BigIntegerDivision {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("100000000");

        // Performing division
        BigInteger quotient = num1.divide(num2);
        System.out.println("Quotient: " + quotient);
    }
}

Output:

Quotient: 1234567890

ব্যাখ্যা:

  • divide() মেথডটি দুটি BigInteger অবজেক্টের ভাগফল প্রদান করে।

5. Modulus (মডুলাস)

import java.math.BigInteger;

public class BigIntegerModulus {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("100000000");

        // Performing modulus
        BigInteger remainder = num1.mod(num2);
        System.out.println("Remainder: " + remainder);
    }
}

Output:

Remainder: 23456790

ব্যাখ্যা:

  • mod() মেথডটি দুটি BigInteger অবজেক্টের ভাগফলে অবশিষ্টাংশ (remainder) প্রদান করে।

6. Power (শক্তি)

import java.math.BigInteger;

public class BigIntegerPower {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num = new BigInteger("2");

        // Calculating power (2^10)
        BigInteger powerResult = num.pow(10);
        System.out.println("2^10: " + powerResult);
    }
}

Output:

2^10: 1024

ব্যাখ্যা:

  • pow() মেথডটি BigInteger অবজেক্টের শক্তি (power) গণনা করে। এখানে 2 এর 10 তম শক্তি গণনা করা হয়েছে।

7. GCD (Greatest Common Divisor)

gcd() মেথডটি দুটি বড় সংখ্যার মধ্যে সর্বাধিক সাধারণ গুণক (GCD) গণনা করে।

import java.math.BigInteger;

public class BigIntegerGCD {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger num1 = new BigInteger("123456789");
        BigInteger num2 = new BigInteger("987654321");

        // Calculating GCD
        BigInteger gcdResult = num1.gcd(num2);
        System.out.println("GCD: " + gcdResult);
    }
}

Output:

GCD: 9

ব্যাখ্যা:

  • gcd() মেথডটি দুটি BigInteger অবজেক্টের মধ্যে সর্বাধিক সাধারণ গুণক (Greatest Common Divisor) প্রদান করে।

8. Modular Exponentiation (মডুলার এক্সপোনেনশিয়েশন)

import java.math.BigInteger;

public class BigIntegerModularExponentiation {
    public static void main(String[] args) {
        // Creating BigInteger objects
        BigInteger base = new BigInteger("5");
        BigInteger exponent = new BigInteger("3");
        BigInteger modulus = new BigInteger("13");

        // Calculating modular exponentiation (5^3 % 13)
        BigInteger modExpResult = base.modPow(exponent, modulus);
        System.out.println("5^3 % 13: " + modExpResult);
    }
}

Output:

5^3 % 13: 8

ব্যাখ্যা:

  • modPow() মেথডটি একটি modular exponentiation গণনা করে, অর্থাৎ (base^exponent) % modulus। এখানে 5 এর 3 তম শক্তি 13 দিয়ে ভাগ করার পর যে অবশিষ্টাংশ (remainder) পাওয়া যায় তা গণনা করা হয়েছে।

সারাংশ:

BigInteger ক্লাসটি বড় পূর্ণসংখ্যার গাণিতিক অপারেশন সহজে পরিচালনা করতে সহায়ক। এখানে যে মেথডগুলির ব্যবহার দেখানো হয়েছে, সেগুলি বড় সংখ্যার যোগ, বিয়োগ, গুণ, ভাগ, মডুলাস, গণনা শক্তি, GCD, এবং modular exponentiation অপারেশনগুলো সঠিকভাবে এবং নির্ভুলভাবে সম্পাদন করতে সহায়ক।

  • add(): দুটি BigInteger যোগফল দেয়।
  • subtract(): দুটি BigInteger বিয়োগফল দেয়।
  • multiply(): দুটি BigInteger গুণফল দেয়।
  • divide(): দুটি BigInteger ভাগফল দেয়।
  • mod(): মডুলাস বা ভাগফলে অবশিষ্টাংশ দেয়।
  • gcd(): দুটি BigInteger এর সর্বাধিক সাধারণ গুণক (GCD) প্রদান করে।
  • modPow(): modular exponentiation এর জন্য ব্যবহৃত হয়।

BigInteger ক্লাসটি বৃহৎ সংখ্যার গাণিতিক হিসাবের জন্য অত্যন্ত উপযোগী এবং এতে কোনো নির্দিষ্ট আকারের সীমাবদ্ধতা নেই, যা ছোট ধরনের পূর্ণসংখ্যার জন্য সম্ভব নয়।

Content added By

BigInteger এর সাথে Addition, Subtraction, Multiplication, এবং Division

352

BigInteger ক্লাসটি Java.math প্যাকেজের একটি গুরুত্বপূর্ণ ক্লাস, যা খুব বড় পূর্ণসংখ্যা (integers) নিয়ে কাজ করার জন্য ব্যবহৃত হয়। এটি অগণিত সঠিকতা (arbitrary precision) প্রদান করে, যার মানে হল যে BigInteger দিয়ে আপনি যে কোনো আকারের পূর্ণসংখ্যার গাণিতিক অপারেশন করতে পারবেন, যা int বা long এর সীমা অতিক্রম করতে পারে।

BigInteger ক্লাসের সাহায্যে আপনি গাণিতিক অপারেশনগুলো যেমন যোগ, বিয়োগ, গুণ, ভাগ ইত্যাদি বড় সংখ্যার জন্য করতে পারেন।

BigInteger ক্লাসের ব্যবহার:

1. BigInteger ক্লাসের ইনস্ট্যান্স তৈরি করা

BigInteger তৈরি করার জন্য দুটি প্রধান উপায় রয়েছে:

  • String দিয়ে ইনস্ট্যান্স তৈরি করা
  • int/long দ্বারা ইনস্ট্যান্স তৈরি করা
import java.math.BigInteger;

public class BigIntegerExample {
    public static void main(String[] args) {
        // Create BigInteger objects from String
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("987654321098765432109876543210");

        // Create BigInteger objects from long or int
        BigInteger num3 = BigInteger.valueOf(1000000L);
        BigInteger num4 = BigInteger.valueOf(5000000L);

        // Print out the BigInteger values
        System.out.println("BigInteger num1: " + num1);
        System.out.println("BigInteger num2: " + num2);
        System.out.println("BigInteger num3: " + num3);
        System.out.println("BigInteger num4: " + num4);
    }
}

Output:

BigInteger num1: 123456789012345678901234567890
BigInteger num2: 987654321098765432109876543210
BigInteger num3: 1000000
BigInteger num4: 5000000

2. Addition (যোগ) with BigInteger

BigInteger.add(BigInteger) মেথডটি দুইটি BigInteger সংখ্যাকে যোগ করার জন্য ব্যবহৃত হয়।

import java.math.BigInteger;

public class BigIntegerAddition {
    public static void main(String[] args) {
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("987654321098765432109876543210");

        // Addition
        BigInteger sum = num1.add(num2);
        System.out.println("Sum: " + sum);
    }
}

Output:

Sum: 1111111110111111111011111111100

ব্যাখ্যা:

  • add() মেথড দুটি BigInteger অবজেক্ট যোগ করে একটি নতুন BigInteger রিটার্ন করে।

3. Subtraction (বিয়োগ) with BigInteger

BigInteger.subtract(BigInteger) মেথডটি দুটি BigInteger সংখ্যার মধ্যে বিয়োগ করার জন্য ব্যবহৃত হয়।

import java.math.BigInteger;

public class BigIntegerSubtraction {
    public static void main(String[] args) {
        BigInteger num1 = new BigInteger("987654321098765432109876543210");
        BigInteger num2 = new BigInteger("123456789012345678901234567890");

        // Subtraction
        BigInteger difference = num1.subtract(num2);
        System.out.println("Difference: " + difference);
    }
}

Output:

Difference: 864197532086419753208641975320

ব্যাখ্যা:

  • subtract() মেথড দুটি BigInteger অবজেক্টের মধ্যে বিয়োগ অপারেশন সম্পাদন করে।

4. Multiplication (গুণ) with BigInteger

BigInteger.multiply(BigInteger) মেথডটি দুটি BigInteger সংখ্যাকে গুণ করার জন্য ব্যবহৃত হয়।

import java.math.BigInteger;

public class BigIntegerMultiplication {
    public static void main(String[] args) {
        BigInteger num1 = new BigInteger("123456789012345678901234567890");
        BigInteger num2 = new BigInteger("987654321098765432109876543210");

        // Multiplication
        BigInteger product = num1.multiply(num2);
        System.out.println("Product: " + product);
    }
}

Output:

Product: 121932631137021795226305170269409090211114380564702343445628259885000

ব্যাখ্যা:

  • multiply() মেথড দুটি BigInteger অবজেক্টের গুণফল প্রদান করে।

5. Division (ভাগ) with BigInteger

BigInteger.divide(BigInteger) মেথডটি দুটি BigInteger সংখ্যার মধ্যে ভাগ করার জন্য ব্যবহৃত হয়। তবে, ভাগের সময় সঠিকভাবে ফলাফল পাওয়ার জন্য divide() মেথডটি অতিরিক্ত নির্দিষ্ট রাউন্ডিং সিস্টেম প্রয়োগ করতে পারে।

import java.math.BigInteger;

public class BigIntegerDivision {
    public static void main(String[] args) {
        BigInteger num1 = new BigInteger("987654321098765432109876543210");
        BigInteger num2 = new BigInteger("123456789012345678901234567890");

        // Division
        BigInteger quotient = num1.divide(num2);
        System.out.println("Quotient: " + quotient);
    }
}

Output:

Quotient: 8

ব্যাখ্যা:

  • divide() মেথডটি BigInteger সংখ্যা দুটি ভাগ করে একটি পূর্ণসংখ্যা (integer) রিটার্ন করে। যদি ভাগের ফলস্বরূপ দশমিক থাকে, তবে এটি decimal truncation (ডিসিমাল স্থান বাদ দেয়া) করতে পারে, যেহেতু BigInteger শুধুমাত্র পূর্ণসংখ্যা (integers) নিয়ে কাজ করে।

6. Handling Division with Rounding (ভাগের ক্ষেত্রে রাউন্ডিং)

যদি আপনি BigDecimal এর মতো BigInteger-এও ভাগের সময় রাউন্ডিং করতে চান, তবে BigDecimal ব্যবহার করতে হবে, কারণ BigInteger শুধুমাত্র পূর্ণসংখ্যা সমর্থন করে।

import java.math.BigDecimal;

public class BigDecimalDivisionWithRounding {
    public static void main(String[] args) {
        BigDecimal num1 = new BigDecimal("987654321098765432109876543210");
        BigDecimal num2 = new BigDecimal("123456789012345678901234567890");

        // Division with rounding
        BigDecimal quotient = num1.divide(num2, 2, BigDecimal.ROUND_HALF_UP); // Rounding to 2 decimal places
        System.out.println("Quotient with rounding: " + quotient);
    }
}

Output:

Quotient with rounding: 8.00

  • BigInteger ক্লাসটি অগণিত সঠিকতা এবং বড় পূর্ণসংখ্যা নিয়ে কাজ করার জন্য গুরুত্বপূর্ণ।
  • এটি add(), subtract(), multiply(), এবং divide() মেথডগুলির মাধ্যমে বড় সংখ্যাগুলির গাণিতিক অপারেশন পরিচালনা করতে সহায়তা করে।
  • BigInteger এর সাহায্যে আপনি বৃহৎ গাণিতিক কাজ যেমন ক্রিপ্টোগ্রাফি, বিজ্ঞান বা উচ্চ নির্ভুলতার জন্য সঠিক গণনা করতে পারেন।
Content added By

BigInteger এর মাধ্যমে Power, GCD (Greatest Common Divisor), এবং Modulus Operation

308

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) মেথডের মাধ্যমে ২৯ কে ৫ দ্বারা ভাগ করলে অবশিষ্টাংশ পাওয়া গেছে।

  1. Power Calculation:
    • BigInteger.pow() মেথড ব্যবহার করে বড় সংখ্যার শক্তি বের করা যায়, যেমন গাণিতিক কাজ যেখানে এক্সপোনেন্ট বা পাওয়ার প্রয়োজন।
  2. GCD (Greatest Common Divisor):
    • BigInteger.gcd() মেথডটি দুটি BigInteger এর মধ্যে সর্বোচ্চ সাধারণ গুণনীয়ক বের করতে ব্যবহৃত হয়, যা গাণিতিক বিশ্লেষণ এবং সংখ্যা তত্ত্বে ব্যবহৃত হয়।
  3. Modulus Operation:
    • BigInteger.mod() মেথডের মাধ্যমে সংখ্যার ভাগফলে অবশিষ্টাংশ বের করা হয়, যা ক্রিপ্টোগ্রাফি এবং সংখ্যাগত সমস্যায় গুরুত্বপূর্ণ।

BigInteger ক্লাসটি বিশাল সংখ্যার সাথে কাজ করার জন্য অপরিহার্য, এবং এটি গাণিতিক অপারেশনগুলিকে সহজ ও কার্যকরীভাবে করতে সহায়তা করে, বিশেষত যখন খুব বড় বা সুনির্দিষ্ট মান নিয়ে কাজ করতে হয়।

Content added By

BigInteger এর সঙ্গে Exponentiation এবং Modulo Operations

333

BigInteger ক্লাসটি java.math প্যাকেজের একটি গুরুত্বপূর্ণ অংশ যা arbitrary precision integers (অতিসাধারণ দৈর্ঘ্যের পূর্ণসংখ্যা) পরিচালনা করতে সক্ষম। এটি এমন বড় সংখ্যার জন্য ব্যবহৃত হয় যেগুলি সাধারণ int, long, বা double টাইপে ফিট হয় না। BigInteger দিয়ে আপনি exponentiation (যত বড় শক্তি) এবং modulo operations (মডুলাস অপারেশন) করতে পারেন।

BigInteger এর মাধ্যমে Exponentiation এবং Modulo Operations:

Exponentiation (যত বড় শক্তি) এবং Modulo (মডুলাস) অপারেশনগুলো গাণিতিক অপারেশন হিসাবে অনেক গুরুত্বপূর্ণ, এবং BigInteger ক্লাসে এই অপারেশনগুলো করার জন্য মেথড সরবরাহ করা হয়েছে।

  1. Exponentiation (শক্তি উত্তোলন):
    • BigInteger.pow(int exponent) মেথডটি একটি পূর্ণসংখ্যার শক্তি (exponentiation) বের করতে ব্যবহৃত হয়।
  2. Modulo (মডুলাস):
    • BigInteger.mod(BigInteger m) মেথডটি একটি সংখ্যাকে অন্য একটি বড় সংখ্যার মডুলাস (baki) বের করে।
    • BigInteger.modPow(BigInteger exponent, BigInteger m) মেথডটি দুটি অপারেশন একসাথে সম্পাদন করে, অর্থাৎ exponentiation এবং modulo একই সময় করে।

1. Exponentiation (BigInteger.pow() ব্যবহার)

BigInteger.pow(int exponent) মেথডের মাধ্যমে আপনি একটি BigInteger এর উপর শক্তি উত্তোলন করতে পারেন।

উদাহরণ: Exponentiation ব্যবহার করে:

import java.math.BigInteger;

public class ExponentiationExample {
    public static void main(String[] args) {
        // Create a BigInteger object
        BigInteger base = new BigInteger("2");

        // Calculate base raised to the power of 10 (2^10)
        BigInteger result = base.pow(10);
        System.out.println("2^10 = " + result);
    }
}

ব্যাখ্যা:

  • base.pow(10): এটি 2 এর দশম শক্তি গণনা করেছে (২^১০)।

আউটপুট:

2^10 = 1024

2. Modulo Operation (BigInteger.mod() ব্যবহার)

BigInteger.mod(BigInteger m) মেথডটি একটি বড় সংখ্যা m দিয়ে অন্য একটি বড় সংখ্যা (অথবা BigInteger) ভাগ করে এর মডুলাস বের করে।

উদাহরণ: Modulo Operation ব্যবহার করে:

import java.math.BigInteger;

public class ModuloExample {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger number = new BigInteger("123456789012345678901234567890");
        BigInteger divisor = new BigInteger("100000");

        // Calculate the modulus (number % divisor)
        BigInteger remainder = number.mod(divisor);
        System.out.println("Modulo (remainder): " + remainder);
    }
}

ব্যাখ্যা:

  • number.mod(divisor): এটি 123456789012345678901234567890 সংখ্যা কে 100000 দিয়ে ভাগ করে তার baki বের করে।

আউটপুট:

Modulo (remainder): 23490

3. Modulo Exponentiation (BigInteger.modPow() ব্যবহার)

BigInteger.modPow(BigInteger exponent, BigInteger m) মেথডটি exponentiation এবং modulo অপারেশন একসাথে সম্পন্ন করে। এটি (base^exponent) % m হিসাব করে।

উদাহরণ: Modulo Exponentiation ব্যবহার করে:

import java.math.BigInteger;

public class ModuloExponentiationExample {
    public static void main(String[] args) {
        // Create BigInteger objects for base, exponent, and modulus
        BigInteger base = new BigInteger("2");
        BigInteger exponent = new BigInteger("10");
        BigInteger modulus = new BigInteger("1000");

        // Perform modulo exponentiation (base^exponent) % modulus
        BigInteger result = base.modPow(exponent, modulus);
        System.out.println("Result of (2^10) % 1000 = " + result);
    }
}

ব্যাখ্যা:

  • base.modPow(exponent, modulus): এখানে আমরা 2^10 এর মান বের করেছি এবং তারপর তার 1000 দিয়ে মডুলাস নিয়েছি।

আউটপুট:

Result of (2^10) % 1000 = 24

4. বড় সংখ্যার গাণিতিক অপারেশন (যেমন গুণফল, ভাগফল, ইত্যাদি)

BigInteger ক্লাসে গুণফল, ভাগফল, যোগফল, বিয়োগফল ইত্যাদি গাণিতিক অপারেশন করা যায় খুব সহজে। এখানে BigInteger.modPow() ব্যবহার করে শক্তি উত্তোলন এবং মডুলাস একসাথে করা হয়েছে।

উদাহরণ: বড় সংখ্যা গুণফল এবং মডুলাস:

import java.math.BigInteger;

public class BigIntegerOperations {
    public static void main(String[] args) {
        // Create two BigInteger objects
        BigInteger num1 = new BigInteger("987654321987654321987654321987654321987654321");
        BigInteger num2 = new BigInteger("123456789123456789123456789123456789123456789");

        // Multiply two BigIntegers
        BigInteger product = num1.multiply(num2);
        System.out.println("Product: " + product);

        // Modulo of the product with another BigInteger
        BigInteger modulus = product.mod(new BigInteger("1000000000"));
        System.out.println("Product mod 1000000000: " + modulus);
    }
}

আউটপুট:

Product: 121932631137021795223746380113865467141306139269083121620825501760196421070000
Product mod 1000000000: 453212345
  • BigInteger হল একটি অত্যন্ত শক্তিশালী ক্লাস যা অবাধ সঠিকতা এবং বড় সংখ্যার গাণিতিক অপারেশন করতে সক্ষম।
  • Exponentiation (শক্তি উত্তোলন) এবং Modulo Operations (মডুলাস) এর জন্য BigInteger.pow(), BigInteger.mod(), এবং BigInteger.modPow() মেথড ব্যবহার করা হয়।
  • Modulo Exponentiation বিশেষভাবে ক্রিপ্টোগ্রাফি এবং নিরাপত্তা সংক্রান্ত কাজগুলোর জন্য ব্যবহারিক।
Content added By

উদাহরণ সহ BigInteger Arithmetic Operations

258

BigInteger ক্লাসটি java.math প্যাকেজের একটি অংশ যা অবিচ্ছিন্ন সঠিকতার (arbitrary precision) পূর্ণসংখ্যার গণনা করতে ব্যবহৃত হয়। এটি সাধারণ int এবং long টাইপের তুলনায় অনেক বড় সংখ্যার জন্য কাজ করে এবং বিভিন্ন গাণিতিক অপারেশন যেমন যোগ, বিয়োগ, গুণ এবং ভাগ সমর্থন করে।

এখানে BigInteger ক্লাসের গাণিতিক অপারেশন (Arithmetic Operations) সম্পর্কিত উদাহরণ দেওয়া হলো।

BigInteger Arithmetic Operations উদাহরণ

1. BigInteger যোগফল (Addition)

import java.math.BigInteger;

public class BigIntegerAddition {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("123456789123456789123456789123456789");
        BigInteger number2 = new BigInteger("987654321987654321987654321987654321");

        // Adding two BigInteger numbers
        BigInteger sum = number1.add(number2);
        System.out.println("Sum: " + sum);
    }
}

আউটপুট:

Sum: 1111111111111111111111111111111111110

ব্যাখ্যা:

  • add(BigInteger val): দুটি BigInteger অবজেক্ট যোগ করার জন্য add() মেথড ব্যবহার করা হয়েছে।

2. BigInteger বিয়োগফল (Subtraction)

import java.math.BigInteger;

public class BigIntegerSubtraction {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789123456789123456789123456789");

        // Subtracting two BigInteger numbers
        BigInteger difference = number1.subtract(number2);
        System.out.println("Difference: " + difference);
    }
}

আউটপুট:

Difference: 864197532864197532864197532864197532

ব্যাখ্যা:

  • subtract(BigInteger val): দুটি BigInteger অবজেক্টের বিয়োগফল বের করার জন্য subtract() মেথড ব্যবহার করা হয়েছে।

3. BigInteger গুণফল (Multiplication)

import java.math.BigInteger;

public class BigIntegerMultiplication {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("123456789");
        BigInteger number2 = new BigInteger("987654321");

        // Multiplying two BigInteger numbers
        BigInteger product = number1.multiply(number2);
        System.out.println("Product: " + product);
    }
}

আউটপুট:

Product: 121932631112635269

ব্যাখ্যা:

  • multiply(BigInteger val): দুটি BigInteger অবজেক্ট গুণফল বের করার জন্য multiply() মেথড ব্যবহার করা হয়েছে।

4. BigInteger ভাগফল (Division)

import java.math.BigInteger;

public class BigIntegerDivision {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789");

        // Dividing two BigInteger numbers
        BigInteger quotient = number1.divide(number2);
        System.out.println("Quotient: " + quotient);
    }
}

আউটপুট:

Quotient: 8000000000

ব্যাখ্যা:

  • divide(BigInteger val): দুটি BigInteger অবজেক্টের ভাগফল বের করার জন্য divide() মেথড ব্যবহার করা হয়েছে।

5. BigInteger মোড (Modulus)

import java.math.BigInteger;

public class BigIntegerModulus {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789");

        // Modulo operation on BigInteger
        BigInteger modulusResult = number1.mod(number2);
        System.out.println("Modulus: " + modulusResult);
    }
}

আউটপুট:

Modulus: 8000000000

ব্যাখ্যা:

  • mod(BigInteger m): এটি BigInteger অবজেক্টে আরেকটি BigInteger এর মডুলাস (বাকি) বের করার জন্য ব্যবহৃত হয়।

6. BigInteger পাওয়ার (Exponentiation)

import java.math.BigInteger;

public class BigIntegerExponentiation {
    public static void main(String[] args) {
        // Creating a BigInteger object
        BigInteger number = new BigInteger("2");

        // Raising the number to the power of 10
        BigInteger result = number.pow(10);
        System.out.println("2 to the power of 10: " + result);
    }
}

আউটপুট:

2 to the power of 10: 1024

ব্যাখ্যা:

  • pow(int exponent): এটি একটি বড় সংখ্যাকে নির্দিষ্ট পদ্ধতিতে উত্তোলন করে (যেমন কিউব বা বর্গমূল)।

7. BigInteger প্রাইম চেক (Prime Check)

import java.math.BigInteger;

public class BigIntegerPrimeCheck {
    public static void main(String[] args) {
        // Creating a BigInteger object
        BigInteger number = new BigInteger("131");

        // Check if the number is prime
        boolean isPrime = number.isProbablePrime(1); // Certainty factor = 1
        System.out.println("Is the number prime? " + isPrime);
    }
}

আউটপুট:

Is the number prime? true

ব্যাখ্যা:

  • isProbablePrime(int certainty): এটি একটি বড় সংখ্যার প্রাইম চেক করার ফাংশন। certainty হল একটি নির্ভরযোগ্যতা পরিমাণ, যেটি 0 থেকে 100 পর্যন্ত হতে পারে।

BigInteger এর অন্যান্য কার্যকারিতা:

  • gcd(BigInteger val): দুটি সংখ্যার গরিষ্ঠ সাধারণ গুণফল বের করে।
  • lcm(BigInteger val): দুটি সংখ্যার সর্বনিম্ন সাধারণ গুণফল বের করে।
  • abs(): সংখ্যার আবসোলিউট মান (অথবা ধনাত্মক মান) বের করে।

সারাংশ:

  • BigInteger হল একটি শক্তিশালী ক্লাস যা অসীম দৈর্ঘ্যের পূর্ণসংখ্যা হিসাব করতে সহায়তা করে এবং বিভিন্ন গাণিতিক অপারেশন যেমন যোগফল, বিয়োগফল, গুণফল, ভাগফল, পাওয়ার, প্রাইম চেক ইত্যাদি পরিচালনা করতে পারে।
  • এটি গাণিতিক এবং অর্থনৈতিক অ্যাপ্লিকেশনগুলিতে ব্যবহৃত হয় যেখানে বড় সংখ্যার প্রিসিশন বা সঠিকতা প্রয়োজন।
  • BigInteger ইমিউটেবল (immutable) হওয়ায় এর মান পরিবর্তন করা সম্ভব নয় এবং এটি thread-safe
Content added By
Promotion

Are you sure to start over?

Loading...