เลข K หลักที่เล็กที่สุดที่หารด้วย X ลงตัว หาได้โดยใช้สูตรตรวจสอบการหารลงตัว สูตรทำงานดังนี้ −
คำนวณเลข K หลักที่น้อยที่สุด [min] เช่น: 10/100/1000 เป็นต้น
ตรวจสอบว่า min หารด้วย X ลงตัวหรือไม่ ถ้าลงตัว นี่คือคำตอบ
ถ้าไม่ลงตัว แล้ว min + X - ((min + X) % k) คือคำตอบ
ตัวอย่าง
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int X = 83;
int K = 5;
cout << "The smallest " << K << " digit number divisible by " << X << " is ";
int MIN = pow(10, K - 1);
if (MIN % X == 0)
cout << MIN;
cout << ((MIN + X) - ((MIN + X) % X));
cout << answer(X, K);
}
ผลลัพธ์
The smallest 5 digit number divisible by 83 is 100430