Dudeney Number คืออะไร?
จำนวนธรรมชาติที่ รากที่สามเท่ากับผลรวมของหลักของมัน
สูตร: n = (digit sum of n)³
ตัวอย่าง
512 = 8³, digit sum = 5+1+2 = 8 → 512 เป็น Dudeney Number
1, 512, 4913, 5832, 17576, 19683 เป็น Dudeney Numbers แรกๆ
อัลกอริธึมตรวจสอบ
- ตรวจสอบว่า n เป็นกำลังสามสมบูรณ์หรือไม่
- ถ้าใช่ คำนวณรากที่สาม
- คำนวณผลรวมหลักของ n
- เปรียบเทียบรากที่สามกับผลรวมหลัก
โปรแกรมตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int digitSum(int n) {
int sum = 0;
while (n > 0) { sum += n % 10; n /= 10; }
return sum;
}
bool isDudeney(int n) {
int cubeRoot = round(cbrt(n));
if (cubeRoot * cubeRoot * cubeRoot != n) return false;
return cubeRoot == digitSum(n);
}
int main() {
int n = 17576;
cout << n << (isDudeney(n) ? " is a Dudeney Number" : " is not a Dudeney Number");
return 0;
}
ผลลัพธ์
17576 is a Dudeney Number