Increasing gaps between consecutive Niven numbers: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Library name change.) |
Catskill549 (talk | contribs) |
||
Line 33: | Line 33: | ||
<br><br> |
<br><br> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f INCREASING_GAPS_BETWEEN_CONSECUTIVE_NIVEN_NUMBERS.AWK |
|||
# converted from C |
|||
BEGIN { |
|||
gap_index = 1 |
|||
previous = 1 |
|||
print("Gap index Gap Niven index Niven number") |
|||
print("--------- --- ----------- ------------") |
|||
for (niven=1; gap_index<=22; niven++) { |
|||
sum = digit_sum(niven,sum) |
|||
if (divisible(niven,sum)) { |
|||
if (niven > previous + gap) { |
|||
gap = niven - previous |
|||
printf("%9d %4d %12d %13d\n",gap_index++,gap,niven_index,previous) |
|||
} |
|||
previous = niven |
|||
niven_index++ |
|||
} |
|||
} |
|||
exit(0) |
|||
} |
|||
function digit_sum(n,sum) { |
|||
# returns the sum of the digits of n given the sum of the digits of n - 1 |
|||
sum++ |
|||
while (n > 0 && n % 10 == 0) { |
|||
sum -= 9 |
|||
n = int(n / 10) |
|||
} |
|||
return(sum) |
|||
} |
|||
function divisible(n,d) { |
|||
if (and(d,1) == 0 && and(n,1) == 1) { |
|||
return(0) |
|||
} |
|||
return(n % d == 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 2889 |
|||
11 32 716 3784 |
|||
12 36 1118 6480 |
|||
13 44 2948 19872 |
|||
14 45 4194 28971 |
|||
15 54 5439 38772 |
|||
16 60 33494 297864 |
|||
17 66 51544 478764 |
|||
18 72 61588 589860 |
|||
19 88 94748 989867 |
|||
20 90 265336 2879865 |
|||
21 99 800054 9898956 |
|||
22 108 3750017 49989744 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |