Increasing gaps between consecutive Niven numbers: Difference between revisions
Increasing gaps between consecutive Niven numbers (view source)
Revision as of 15:58, 27 August 2022
, 1 year agosyntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 36:
{{trans|Python}}
<
sum++
L n > 0 & n % 10 == 0
Line 62:
previous = niven
niven_index++
niven++</
{{out}}
Line 92:
=={{header|ALGOL 68}}==
<
# ( numbers divisible by the sum of their digits ) #
INT n count := 0;
Line 151:
OD # d1 #
OD # d0 #
END</
{{out}}
<pre>
Line 186:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f INCREASING_GAPS_BETWEEN_CONSECUTIVE_NIVEN_NUMBERS.AWK
# converted from C
Line 222:
return(n % d == 0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 253:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight 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>
=={{header|C}}==
{{trans|C++}}
<
#include <stdbool.h>
#include <stdint.h>
Line 339:
}
return 0;
}</
{{out}}
Line 379:
=={{header|C++}}==
<
#include <iomanip>
#include <iostream>
Line 423:
}
return 0;
}</
{{out}}
Line 468:
(the last one where the Niven number fits in the integer type).
<
# print uint32 right-aligned in column, with
Line 539:
end if;
niven := niven + 1;
end loop;</
{{out}}
Line 575:
=={{header|Fortran}}==
{{trans|C}}
<
implicit none
integer*8 prev /1/, gap /0/, sum /0/
Line 650:
go to 40
end if
end subroutine</
{{out}}
Line 692:
=={{header|FreeBASIC}}==
{{trans|AWK}}
<
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>
{{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.
<
import "fmt"
Line 820:
pn = n
}
}</
{{out}}
Line 861:
=={{header|Haskell}}==
<
import Control.Monad (guard)
import Text.Printf (printf)
Line 893:
$ takeWhile (\(_, gapIndex, _) -> gapIndex < 10_000_000) findGaps
where
row = "%5s%15s%15s\n"</
{{out}}
<pre>
Line 923:
=={{header|J}}==
<syntaxhighlight 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>
<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.
<
Given that <syntaxhighlight lang
`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}}==
<
public class NivenNumberGaps {
Line 1,002:
}
</syntaxhighlight>
{{out}}
Line 1,055:
</pre>
'''Preliminaries'''
<
def digit_sum:
Line 1,061:
add(digits);
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .; </
'''Niven Numbers'''
<
# 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))" )</
{{out}}
Line 1,123:
=={{header|Julia}}==
<
function findharshadgaps(N)
Line 1,142:
findharshadgaps(50_000_000_000)
</
<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.
<
INTERNAL FUNCTION REM.(A,B) = A-A/B*B
Line 1,222:
LOOP CONTINUE
END OF PROGRAM </
{{out}}
Line 1,252:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
$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]</
{{out}}
<pre>gap index gap niven index niven number
Line 1,291:
=={{header|Nim}}==
{{trans|C++}}
<
func digitsSum(n, sum: uint64): uint64 =
Line 1,323:
inc gapIndex
previous = niven
inc nivenIndex</
{{out}}
Line 1,363:
{{works with|Free Pascal}}
As fast as [http://rosettacode.org/wiki/Increasing_gaps_between_consecutive_Niven_numbers#Go GO]
<
{$IFDEF FPC}
{$MODE DELPHI}
Line 1,490:
begin
FindGaps;
end.</
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,561:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use List::Util 'sum';
Line 1,580:
$last = $count;
last if ++$index >= $threshold;
}</
{{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
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,680:
=={{header|Python}}==
<
"""
Line 1,721:
niven_index += 1
niven += 1
</syntaxhighlight>
{{out}}
Line 1,754:
{{works with|Rakudo|2019.11}}
<syntaxhighlight lang="raku"
unit sub MAIN (Int $threshold = 10000000);
Line 1,773:
$last = count;
last if $index >= $threshold;
}</
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,801:
=={{header|REXX}}==
<
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</
{{out|output|text= when using the input of: <tt> 20000000000 </tt> <small> (which is '''20''' billion) </small> }}
<pre>
Line 1,873:
The "chunk" method is essentially a sum of several chunks, the sums are found by a table lookup (associative array in REXX).
<
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</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> !-->
=={{header|Ruby}}==
<
cur_gap = 0
Line 1,926:
end
end
</syntaxhighlight>
{{out}}
<pre>Gap Index of gap Starting Niven
Line 1,954:
</pre>
=={{header|Rust}}==
<
// num-format = "0.4"
Line 2,003:
niven += 1;
}
}</
{{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).
<
var newSum // recursive
Line 2,094:
i = i + 1
n = h.call()
}</
{{out}}
Line 2,131:
This program runs under Linux.
<
section .data
;;; Header
Line 2,249:
dec cl
jnz .pad
.out: ret</
{{out}}
Line 2,291:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight 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>
{{out}}
<pre>
Line 2,335:
=={{header|zkl}}==
<
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)));</
<
prev,gap := harshadW.next(),0;
while(harshadW.n<=10_000_000){
Line 2,350:
}
prev=h;
}</
{{out}}
<pre>
|