Thursday, February 13, 2025

နက်ရှိုင်းဖွေရှာနဲ့ ရောင်းရင်းများ (ဖေါ်မြူလာအသစ်နဲ့ Pi ကိုတွက်ခြင်း) - ၁

တလောကပဲ ခုခေတ်စားနေတဲ့ တရုပ်ပြည်ကလုပ်တဲ့ DeepSeek ဉာဏ်ရည်တုအကြောင်းယူကျွတ်ဗီဒီယိုကိုတွေ့လို့ကြည့်မိနေတုန်းမှာစိတ်ဝင်စားပြီး သူ့အထဲဝင်ပြီးမေးခွန်းတခုမေးကြည့်တာ အဖြေကောင်းလို့၊မြန်လို့အတော်သဘောကျသွားပါတယ်။ အဲဒီနောက်မှာ string theory ကိုသုတေသနလုပ်နေတာကနေတွေ့လာတဲ့ Saha နဲ့ Siha တို့ရဲ့ pi ဖေါ်မြူလာအသစ်အကြောင်းကိုဖတ်မိတော့ DeepSeek ကိုတွက်နည်းပရိုဂရမ်နမူနာရေးခိုင်းဖို့စိတ်ကူးရသွားတယ်။ ဖေါ်မြူလာကဒီလိုပါ -

ကျနော်က DeepSeek ကို R ဘာသာစကားနဲ့ပရိုဂရမ်ရေးခိုင်းပါတယ်။ သူရေးပြတဲ့ပရိုဂရမ်ကိုစမ်းကြည့်လိုက်-အဆင်မပြေလိုက်-သူကပြင်ပေးလိုက်နဲ့ ငါးကြိမ်လောက်ရှိသွားတာတောင်အဆင်မပြေဘူး။

နောက်တခါမှာအရင်ကစမ်းသပ်ဘူးတဲ့ Gemini ဉာဏ်ရည်တုကိုအဲဒီပရိုဂရမ်ရေးခိုင်းပြန်တယ်။ ဒီမှာလဲပဲလေးငါးခါလောက်သူပြင်ပေးတာကအဆင်မပြေတော့ လက်လျော့လိုက်တယ်။

ဒီနှစ်ခုကြားမှာ ChatGPT ကိုခိုင်းကြည့်ရာမှာတော့ တကြိမ်ထဲနဲ့အဆင်ပြေသွားတယ်။ သူရေးပြတဲ့ပရိုဂရမ်နဲ့တွက်ကြည့်တော့ Pi က ဒဿမ ၁၃-နေရာအထိမှန်ပါတယ်။

Approximated value of pi:   3.14159265358976 (တွက်ပေးတဲ့အဖြေ)    
Built-in value of pi:       3.14159265358979 (ဒဿမ ၁၃- နေရာအထိအမှန်)  

ChatGPT ပထမရေးပေးတာက ဒီလိုပါ -

# Define the function to calculate π using the Saha-Sinha formula
calculate_pi <- function(lambda_val, epsilon) {
  # Initialize variables
  pi_val <- 4           # initial approximation of pi
  n <- 1                # iteration counter
  prev_pi <- 0          # previous pi value to check convergence
  factorial <- 1        # factorial value (1! = 1)
  
  # Print header for iteration log
  cat(sprintf("| %9s | %18s |\n", "Iteration", "Value"))
  cat(sprintf("|%10s|%20s|\n", "----------", "--------------------"))
  
  # Loop until the change is less than the epsilon threshold
  while (abs(pi_val - prev_pi) >= epsilon) {
    # Compute terms
    term1 <- 1 / factorial
    term2 <- 1 / (n + lambda_val) - 4 / (2 * n + 1)
    term3 <- ((2 * n + 1)^2) / (4 * (n + lambda_val)) - n
    
    # Compute the Pochhammer symbol as a product:
    # For n=1, the product is 1 (empty product), otherwise product_{i=0}^{n-2} (term3 + i)
    if (n == 1) {
      pochhammer <- 1
    } else {
      pochhammer <- prod(term3 + 0:(n - 2))
    }
    
    # Save the current pi value before updating
    prev_pi <- pi_val
    # Update pi with the current term
    pi_val <- pi_val + term1 * term2 * pochhammer
    
    # Log the current iteration and pi value
    cat(sprintf("| %9d | %18.14f |\n", n, pi_val))
    
    # Prepare for next iteration
    n <- n + 1
    factorial <- factorial * n  # update factorial to n!
  }
  
  # Return the final approximation of pi and the iteration count
  list(pi = pi_val, iterations = n - 1)
}

# Set parameters for the calculation
lambda_val <- 42
epsilon <- 1e-14

# Call the function to calculate π
result <- calculate_pi(lambda_val, epsilon)

# Print summary of results
cat("\n\n")
cat(sprintf("Approximated value of pi: %18.14f\n", result$pi))
cat(sprintf("Built-in value of pi:     %18.14f\n", pi))
cat(sprintf("Number of iterations:     %d\n", result$iterations))
cat(sprintf("Difference from pi:       %e\n", abs(result$pi - pi)))

ဒီလိုအဖြေထွက်ပါတယ် -

ChatGPT ကို ပရိုဂရမ်စရေးခိုင်းစဉ်က အင်တာနက်ထဲမှာတွေ့ထားတဲ့ Saha နဲ့ Siha တို့ရဲ့ pi ဖေါ်မြူလာအသစ်နဲ့တွက်ထားတဲ့  Pavel Peřina ရေးတဲ့ New iterative method of computing Pi, Python implementation ဆောင်းပါး (https://www.pavelp.cz/posts/eng-iteratively-computing-pi-in-python/) ကိုညွှန်ပြပေးခဲ့ပါတယ်။
ChatGPT ကပရိုဂရမ်ရေးရာမှာ ပြောခဲ့တဲ့ဆောင်းပါးထဲမှာရေးပြထားတဲ့ Python ပရိုဂရမ်ကိုပဲ R ပရိုဂရမ်အဖြစ်ပြောင်းပြီးရေးပေးတာပါ။ သူ့ကို သူ့အမြင်နဲ့သီးခြားပရိုဂရမ်ရေးခိုင်းတော့ ဒီလို‌ရေးပေးတယ် -

# Function to compute π using the Saha–Sinha formula.
# Parameters:
#   lambda    - The λ parameter in the formula.
#   epsilon   - Convergence threshold: the iteration stops when the
#               absolute change in π is less than epsilon.
#   max_iter  - Maximum number of iterations (to avoid infinite loops).
compute_pi <- function(lambda, epsilon = 1e-14, max_iter = 100) {
  # Starting approximation for π is 4.
  pi_val <- 4
  n <- 1                # Iteration counter
  prev_pi <- 0          # To store the previous value of π
  fact <- 1             # To hold n! (starting with 1! = 1)
  
  # Optional: Print header for iteration logs.
  cat(sprintf("| %9s | %20s |\n", "Iteration", "π Approximation"))
  cat(sprintf("|%9s | %20s |\n", "---------", "--------------------"))
  
  # Iterate until the change is below epsilon or until max_iter is reached.
  while (abs(pi_val - prev_pi) >= epsilon && n <= max_iter) {
    # Compute term1 = 1/n!
    term1 <- 1 / fact
    
    # Compute term2 = (1/(n+λ) - 4/(2n+1))
    term2 <- 1 / (n + lambda) - 4 / (2 * n + 1)
    
    # Compute term3 = ((2n+1)^2/(4(n+λ)) - n)
    term3 <- ((2 * n + 1)^2) / (4 * (n + lambda)) - n
    
    # Compute the rising factorial (Pochhammer symbol):
    # For n = 1, the product is defined as 1.
    if (n == 1) {
      poch <- 1
    } else {
      poch <- prod(term3 + 0:(n - 2))
    }
    
    # Save the current approximation before updating.
    prev_pi <- pi_val
    # Add the current term to the π approximation.
    pi_val <- pi_val + term1 * term2 * poch
    
    # Print the current iteration and π approximation.
    cat(sprintf("| %9d | %20.14f |\n", n, pi_val))
    
    # Prepare for the next iteration:
    n <- n + 1
    fact <- fact * n  # Update factorial to n! for the next iteration.
  }
  
  # Return the computed value of π and the number of iterations used.
  list(pi = pi_val, iterations = n - 1)
}

# Example usage:
lambda_val <- 42     # Parameter λ in the formula
epsilon <- 1e-14     # Convergence threshold

result <- compute_pi(lambda_val, epsilon)

cat("\nFinal results:\n")
cat(sprintf("Computed π           : %20.14f\n", result$pi))
cat(sprintf("Built-in π           : %20.14f\n", pi))
cat(sprintf("Number of iterations : %d\n", result$iterations))
cat(sprintf("Difference           : %e\n", abs(result$pi - pi)))

ဒီမှာလဲအဖြေကထပ်တူထွက်ပါတယ် -

No comments:

Post a Comment