Increasing gaps between consecutive Niven numbers: Difference between revisions
Content added Content deleted
(Added C++ solution) |
|||
Line 32: | Line 32: | ||
:* [https://cs.uwaterloo.ca/journals/JIS/VOL6/Doyon/doyon.pdf (PDF) version of the (above) article by Doyon]. |
:* [https://cs.uwaterloo.ca/journals/JIS/VOL6/Doyon/doyon.pdf (PDF) version of the (above) article by Doyon]. |
||
<br><br> |
<br><br> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <cstdint> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
// Returns the sum of the digits of n given the |
|||
// sum of the digits of n - 1 |
|||
uint64_t digit_sum(uint64_t n, int sum) |
|||
{ |
|||
++sum; |
|||
while (n > 0 && n % 10 == 0) |
|||
{ |
|||
sum -= 9; |
|||
n /= 10; |
|||
} |
|||
return sum; |
|||
} |
|||
int main() |
|||
{ |
|||
// Print numbers with commas |
|||
std::cout.imbue(std::locale("")); |
|||
uint64_t previous = 1, gap = 0; |
|||
int niven_index = 0, gap_index = 1, sum = 0; |
|||
std::cout << "Gap index Gap Niven index Niven number\n"; |
|||
for (uint64_t niven = 1; gap_index <= 32; ++niven) |
|||
{ |
|||
sum = digit_sum(niven, sum); |
|||
if (niven % sum == 0) |
|||
{ |
|||
if (niven > previous + gap) |
|||
{ |
|||
gap = niven - previous; |
|||
std::cout << std::setw(9) << gap_index++ |
|||
<< std::setw(5) << gap |
|||
<< std::setw(15) << niven_index |
|||
<< std::setw(16) << previous << '\n'; |
|||
} |
|||
previous = niven; |
|||
++niven_index; |
|||
} |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Gap index Gap Niven index Niven number |
|||
1 1 1 1 |
|||
2 2 10 10 |
|||
3 6 11 12 |
|||
4 7 26 63 |
|||
5 8 28 72 |
|||
6 10 32 90 |
|||
7 12 83 288 |
|||
8 14 102 378 |
|||
9 18 143 558 |
|||
10 23 561 2,889 |
|||
11 32 716 3,784 |
|||
12 36 1,118 6,480 |
|||
13 44 2,948 19,872 |
|||
14 45 4,194 28,971 |
|||
15 54 5,439 38,772 |
|||
16 60 33,494 297,864 |
|||
17 66 51,544 478,764 |
|||
18 72 61,588 589,860 |
|||
19 88 94,748 989,867 |
|||
20 90 265,336 2,879,865 |
|||
21 99 800,054 9,898,956 |
|||
22 108 3,750,017 49,989,744 |
|||
23 126 6,292,149 88,996,914 |
|||
24 135 44,194,186 689,988,915 |
|||
25 144 55,065,654 879,987,906 |
|||
26 150 61,074,615 989,888,823 |
|||
27 153 179,838,772 2,998,895,823 |
|||
28 192 399,977,785 6,998,899,824 |
|||
29 201 497,993,710 8,889,999,624 |
|||
30 234 502,602,764 8,988,988,866 |
|||
31 258 547,594,831 9,879,997,824 |
|||
32 276 1,039,028,518 18,879,988,824 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |