Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การหารเลขยกกำลังฐานเดียวกันใน C#

กฎเลขยกกำลัง: การหารฐานเดียวกัน

กฎคณิตศาสตร์: a^m ÷ a^n = a^(m-n) — หารเลขยกกำลังฐานเดียวกัน = ฐานเดิม ยกกำลังเท่ากับผลต่างของเลขชี้กำลัง

ตัวอย่างโค้ด

using System;

class Demo
{
    static void Main()
    {
        double n = 10;     // ฐาน
        double e1 = 10;    // เลขชี้กำลังตัวเศษ
        double e2 = 8;     // เลขชี้กำลังตัวส่วน

        // กฎ: n^e1 / n^e2 = n^(e1-e2)
        double exponentDiff = e1 - e2;
        double result = Math.Pow(n, exponentDiff);

        Console.WriteLine($"ฐาน: {n}");
        Console.WriteLine($"เลขยกกำลัง: {n}^{e1} ÷ {n}^{e2}");
        Console.WriteLine($"= {n}^{exponentDiff}");
        Console.WriteLine($"= {result}");
        
        // ตรวจสอบ
        double verify = Math.Pow(n, e1) / Math.Pow(n, e2);
        Console.WriteLine($"\nตรวจสอบ: {Math.Pow(n, e1)} ÷ {Math.Pow(n, e2)} = {verify}");
    }
}

ผลลัพธ์


ฐาน: 10
เลขยกกำลัง: 10^10 ÷ 10^8
= 10^2
= 100

กรณีพิเศษและข้อสังเกต

1. ผลลัพธ์เป็นเลขชี้กำลังลบ


double n = 2;
double e1 = 3, e2 = 5; // 2^3 / 2^5 = 2^(-2) = 1/4 = 0.25
double result = Math.Pow(n, e1 - e2); // 0.25

2. ผลลัพธ์เป็น 1 (เลขชี้กำลังเท่ากัน)


double n = 5;
double e1 = 4, e2 = 4; // 5^4 / 5^4 = 5^0 = 1
double result = Math.Pow(n, e1 - e2); // 1

3. ฐานติดลบ


// ฐานติดลบ ต้องระวังเลขชี้กำลัง
Math.Pow(-2, 3) / Math.Pow(-2, 1) = (-8) / (-2) = 4
Math.Pow(-2, 3 - 1) = Math.Pow(-2, 2) = 4 ✓

// แต่ถ้าเลขชี้กำลังไม่ใช่จำนวนเต็ม
Math.Pow(-2, 0.5) // NaN (รากที่ 2 ของลบ)

การใช้ BigInteger สำหรับจำนวนเต็มใหญ่


using System.Numerics;

BigInteger baseNum = 10;
BigInteger e1 = 100;
BigInteger e2 = 50;

// BigInteger ไม่มี Pow ที่รับ BigInteger exponent ต้องแปลงเป็น int
// แต่ 10^100 / 10^50 = 10^50
BigInteger result = BigInteger.Pow(10, (int)(e1 - e2)); // 10^50

สรุปกฎเลขยกกำลัง (รวมจากบทก่อนหน้า)


// คูณ: a^m * a^n = a^(m+n)
// หาร: a^m / a^n = a^(m-n)  
// กำลังของกำลัง: (a^m)^n = a^(m*n)
// กำลังศูนย์: a^0 = 1
// กำลังลบ: a^(-n) = 1/a^n
// กำลังเศษส่วน: a^(1/n) = nth root of a

การประยุกต์ใช้จริง

  • คำนวณดอกเบี้ยทบต้น (Compound Interest)
  • อัลกอริทึมการเข้ารหัส (RSA, Diffie-Hellman)
  • การวิเคราะห์อัลกอริทึม (Big-O notation)
  • ฟิสิกส์/วิศวกรรม: กำลังของ 10 (Scientific notation)