Jump to content

Increasing gaps between consecutive Niven numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
m (syntax highlighting fixup automation)
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 151:
OD # d1 #
OD # d0 #
END</langsyntaxhighlight>
{{out}}
<pre>
Line 186:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f INCREASING_GAPS_BETWEEN_CONSECUTIVE_NIVEN_NUMBERS.AWK
# converted from C
Line 222:
return(n % d == 0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 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 292:
next niven
end
</syntaxhighlight>
</lang>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
Line 339:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 379:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdint>
#include <iomanip>
#include <iostream>
Line 423:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 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 539:
end if;
niven := niven + 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 575:
=={{header|Fortran}}==
{{trans|C}}
<langsyntaxhighlight Fortranlang="fortran"> program nivengaps
implicit none
integer*8 prev /1/, gap /0/, sum /0/
Line 650:
go to 40
end if
end subroutine</langsyntaxhighlight>
 
{{out}}
Line 692:
=={{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 730:
Next niven
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 763:
=={{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 820:
pn = n
}
}</langsyntaxhighlight>
 
{{out}}
Line 861:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE NumericUnderscores #-}
import Control.Monad (guard)
import Text.Printf (printf)
Line 893:
$ takeWhile (\(_, gapIndex, _) -> gapIndex < 10_000_000) findGaps
where
row = "%5s%15s%15s\n"</langsyntaxhighlight>
{{out}}
<pre>
Line 923:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
tasks=: (gap , (,:~ index))@:niven
 
Line 934:
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 957:
</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 968:
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">
public class NivenNumberGaps {
 
Line 1,002:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,055:
</pre>
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def add(s): reduce s as $x (null; . + $x);
 
def digit_sum:
Line 1,061:
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,090:
"--------- --- ----------- ------------",
( 1E7 | nivens
| "\(.gap_index|lpad(9)) \(.gap|lpad(4)) \(.niven_index|lpad(12)) \(.niven|lpad(13))" )</langsyntaxhighlight>
 
{{out}}
Line 1,123:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
function findharshadgaps(N)
Line 1,142:
 
findharshadgaps(50_000_000_000)
</langsyntaxhighlight>{{out}}
<pre>
Gap Index Number Index Niven Number
Line 1,189:
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,222:
LOOP CONTINUE
 
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,252:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[NivenQ]
$HistoryLength = 0;
NivenQ[n_Integer] := Divisible[n, Total[IntegerDigits[n]]]
Line 1,261:
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,291:
=={{header|Nim}}==
{{trans|C++}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
func digitsSum(n, sum: uint64): uint64 =
Line 1,323:
inc gapIndex
previous = niven
inc nivenIndex</langsyntaxhighlight>
 
{{out}}
Line 1,363:
{{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,490:
begin
FindGaps;
end.</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,561:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 1,580:
$last = $count;
last if ++$index >= $threshold;
}</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,609:
=={{header|Phix}}==
Replaced sum(digits) in the original with sd, otherwise no great attempt to optimise
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 1,647:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,680:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 1,721:
niven_index += 1
niven += 1
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,754:
{{works with|Rakudo|2019.11}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
unit sub MAIN (Int $threshold = 10000000);
Line 1,773:
$last = count;
last if $index >= $threshold;
}</langsyntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,801:
 
=={{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,827:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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,873:
 
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,910:
/*──────────────────────────────────────────────────────────────────────────────────────*/
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|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,926:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,954:
</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// num-format = "0.4"
 
Line 2,003:
niven += 1;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,046:
{{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 lang="ecmascript">import "/fmt" for Fmt
 
var newSum // recursive
Line 2,094:
i = i + 1
n = h.call()
}</langsyntaxhighlight>
 
{{out}}
Line 2,131:
This program runs under Linux.
 
<langsyntaxhighlight lang="asm"> bits 64
section .data
;;; Header
Line 2,249:
dec cl
jnz .pad
.out: ret</langsyntaxhighlight>
 
{{out}}
Line 2,291:
=={{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,327:
next niven
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,335:
 
=={{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,341:
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,350:
}
prev=h;
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.