สมมติว่าเรามีตัวเลขสองตัว n และข้อเท็จจริง เราต้องหากำลังที่ใหญ่ที่สุดของ n ที่หารความจริง! (แฟกทอเรียลของความจริง). ดังนั้นหาก fact =5 และ n =2 ผลลัพธ์จะเป็น 3 ดังนั้น 5! =120 และนี่หารด้วย 2^3 =8 ลงตัว
เราจะใช้สูตรของ Legendre นี้พบว่ากำลังที่ใหญ่ที่สุดของจำนวนเฉพาะที่แบ่งความจริง!. เราจะหาตัวประกอบเฉพาะของ n ทั้งหมด แล้วหากำลังที่ใหญ่ที่สุดของมัน ที่หารความจริง!.
ดังนั้นหากข้อเท็จจริงคือ 146 และ n =15 แล้วตัวประกอบเฉพาะของ n คือ 5 และ 3 ดังนั้น
สำหรับ 3 มันจะเป็น [146/3] + [48/3] + [16/3] + [5/3] + [1/3] =48 + 16 + 5 + 1 + 0 =70.พี>
สำหรับ 5 มันจะเป็น [146/5] + [29/5] + [5/5] + [1/3] =29 + 5 + 1 + 0 =35.
ตัวอย่าง
#include<iostream>
#include<cmath>
using namespace std;
int getPowerPrime(int fact, int p) {
int res = 0;
while (fact > 0) {
res += fact / p;
fact /= p;
}
return res;
}
int findMinPower(int fact, int n) {
int res = INT_MAX;
for (int i = 2; i <= sqrt(n); i++) {
int cnt = 0;
if (n % i == 0) {
cnt++;
n = n / i;
}
if (cnt > 0) {
int curr = getPowerPrime(fact, i) / cnt;
res = min(res, curr);
}
}
if (n >= 2) {
int curr = getPowerPrime(fact, n);
res = min(res, curr);
}
return res;
}
int main() {
int fact = 146, n = 5;
cout << "Minimum power: " << findMinPower(fact, n);
} ผลลัพธ์
Minimum power: 35