เราต้องหาจำนวนธรรมชาติตัวแรกที่แฟกทอเรียล (Factorial) หาร x ลงตัว x ถูกกำหนดโดยผู้ใช้ ดังนั้นถ้า x = 16 ผลลัพธ์จะเป็น 6 เพราะ 6! mod 16 = 0 เราจะใช้วิธีทั่วไปในการแก้ปัญหานี้ นับ 1!, 2!, … n! แบบวนซ้ำ และตรวจสอบการหารลงตัวด้วย x หาก modulus เป็น 0 ให้หยุดและคืนค่าตัวเลข
ตัวอย่าง
#include <iostream>
using namespace std;
int getNumber(int x) {
int fact = 1;
int i = 0;
while (fact % x != 0) {
i++;
fact = fact * i;
}
return i;
}
int main() {
int x = 16;
cout << "Minimum value of N is: " << getNumber(x);
}
ผลลัพธ์
Minimum value of N is: 6