Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*6.28
- Deitel & Deitel C++ How to Program, 10th ed(Indian subcontinent adaptation)
- Visual Studio Community 2019
- */
- #include <iostream>
- #include <cmath>
- using namespace std;
- bool isDudeney(unsigned long testNum, unsigned long cubed) {
- unsigned long sum{ 0 };
- int digit{ 0 };
- for (unsigned long divisor = 1'000'000'000; divisor >= 10; divisor /= 10) {
- if (testNum >= divisor) {
- sum += testNum / divisor;
- }
- testNum %= divisor;
- }
- sum += testNum; // at this point testNum should be the digit in the one's position
- if (sum == cubed) return true;
- else return false;
- }
- int main() {
- unsigned long testnum;
- int dudeneyCtr{ 0 };
- unsigned long cubedroot;
- // check up to 100 million
- for (testnum = 0; testnum < 100'000'000; testnum++) {
- if (testnum > 0 && testnum % 10'000'000 == 0) cout << testnum << endl; // give some feedback
- cubedroot = cbrt(testnum);
- if (cubedroot * cubedroot * cubedroot == testnum) {
- if (isDudeney(testnum, cubedroot)) {
- cout << testnum << " is a Dudeney. Its cube root is " << cubedroot << endl;
- dudeneyCtr++;
- }
- }
- }
- cout << endl << "Count of Dudeney numbers: " << dudeneyCtr << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment