Increasing gaps between consecutive Niven numbers: Difference between revisions

added RPL
(Added Algol 68)
(added RPL)
(8 intermediate revisions by 8 users not shown)
Line 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F digit_sum(=n, =sum)
sum++
L n > 0 & n % 10 == 0
Line 62:
previous = niven
niven_index++
niven++</langsyntaxhighlight>
 
{{out}}
Line 92:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # show where the gaps increase in the series of Niven numbers #
# ( numbers divisible by the sum of their digits ) #
INT n count := 0;
Line 101:
print( ( "Index Value Index Number Niven Number", newline ) );
print( ( "----- ----- -------- --------- ------------", newline ) );
INT n0t8 := 0;
FOR d0 FROM 0 TO 9 DO
INT n1 := 0;
FOR d1 FROM 0 TO 9 DO
INT s1 = d0 + d1, t1 = n0 + n1;
INT n2 := 0;
FOR d2 FROM 0 TO 9 DO
INT s2 = s1 + d2, t2 = t1 + n2;
INT n3 := 0;
FOR d3 FROM 0 TO 9 DO
INT s3 = s2 + d3, t3 = t2 + n3;
INT n4 := 0;
FOR d4 FROM 0 TO 9 DO
INT s4 = s3 + d4, t4 = t3 + n4;
INT n5 := 0;
FOR d5 FROM 0 TO 9 DO
INT s5 = s4 + d5, t5 = t4 + n5;
INT n6 := 0;
FOR d6 FROM 0 TO 9 DO
INT s6 = s5 + d6, t6 = t5 + n6;
INT n7 := 0;
FOR d7 FROM 0 TO 9 DO
INT s7 = s6 + d7, t7 = t6 + n7;
FOR d8 FROM 0 TO 9 DO
INT s8 = s7 + d8;
IF s8 /= 0 THEN
INT t8 +:= t7 + d81;
IF t8 MOD s8 = 0 THEN
# have a Niven number #
Line 149 ⟶ 142:
FI
FI
OD # d8 # ;
OD # d7 n7 +:= 10#
OD # d7d6 # ;
OD # d5 n6 +:= 100#
OD # d6d4 # ;
OD # d3 n5 +:= 1 000#
OD # d5d2 # ;
OD # d1 n4 +:= 10 000#
OD # d4 # ;
n3 +:= 100 000
OD # d3 # ;
n2 +:= 1 000 000
OD # d2 # ;
n1 +:= 10 000 000
OD # d1 # ;
n0 +:= 100 000 000
OD # d0 #
END</langsyntaxhighlight>
{{out}}
<pre>
Line 201 ⟶ 186:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f INCREASING_GAPS_BETWEEN_CONSECUTIVE_NIVEN_NUMBERS.AWK
# converted from C
Line 237 ⟶ 222:
return(n % d == 0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 268 ⟶ 253:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
function digit_sum(n, sum)
# devuelve la suma de los dígitos de n dada la suma de los dígitos de n - 1
Line 307 ⟶ 292:
next niven
end
</syntaxhighlight>
</lang>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
Line 354 ⟶ 339:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 394 ⟶ 379:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 438 ⟶ 423:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 483 ⟶ 468:
(the last one where the Niven number fits in the integer type).
 
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
# print uint32 right-aligned in column, with
Line 554 ⟶ 539:
end if;
niven := niven + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 587 ⟶ 572:
27 153 179,838,772 2,998,895,823</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Windows,SysUtils,StdCtrls}}
Because the code requires almost two minutes to run, it has optional callback that supplies information that can be used to power a progress bar.
<syntaxhighlight lang="Delphi">
function SumDigits(N: integer): integer;
{Sum the integers in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
 
 
function IsNiven(N: integer): boolean;
{Test if N is evenly divisible by sum}
{i.e. is it a Niven Number}
var Sum: integer;
begin
Sum:=SumDigits(N);
Result:=(N mod Sum)=0;
end;
 
 
function GetNextNiven(Start: integer): integer;
{Get the next Niven number after Start}
begin
repeat Inc(Start)
until IsNiven(Start);
Result:=Start;
end;
 
 
procedure ShowNivenGaps(Memo: TMemo; Prog: TProgress);
{Show when gaps between sucessive Niven Numbers changes}
var I: integer;
var N1,N2,Gap: integer;
var Cnt: integer;
const Limit = 65000000;
begin
Memo.Lines.Add(' Gap Gap Niven Niven Next');
Memo.Lines.Add('Index Value Index Number Niven Number');
Memo.Lines.Add('----- ----- -------- --------- ------------');
Gap:=0; Cnt:=0;
N1:=GetNextNiven(0);
for I:=1 to Limit do
begin
{Get next Niven and test if Gap has changed}
N2:=GetNextNiven(N1);
if (N2-N1)>Gap then
begin
Gap:=N2-N1;
Inc(Cnt);
Memo.Lines.Add(Format('%5d%6d%15.0n%15.0n%15.0n',[Cnt,Gap,I+0.0,N1+0.0,N2+0.0]));
end;
N1:=N2;
if Assigned(Prog) and ((I mod 100000)=0) then Prog(MulDiv(100,I,Limit));
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
Gap Gap Niven Niven Next
Index Value Index Number Niven Number
----- ----- -------- --------- ------------
1 1 1 1 2
2 2 10 10 12
3 6 11 12 18
4 7 26 63 70
5 8 28 72 80
6 10 32 90 100
7 12 83 288 300
8 14 102 378 392
9 18 143 558 576
10 23 561 2,889 2,912
11 32 716 3,784 3,816
12 36 1,118 6,480 6,516
13 44 2,948 19,872 19,916
14 45 4,194 28,971 29,016
15 54 5,439 38,772 38,826
16 60 33,494 297,864 297,924
17 66 51,544 478,764 478,830
18 72 61,588 589,860 589,932
19 88 94,748 989,867 989,955
20 90 265,336 2,879,865 2,879,955
21 99 800,054 9,898,956 9,899,055
22 108 3,750,017 49,989,744 49,989,852
23 126 6,292,149 88,996,914 88,997,040
24 135 44,194,186 689,988,915 689,989,050
25 144 55,065,654 879,987,906 879,988,050
26 150 61,074,615 989,888,823 989,888,973
</pre>
 
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func digsum n sum .
sum += 1
while n > 0 and n mod 10 = 0
sum -= 9
n = n div 10
.
return sum
.
func divisible n d .
if d mod 2 = 0 and n mod 2 = 1
return 0
.
return if n mod d = 0
.
numfmt 0 8
previous = 1
print " Gap index Gap Niven index Niven number"
print " --------- --- ----------- ------------"
for niven = 1 to 10000000
sum = digsum niven sum
if divisible niven sum = 1
if niven > previous + gap
gap_index += 1
gap = niven - previous
print gap_index & gap & " " & niven_index & " " & previous
.
previous = niven
niven_index += 1
.
.
</syntaxhighlight>
{{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
</pre>
 
=={{header|Fortran}}==
{{trans|C}}
<langsyntaxhighlight Fortranlang="fortran"> program nivengaps
implicit none
integer*8 prev /1/, gap /0/, sum /0/
Line 665 ⟶ 811:
go to 40
end if
end subroutine</langsyntaxhighlight>
 
{{out}}
Line 703 ⟶ 849:
31 258 547,594,831 9,879,997,824
32 276 1,039,028,518 18,879,988,824</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|AWK}}
<langsyntaxhighlight lang="freebasic">
Function digit_sum(n As Uinteger, sum As Ulong) As Ulong
' devuelve la suma de los dígitos de n dada la suma de los dígitos de n - 1
Line 745 ⟶ 890:
Next niven
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 774 ⟶ 919:
23 126 6,292,149 88,996,914
</pre>
 
 
=={{header|FutureBasic}}==
Note: Computation time rises exponentially after the first 22 iterations.
<syntaxhighlight lang="futurebasic">
local fn DigitSum( n as UInt64, sum as UInt64 ) as UInt64
sum++
while ( n > 0 and n mod 10 == 0 )
sum -= 9
n = n / 10
wend
end fn = sum
 
 
local fn IsDivisible( n as UInt64, d as UInt64 ) as BOOL
BOOL result
if ( ( d and 1 ) == 0 and ( n and 1 ) == 1 ) then result = NO : exit fn
result = n mod d == 0
end fn = result
 
local fn DoIt
UInt64 niven, previous = 1, gap = 0, sum = 0
long niven_index = 0, gap_index = 1
printf( @"Index Gap Niven index Niven number" )
printf( @"----- --- ----------- ------------" )
for niven = 1 to gap_index <= 24
sum = fn DigitSum( niven, sum )
if ( fn IsDivisible( niven, sum ) )
if ( niven > previous + gap )
gap = niven - previous
printf @"%3d %6llu %13d %15llu", gap_index, gap, niven_index, previous
gap_index++
end if
previous = niven
niven_index++
end if
next
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
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
23 126 6292149 88996914
24 135 44194186 689988915
</pre>
 
 
 
 
 
 
 
=={{header|Go}}==
This reuses code from the [[https://rosettacode.org/wiki/Harshad_or_Niven_series#Go Harshad or Niven series]] task though converted to use 'uint64' rather than 'int' in case anyone is running Go on a 32-bit platform.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 835 ⟶ 1,059:
pn = n
}
}</langsyntaxhighlight>
 
{{out}}
Line 876 ⟶ 1,100:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (guard)
import Text.Printf (printf)
Line 908 ⟶ 1,132:
$ takeWhile (\(_, gapIndex, _) -> gapIndex < 10_000_000) findGaps
where
row = "%5s%15s%15s\n"</langsyntaxhighlight>
{{out}}
<pre>
Line 938 ⟶ 1,162:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
tasks=: (gap , (,:~ index))@:niven
 
Line 949 ⟶ 1,173:
assert 1 = +/ 10 12 E. niven 100 NB. the sublist 10 12 occurs once in niven numbers less than 100
assert 0 1 6 90 -: gap 1 2 8 98 NB. show infix swapped subtractions
</syntaxhighlight>
</lang>
 
<pre>
Line 972 ⟶ 1,196:
</pre>
J tokens are either isolated ASCII symbols or end with a suffix either `.' or ':' . Verbs return nouns. The sentence of nouns and verbs ~.>./\2-~/\A evaluate from right to left.
<langsyntaxhighlight Jlang="j">( ~. ( >./\ ( 2 -~/\ A ) ) )</langsyntaxhighlight>
 
Given that <syntaxhighlight lang J="j">A</langsyntaxhighlight> names a list of Niven numbers
`2 -~/\ A' finds the difference between successive pairs of A. In 2 -~/\ A the verb -~/\ surrounded by nouns is dyadic, which is to do -~/ on the length 2 infixes of A. Consuming the 2 and A, infix adverb \ passes length 2 vectors of items of A to the verb -~/ . Adverb / inserts the verb - between the items of the vector, which it then evaluates since there's no more...except that we want a[i+1]-a[i], which explains the passive ~ adverb to swap the arguments.
 
Line 983 ⟶ 1,207:
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">
public class NivenNumberGaps {
 
Line 1,017 ⟶ 1,241:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,070 ⟶ 1,294:
</pre>
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def add(s): reduce s as $x (null; . + $x);
 
def digit_sum:
Line 1,076 ⟶ 1,300:
add(digits);
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; </langsyntaxhighlight>
'''Niven Numbers'''
<langsyntaxhighlight lang="jq"># input: the largest Niven number to be considered (null => infinite)
# output: a stream of informative JSON objects -
# {gap_index, gap, niven_index, previous}
Line 1,105 ⟶ 1,329:
"--------- --- ----------- ------------",
( 1E7 | nivens
| "\(.gap_index|lpad(9)) \(.gap|lpad(4)) \(.niven_index|lpad(12)) \(.niven|lpad(13))" )</langsyntaxhighlight>
 
{{out}}
Line 1,138 ⟶ 1,362:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
function findharshadgaps(N)
Line 1,157 ⟶ 1,381:
 
findharshadgaps(50_000_000_000)
</langsyntaxhighlight>{{out}}
<pre>
Gap Index Number Index Niven Number
Line 1,204 ⟶ 1,428:
MAD does not have bitwise operations. It's possible to fake them using division,
but that would not be much of an optimization.
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION REM.(A,B) = A-A/B*B
Line 1,237 ⟶ 1,461:
LOOP CONTINUE
 
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,267 ⟶ 1,491:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[NivenQ]
$HistoryLength = 0;
NivenQ[n_Integer] := Divisible[n, Total[IntegerDigits[n]]]
Line 1,276 ⟶ 1,500:
nivennumber = Pick[Most[sel], i];
gap = sel[[nivenindex + 1]] - sel[[nivenindex]];
Grid[{gapindex, gap, nivenindex, nivennumber} // Transpose // Prepend[{"gap index", "gap", "niven index", "niven number"}], Frame -> All]</langsyntaxhighlight>
{{out}}
<pre>gap index gap niven index niven number
Line 1,306 ⟶ 1,530:
=={{header|Nim}}==
{{trans|C++}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
func digitsSum(n, sum: uint64): uint64 =
Line 1,338 ⟶ 1,562:
inc gapIndex
previous = niven
inc nivenIndex</langsyntaxhighlight>
 
{{out}}
Line 1,378 ⟶ 1,602:
{{works with|Free Pascal}}
As fast as [http://rosettacode.org/wiki/Increasing_gaps_between_consecutive_Niven_numbers#Go GO]
<langsyntaxhighlight lang="pascal">program NivenGaps;
{$IFDEF FPC}
{$MODE DELPHI}
Line 1,505 ⟶ 1,729:
begin
FindGaps;
end.</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,576 ⟶ 1,800:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 1,595 ⟶ 1,819:
$last = $count;
last if ++$index >= $threshold;
}</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,624 ⟶ 1,848:
=={{header|Phix}}==
Replaced sum(digits) in the original with sd, otherwise no great attempt to optimise
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer n = 1, prev = 1, g, gap = 0, count = 1, sd = 1
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence digits={1}
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">gap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">={</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
procedure nNiven()
while 1 do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">nNiven</span><span style="color: #0000FF;">()</span>
n += 1
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
for i=length(digits) to 0 by -1 do
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if i=0 then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
digits = prepend(digits,1)
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">prepend</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end if
if digits[i] <9span thenstyle="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
digits[i] += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
digits[i] <span style="color: 0#008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sd -= 9
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end for
<span style="color: #000000;">sd</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">9</span>
sd += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if remainder(n,sd)=0 then exit end if
<span style="color: #000000;">sd</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end while
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sd</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
printf(1,"gap size Niven index Niven #\n")
atom t0 = time()
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gap size Niven index Niven #\n"</span><span style="color: #0000FF;">)</span>
while n<=1_000_000_000 do
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
nNiven()
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">10_000_000</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1_000_000_000</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
g = n-prev
<span style="color: #000000;">nNiven</span><span style="color: #0000FF;">()</span>
if g>gap then
<span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">prev</span>
string e = elapsed(time()-t0)
<span style="color: #008080;">if</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">></span><span style="color: #000000;">gap</span> <span style="color: #008080;">then</span>
printf(1,"%,5d %,14d %,15d (%s)\n",{g, count, prev, e})
<span style="color: #004080;">string</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
gap = g
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,5d %,14d %,15d (%s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">g</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
end if
<span style="color: #000000;">gap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">g</span>
prev = n
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
count += 1
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
end while</lang>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,692 ⟶ 1,919:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 1,733 ⟶ 1,960:
niven_index += 1
niven += 1
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,766 ⟶ 1,993:
{{works with|Rakudo|2019.11}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
unit sub MAIN (Int $threshold = 10000000);
Line 1,785 ⟶ 2,012:
$last = count;
last if $index >= $threshold;
}</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,813 ⟶ 2,040:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the largest gap between Niven numbers (up to LIMIT).*/
parse arg lim . /*obtain optional arguments from the CL*/
if lim=='' | lim==',' then lim= 10000000 /*Not specified? Then use the default.*/
Line 1,839 ⟶ 2,066:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do c=length(_)-3 to 1 by -3; _=insert(',', _, c); end; return _
tell: say arg(1); return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 20000000000 </tt> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <small> (which is &nbsp; '''20''' &nbsp; billion) </small> }}
<pre>
Line 1,885 ⟶ 2,112:
 
The "chunk" method is essentially a sum of several chunks, &nbsp; the sums are found by a table lookup (associative array in REXX).
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays the largest gap between Niven numbers (up to LIMIT).*/
parse arg lim . /*obtain optional arguments from the CL*/
if lim=='' | lim==',' then lim= 1000000000000 /*Not specified? Then use the default.*/
Line 1,922 ⟶ 2,149:
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do c=length(_)-3 to 1 by -3; _=insert(',', _, c); end; return _
tell: say arg(1); return</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br> !-->
 
=={{header|RPL}}==
{{works with|RPL|HP49-C}}
« →STR 0.
1. PICK3 SIZE '''FOR''' j
OVER j DUP SUB STR→ +
'''NEXT''' NIP
» '<span style="color:blue">∑DIG</span>' STO
« '''DO''' 1 + '''UNTIL''' DUPDUP <span style="color:blue">∑DIG</span> / FP NOT '''END'''
» '<span style="color:blue">NEXTNIVEN</span>' STO
« { { 1 1 1 1 } } 1 1 1 → res gap gapindex nivindex
« 1
'''WHILE''' res SIZE 15 < '''REPEAT'''
DUP <span style="color:blue">NEXTNIVEN</span> DUP2 SWAP -
'''IF''' DUP gap >
'''THEN''' DUP 'gap' STO 'gapindex' INCR SWAP nivindex 5 ROLL
4 →LIST 1 →LIST 'res' SWAP STO+
'''ELSE''' ROT DROP2 '''END'''
'nivindex' 1 STO+
'''END'''
DROP res
» » '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { { 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 } }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">nivens = Enumerator.new {|y| (1..).each {|n| y << n if n.remainder(n.digits.sum).zero?} }
 
cur_gap = 0
Line 1,938 ⟶ 2,193:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,965 ⟶ 2,220:
126 6292149 88996914
</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// num-format = "0.4"
 
Line 2,015 ⟶ 2,271:
niven += 1;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,058 ⟶ 2,314:
{{libheader|Wren-fmt}}
Limited to Niven numbers up to 1 billion in order to finish in a reasonable time (a little under 2 minutes on my machine).
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var newSum // recursive
Line 2,100 ⟶ 2,356:
var g = n - pn
if (g > pg) {
SystemFmt.print("%(Fmt.d(3,$3d g)) %(Fmt.dc(13$,13d i)) $,14d", %(Fmt.dc(14g, i, pn))")
pg = g
}
Line 2,106 ⟶ 2,362:
i = i + 1
n = h.call()
}</langsyntaxhighlight>
 
{{out}}
Line 2,143 ⟶ 2,399:
This program runs under Linux.
 
<langsyntaxhighlight lang="asm"> bits 64
section .data
;;; Header
Line 2,261 ⟶ 2,517:
dec cl
jnz .pad
.out: ret</langsyntaxhighlight>
 
{{out}}
Line 2,300 ⟶ 2,556:
32 276 1,039,028,518 18,879,988,824</pre>
 
=={{header|XPL0}}==
{{trans|11l}}
<syntaxhighlight lang "XPL0">func DigitSum(N, Sum); \Return sum of digits in N given sum of digits in N-1
int N, Sum;
[Sum:= Sum+1;
while N > 0 and rem(N/10) = 0 do
[Sum:= Sum -9;
N:= N/10;
];
return Sum;
];
 
int Previous, Gap, S, NivenIndex, GapIndex, Niven;
def Tab = 9;
 
[Previous:= 1;
Gap:= 0;
S:= 0;
NivenIndex:= 0;
GapIndex:= 1;
 
Text(0, "Index Gap Index Niven^m^j");
 
Niven:= 1;
 
while GapIndex <= 23 do
[S:= DigitSum(Niven, S);
if rem(Niven/S) = 0 then
[if Niven > Previous + Gap then
[Gap:= Niven - Previous;
IntOut(0, GapIndex); ChOut(0, Tab);
IntOut(0, Gap); ChOut(0, Tab);
IntOut(0, NivenIndex); ChOut(0, Tab);
IntOut(0, Previous); CrLf(0);
GapIndex:= GapIndex+1;
];
Previous:= Niven;
NivenIndex:= NivenIndex+1;
];
Niven:= Niven+1;
];
]</syntaxhighlight>
{{out}}
<pre>
Index Gap Index Niven
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
23 126 6292149 88996914
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
sub digit_sum(n, sum)
// devuelve la suma de los dígitos de n dada la suma de los dígitos de n - 1
Line 2,339 ⟶ 2,664:
next niven
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">harshadW:=[1..].tweak(fcn(n){ if(n%(n.split().sum(0))) Void.Skip else n });
harshadW:=Walker.zero().tweak(fcn(go){ // faster than one liner, fewer calls
foreach h in ([go.value..]){ // spin
Line 2,353 ⟶ 2,677:
if(0 == h%s){ go.set(h+1); return(h) }
}
}.fp(Ref(1)));</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("gap size Niven index Niven #");
prev,gap := harshadW.next(),0;
while(harshadW.n<=10_000_000){
Line 2,362 ⟶ 2,686:
}
prev=h;
}</langsyntaxhighlight>
{{out}}
<pre>
1,151

edits