Increasing gaps between consecutive Niven numbers: Difference between revisions

Add Cowgol
(Add Cowgol)
Line 264:
32 276 1,039,028,518 18,879,988,824
</pre>
 
=={{header|Cowgol}}==
{{trans|C}}
 
The largest integer Cowgol supports is 32-bit, so this code prints up to the 27th gap
(the last one where the Niven number fits in the integer type).
 
<lang cowgol>include "cowgol.coh";
 
# print uint32 right-aligned in column, with
# thousands separators
sub print_col(num: uint32, width: uint8) is
# maximum integer is 4,294,967,296 for length 13,
# plus one extra for the zero terminator
var buf: uint8[14];
var start := &buf[0];
var last := UIToA(num, 10, &buf[0]);
# right-align and add separators
var right := &buf[13];
var th: uint8 := 3;
while last >= start loop
[right] := [last];
right := @prev right;
last := @prev last;
if th == 0 and last >= start then
# add separator every 3 characters
[right] := ',';
right := @prev right;
th := 2;
else
th := th - 1;
end if;
end loop;
# print the result and spaces
var size := (13 - (right - start)) as uint8;
while width >= size loop
print_char(' ');
width := width-1;
end loop;
print(@next right);
end sub;
 
# returns sum of digits of n, given sum of digits of n-1
sub digit_sum(n: uint32, prev: uint32): (sum: uint32) is
sum := prev + 1;
while n > 0 and n % 10 == 0 loop
sum := sum - 9;
n := n / 10;
end loop;
end sub;
 
var prev: uint32 := 1;
var gap: uint32 := 0;
var sum: uint32 := 0;
var idx: uint32 := 0;
var gap_idx: uint8 := 1;
var niven: uint32 := 1;
 
print("Gap index Gap Niven index Niven number\n");
while gap_idx <= 27 loop
sum := digit_sum(niven, sum);
if (not (sum & 1 == 0 and niven & 1 == 1))
and (niven % sum == 0) then
if niven > prev + gap then
gap := niven - prev;
print_col(gap_idx as uint32, 9);
gap_idx := gap_idx + 1;
print_col(gap, 5);
print_col(idx, 15);
print_col(prev, 16);
print_nl();
end if;
prev := niven;
idx := idx + 1;
end if;
niven := niven + 1;
end loop;</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</pre>
 
 
=={{header|Go}}==
Line 363 ⟶ 474:
276 1,039,028,518 18,879,988,824
</pre>
 
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE NumericUnderscores #-}
2,112

edits