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

คำนวณยกกำลัง 3 ใน C# (Power of Three)

การคำนวณยกกำลัง 3 (Cube) หรือ $n^3$ ใน C# สามารถทำได้หลายวิธี ตั้งแต่การคูณตรงไปจนถึง Recursive และ Built-in Methods

วิธีที่ 1: คูณตรง (Simplest & Fastest)

public static long Cube(int n) {
    return (long)n * n * n;
}

// ใช้งาน
long result = Cube(5);  // 125

วิธีที่ 2: Recursive (ตามตัวอย่างต้นฉบับ)

public static long Power(int n, int p) {
    if (p != 0) {
        return n * Power(n, p - 1);
    }
    return 1;
}

// ใช้งาน: 5^3
long res = Power(5, 3);  // 125

/* Trace:
Power(5, 3) = 5 * Power(5, 2)
Power(5, 2) = 5 * Power(5, 1)
Power(5, 1) = 5 * Power(5, 0)
Power(5, 0) = 1
= 5 * 5 * 5 * 1 = 125 */

ตัวอย่างโค้ดสมบูรณ์ (Recursive)

using System;

public class Demo {
    public static void Main(string[] args) {
        int n = 5;
        int p = 3;
        long res = Power(n, p);
        Console.WriteLine(res);  // 125
    }

    static long Power(int n, int p) {
        if (p != 0) {
            return n * Power(n, p - 1);
        }
        return 1;
    }
}

ผลลัพธ์

125

วิธีที่ 3: Math.Pow (Built-in)

using System;

double n = 5;
double p = 3;
double result = Math.Pow(n, p);  // 125.0

// หมายเหตุ: คืนค่า double อาจมี Precision Issue กับเลขใหญ่
// Math.Pow(10, 3) = 1000.0 ✓
// Math.Pow(10, 15) อาจได้ 999999999999999.9

วิธีที่ 4: Exponentiation by Squaring (O(log n))

public static long PowerFast(long base, int exp) {
    long result = 1;
    while (exp > 0) {
        if ((exp & 1) == 1)  // exp is odd
            result *= base;
        base *= base;
        exp >>= 1;  // exp /= 2
    }
    return result;
}

// 5^3 = 125
// Loop 1: exp=3(odd) result=5, base=25, exp=1
// Loop 2: exp=1(odd) result=125, base=625, exp=0
// Done

Benchmark Comparison (n=10, p=10)

Method Time Complexity Relative Speed Precision
Direct Multiply (n*n*n) O(1) Fastest Exact (Integer)
Exponentiation by Squaring O(log p) Very Fast Exact (Integer)
Recursive O(p) Slow (Stack Overhead) Exact
Math.Pow O(1) Hardware Fast Double (Approximate)

BigInteger สำหรับเลขใหญ่มาก

using System.Numerics;

public static BigInteger BigPower(BigInteger base, int exp) {
    BigInteger result = 1;
    while (exp > 0) {
        if ((exp & 1) == 1) result *= base;
        base *= base;
        exp >>= 1;
    }
    return result;
}

// 2^1000
var huge = BigPower(2, 1000);  // 301 digits

Checked vs Unchecked Context

// Checked - โยน OverflowException ถ้าเกิน Range
checked {
    int max = int.MaxValue;
    var cube = max * max * max;  // OverflowException
}

// Unchecked - Wrap Around (Default สำหรับ Non-constant)
unchecked {
    int max = int.MaxValue;
    var cube = max * max * max;  // ไม่โยน Exception, ผลผิดพลาด
}

Best Practice สำหรับ Power of Three

// ✅ สำหรับ Cube (p=3) แบบคงที่ - ใช้ Direct Multiply
public static long Cube(int n) => (long)n * n * n;

// ✅ สำหรับ General Power - ใช้ Exponentiation by Squaring
public static long Power(int n, int p) {
    if (p < 0) throw new ArgumentException("Negative exponent not supported for int");
    long result = 1;
    long baseVal = n;
    while (p > 0) {
        if ((p & 1) == 1) result *= baseVal;
        baseVal *= baseVal;
        p >>= 1;
    }
    return result;
}