Jump to content

Disarium numbers: Difference between revisions

Add C# implementation
(Add Cobol)
(Add C# implementation)
(28 intermediate revisions by 16 users not shown)
Line 30:
<br>
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F is_disarium(n)
V digitos = String(n).len
V suma = 0
V x = n
L x != 0
suma += (x % 10) ^ digitos
digitos--
x I/= 10
I suma == n
R 1B
E
R 0B
 
V limite = 19
V cont = 0
V n = 0
print(‘The first ’limite‘ Disarium numbers are:’)
L cont < limite
I is_disarium(n)
print(n, end' ‘ ’)
cont++
n++
</syntaxhighlight>
 
{{out}}
<pre>
The first 19 Disarium numbers are:
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|Action!}}==
Line 266 ⟶ 300:
<pre>
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto encontrarunDisariumpara(_X_)
#synon _encontrarunDisariumpara siencontréunDisarium
 
algoritmo
decimales '0'
iterar para ( n=3000000, n, --n )
si encontré un Disarium 'n', entonces{
imprimir( #(utf8("El número ")),n," es Disarium\n")
}
siguiente
terminar
 
subrutinas
 
encontrar un Disarium para (n)
 
i=0
n, obtener tamaño parte entera, mover a 'i'
m=0, tn=n, d=0
iterar mientras ( tn )
último dígito de 'tn', mover a 'd,tn'
d, elevado a 'i', más 'm'
mover a 'm'
--i
reiterar
 
retornar ' #(m==n) '
</syntaxhighlight>
{{out}}
<pre>
El número 2646798 es Disarium
El número 2427 es Disarium
El número 1676 es Disarium
El número 1306 es Disarium
El número 598 es Disarium
El número 518 es Disarium
El número 175 es Disarium
El número 135 es Disarium
El número 89 es Disarium
El número 9 es Disarium
El número 8 es Disarium
El número 7 es Disarium
El número 6 es Disarium
El número 5 es Disarium
El número 4 es Disarium
El número 3 es Disarium
El número 2 es Disarium
El número 1 es Disarium
 
</pre>
 
Line 406 ⟶ 495:
end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
<pre>
 
Igual que la entrada de FreeBASIC.
==={{header|Chipmunk Basic}}===
</pre>
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 sub isdisarium(n)
120 digitos = len(str$(n))
130 suma = 0
140 x = n
150 while x <> 0
160 r = (x mod 10)
170 suma = suma+(r^digitos)
180 digitos = digitos-1
190 x = int(x/10)
200 wend
210 if suma = n then isdisarium = true else isdisarium = false
220 end sub
230 '
240 limite = 19
250 cnt = 0
260 n = 0
270 print "The first ";limite;" Disarium numbers are:"
280 while cnt < limite
290 if isdisarium(n) then
300 print n;" ";
310 cnt = cnt+1
320 endif
330 n = n+1
340 wend
350 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
Line 435 ⟶ 553:
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Python entry.</pre>
<pre>
Igual que la entrada de Python.
</pre>
 
==={{header|Run BASIC}}===
Line 526 ⟶ 642:
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>e>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Yabasic}}===
Line 643 ⟶ 757:
1676
2427</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="BQN">Digits ← {𝕊 0: ⟨⟩; (𝕊⌊𝕩÷10)∾10|𝕩}
DigitPowerSum ← (+´⊢⋆1+↕∘≠)∘Digits
Disarium ← ⊢=DigitPowerSum
 
Disarium¨⊸/ ↕2500</syntaxhighlight>
{{out}}
<pre>⟨ 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 ⟩</pre>
 
=={{header|C}}==
Line 685 ⟶ 808:
{{out}}
<pre>0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
class DisariumNumbers {
// Method to check if a number is a Disarium number
public static bool IsDisarium(int num) {
int n = num;
int len = num.ToString().Length;
int sum = 0;
int i = 1;
while (n > 0) {
// C# does not support implicit conversion from double to int, so we explicitly convert the result of Math.Pow to int
sum += (int)Math.Pow(n % 10, len - i + 1);
n /= 10;
i++;
}
return sum == num;
}
 
static void Main(string[] args) {
int i = 0;
int count = 0;
// Find and print the first 19 Disarium numbers
while (count <= 18) {
if (IsDisarium(i)) {
Console.Write($"{i} ");
count++;
}
i++;
}
Console.WriteLine();
}
}
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
 
</pre>
 
=={{header|C++}}==
Line 839 ⟶ 1,004:
1676
2427</pre>
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 FUNC dps#(n#) CLOSED
0020 DIM digits#(10)
0030 length#:=0
0040 rest#:=n#
0050 WHILE rest#>0 DO
0060 length#:+1
0070 digits#(length#):=rest# MOD 10
0080 rest#:=rest# DIV 10
0090 ENDWHILE
0100 sum#:=0
0110 FOR i#:=1 TO length# DO
0120 sum#:+digits#(i#)^(length#-i#+1)
0130 ENDFOR i#
0140 RETURN sum#
0150 ENDFUNC dps#
0160 //
0170 amount#:=18
0180 num#:=0
0190 WHILE amount#>0 DO
0200 IF dps#(num#)=num# THEN
0210 amount#:-1
0220 PRINT num#
0230 ENDIF
0240 num#:+1
0250 ENDWHILE
0260 PRINT
0270 END
</syntaxhighlight>
{{out}}
<pre>0
1
2
3
4
5
6
7
8
9
89
135
175
518
598
1306
1676
2427</pre>
 
=={{header|Cowgol}}==
Line 902 ⟶ 1,116:
2427
2646798</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Finds the first 19 numbers in 425 miliseconds. It uses a look up table for the powers and tests about 5 million numbers per second. However, this is not fast enough to find the 20th number. By my calculation, at this speed, it would only take 77,000 years. In other words, the brute force method can't be used to find the 20th number.
 
<syntaxhighlight lang="Delphi">
{Table to speed up calculating powers. Contains all the powers
of the digits 0..9 raised to the 0..21 power}
 
const PowersTable: array [0..21,0..9] of int64 = (
($01,$01,$01,$01,$01,$01,$01,$01,$01,$01),
($00,$01,$02,$03,$04,$05,$06,$07,$08,$09),
($00,$01,$04,$09,$10,$19,$24,$31,$40,$51),
($00,$01,$08,$1B,$40,$7D,$D8,$157,$200,$2D9),
($00,$01,$10,$51,$100,$271,$510,$961,$1000,$19A1),
($00,$01,$20,$F3,$400,$C35,$1E60,$41A7,$8000,$E6A9),
($00,$01,$40,$2D9,$1000,$3D09,$B640,$1CB91,$40000,$81BF1),
($00,$01,$80,$88B,$4000,$1312D,$44580,$C90F7,$200000,$48FB79),
($00,$01,$100,$19A1,$10000,$5F5E1,$19A100,$57F6C1,$1000000,$290D741),
($00,$01,$200,$4CE3,$40000,$1DCD65,$99C600,$267BF47,$8000000,$17179149),
($00,$01,$400,$E6A9,$100000,$9502F9,$39AA400,$10D63AF1,$40000000,$CFD41B91),
($00,$01,$800,$2B3FB,$400000,$2E90EDD,$159FD800,$75DB9C97,$200000000,$74E74F819),
($00,$01,$1000,$81BF1,$1000000,$E8D4A51,$81BF1000,$339014821,$1000000000,$41C21CB8E1),
($00,$01,$2000,$1853D3,$4000000,$48C27395,$30A7A6000,$168F08F8E7,$8000000000,$24FD3027FE9),
($00,$01,$4000,$48FB79,$10000000,$16BCC41E9,$123EDE4000,$9DE93ECE51,$40000000000,$14CE6B167F31),
($00,$01,$8000,$DAF26B,$40000000,$71AFD498D,$6D79358000,$45160B7A437,$200000000000,$BB41C3CA78B9),
($00,$01,$10000,$290D741,$100000000,$2386F26FC1,$290D7410000,$1E39A5057D81,$1000000000000,$6954FE21E3E81),
($00,$01,$20000,$7B285C3,$400000000,$B1A2BC2EC5,$F650B860000,$D39383266E87,$8000000000000,$3B3FCEF3103289),
($00,$01,$40000,$17179149,$1000000000,$3782DACE9D9,$5C5E45240000,$5C908960D05B1,$40000000000000,$2153E468B91C6D1),
($00,$01,$80000,$4546B3DB,$4000000000,$1158E460913D,$22A359ED80000,$287F3C1A5B27D7,$200000000000000,$12BF307AE81FFD59),
($00,$01,$100000,$CFD41B91,$10000000000,$56BC75E2D631,$CFD41B9100000,$11B7AA4B87E16E1,$1000000000000000,$A8B8B452291FE821),
($00,$01,$200000,$26F7C52B3,$40000000000,$1B1AE4D6E2EF5,$4DEF8A56600000,$7C05A810B72A027,$8000000000000000,$EE7E56E3721F2929));
 
 
function GetPower(X,Y: integer): int64;
{Extract power from table}
begin
Result:=PowersTable[Y,X];
end;
 
 
function IsDisarium(N: integer): boolean;
{Sum all powers of the digits raised to position power}
var S: string;
var I,J: integer;
var Sum: int64;
begin
Sum:=0;
S:=IntToStr(N);
for I:=1 to Length(S) do
begin
Sum:=Sum+GetPower(byte(S[I])-$30,I);
end;
Result:=Sum=N;
end;
 
 
procedure ShowDisariumNumbers(Memo: TMemo);
{Show Disarium numbers up to specified limit}
{Processes about 5 million numbers per second}
var I,Cnt: int64;
begin
Cnt:=0;
I:=0;
while I<High(int64) do
begin
if IsDisarium(I) then
begin
Inc(Cnt);
Memo.Lines.Add(IntToStr(Cnt)+': '+IntToStr(I));
if Cnt>=19 then break;
end;
Inc(I);
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
1: 0
2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 7
9: 8
10: 9
11: 89
12: 135
13: 175
14: 518
15: 598
16: 1306
17: 1676
18: 2427
19: 2646798
 
</pre>
 
 
=={{header|D}}==
Line 1,116 ⟶ 1,433:
1676
2427</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func disarium x .
h = x
while h > 0
d[] &= h mod 10
h = h div 10
.
for i = 1 to len d[]
h += pow d[i] (len d[] - i + 1)
.
return if h = x
.
while count < 19
if disarium n = 1
count += 1
print n
.
n += 1
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 1,170 ⟶ 1,509:
= 1676
= 2427</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: pow 1 swap 0 ?do over * loop nip ;
: len 1 swap begin dup 10 >= while 10 / swap 1+ swap repeat drop ;
 
: dps 0 swap dup len
begin dup while
swap 10 /mod swap
2 pick pow
3 roll +
rot 1- rot
swap
repeat
2drop
;
 
: disarium dup dps = ;
: disaria 2700000 0 ?do i disarium if i . cr then loop ;
 
disaria
bye</syntaxhighlight>
{{out}}
<pre>0
1
2
3
4
5
6
7
8
9
89
135
175
518
598
1306
1676
2427
2646798 </pre>
 
=={{header|FutureBasic}}==
Line 1,440 ⟶ 1,820:
 
=={{header|J}}==
<syntaxhighlight lang="j">digits=: 10 #".inv ]"0@":
disarium=: (= (+/ .@:^ #\)@digits)"0
 
I.disarium i.1e4
I. disarium i. 27e5
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427</syntaxhighlight>
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
Line 1,595 ⟶ 1,977:
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|Lua}}==
Like most other solutions, this stops at 19. Computation time aside, the 20th Disarium number is greater than 2^53, above which double-precision floating-point format (which is Lua's in-built number type) has insufficient precision to distinguish between one integer and the next.
<syntaxhighlight lang="lua">function isDisarium (x)
local str, sum, digit = tostring(x), 0
for pos = 1, #str do
digit = tonumber(str:sub(pos, pos))
sum = sum + (digit ^ pos)
end
return sum == x
end
 
local count, n = 0, 0
while count < 19 do
if isDisarium(n) then
count = count + 1
io.write(n .. " ")
end
n = n + 1
end</syntaxhighlight>
{{out}}
<pre>0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre>
 
=={{header|MAD}}==
Line 1,679 ⟶ 2,083:
{{out}}
<pre>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798}</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a list of digits given a nonnegative integer */
decompose(num) := block([digits, remainder],
digits: [],
while num > 0 do
(remainder: mod(num, 10),
digits: cons(remainder, digits),
num: floor(num/10)),
digits
)$
 
disariump(n):=block(
decompose(n),
makelist(%%[i]^i,i,length(%%)),
apply("+",%%),
if n=%% then true)$
 
disarium_count(len):=block([i:0,count:0,result:[]],
while count<len do (if disariump(i) then (result:endcons(i,result),count:count+1),i:i+1),
result)$
 
/*Test cases */
disarium_count(18);
</syntaxhighlight>
{{out}}
<pre>
[0,1,2,3,4,5,6,7,8,9,89,135,175,518,598,1306,1676,2427]
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
isDisarium = function(n)
num = n
sum = 0
if num == 0 then return true
for i in range(ceil(log(n)), 1)
sum += (n % 10) ^ i
n = floor(n / 10)
end for
return num == sum
end function
 
foundCnt = 0
cnt = 0
while foundCnt < 19
if isDisarium(cnt) then
foundCnt += 1
print cnt
end if
cnt +=1
end while</syntaxhighlight>
{{out}}
<pre>
0
1
2
3
4
5
6
7
8
9
89
135
175
518
598
1306
1676
2427
2646798
</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (take 18 disaria)), Stdout "\n"]
 
disaria :: [num]
disaria = filter disarium [0..]
 
disarium :: num->bool
disarium n = n = sum (zipWith (^) (digits n) [1..])
 
digits :: num->[num]
digits 0 = [0]
digits n = reverse (digits' n)
where digits' 0 = []
digits' n = (n mod 10) : digits' (n div 10)
 
zipWith :: (* -> ** -> ***) -> [*] -> [**] -> [***]
zipWith f x y = map f' (zip2 x y)
where f' (x,y) = f x y </syntaxhighlight>
{{out}}
<pre>[0,1,2,3,4,5,6,7,8,9,89,135,175,518,598,1306,1676,2427]</pre>
 
=={{header|Modula-2}}==
Line 1,751 ⟶ 2,252:
1676
2427</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
Line 1,782 ⟶ 2,284:
 
let is_disarium n =
let rec aux x f =
if x < 10
then x,f 2 x
else let n, l = aux (x / 10) in(fun nl y -> f (succ l) (y + pow (x mod 10) l, succ l))
in
n = fst (aux n Fun.(const id)
 
let () =
Seq.(ints 0 |> filter is_disarium |> take 19 |> mapiter string_of_int(Printf.printf " %u%!"))
|> List.of_seq |> String.concat " " |> print_endlineprint_newline</syntaxhighlight>
{{out}}
<pre> 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798</pre>
 
=={{header|Odin}}==
<syntaxhighlight lang="Go">
package disarium
import "core:fmt"
import "core:math"
 
/* main block start */
main :: proc() {
fmt.print("\nThe first 18 Disarium numbers are:")
count, i: int
for count < 19 {
if is_disarium(i) {
fmt.print(" ", i)
count += 1
}
i += 1
}
fmt.println("")
} /* main block end */
 
/* proc definitions */
power :: proc(base, exponent: int) -> int {
result := 1
for _ in 1 ..= exponent {
result *= base
}
return result
}
 
is_disarium :: proc(num: int) -> bool {
n := num
sum := 0
len := n <= 9 ? 1 : cast(int)math.floor_f64(math.log10_f64(auto_cast n) + 1)
for n > 0 {
sum += power(n % 10, len)
n /= 10
len -= 1
}
return num == sum
}
</syntaxhighlight>
{{out}}
<pre>
The first 18 Disarium numbers are: 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
simply adding one by one and keep track of sums.
<syntaxhighlight lang="pascal">
program disarium;
//compile with fpc -O3 -Xs
{$IFDEF WINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
{$IFDEF FPC}
{$Mode Delphi}
uses
sysutils;
{$ELSE}
uses
system.SysUtils;
{$ENDIF}
const
MAX_BASE = 16;
cDigits : array[0..MAX_BASE-1] of char =
('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
 
MAX_DIGIT_CNT = 31;
 
type
tDgt_cnt= 0..MAX_DIGIT_CNT-1;
tdgtPows = array[tDgt_cnt,0..MAX_BASE] of Uint64;
tdgtMaxSumPot = array[tDgt_cnt] of Uint64;
tmyDigits = record
dgtPot : array[tDgt_cnt] of Uint64;
dgtSumPot : array[tDgt_cnt] of Uint64;
dgtNumber : UInt64;
digit : array[0..31] of byte;
dgtMaxLen : tDgt_cnt;
end;
 
const
UPPER_LIMIT = 100*1000*1002;
 
var
{$Align 32}
dgtPows :tdgtPows;
 
procedure InitMyPots(var mp :tdgtPows;base:int32);
var
pot,dgt:Uint32;
p : Uint64;
begin
fillchar(mp,SizeOf(mp),#0);
For dgt := 0 to BASE do
begin
p := dgt;
For pot in tDgt_cnt do
begin
mp[pot,dgt] := p;
p := p*dgt;
end;
end;
p := 0;
end;
 
procedure Out_Digits(var md:tmyDigits);
var
i : Int32;
Begin
with md do
begin
write('dgtNumber ',dgtNumber,' = ',dgtSumPot[0],' in Base ');
For i := dgtMaxLen-1 downto 0 do
write(cDigits[digit[i]]);
writeln;
end;
end;
 
procedure IncByOne(var md:tmyDigits;Base: Int32);inline;
var
PotSum : Uint64;
potBase: nativeInt;
dg,pot,idx : Int32;
 
Begin
with md do
begin
//first digit seperate
pot := dgtMaxLen-1;
dg := digit[0]+1;
if dg < BASE then
begin
inc(dgtNumber);
digit[0]:= dg;
dgtPot[0] := dgtPows[pot,dg];
dgtSumPot[0] := dgtSumPot[1] + dgtPot[0];
EXIT;
end;
 
dec(dgtNumber,Base-1);
digit[0]:= 0;
dgtPot[0]:= 0;
dgtSumPot[0] := dgtSumPot[1];
 
potbase := Base;
idx := 1;
dec(pot);
while pot >= 0 do
Begin
dg := digit[idx]+1;
if dg < BASE then
begin
inc(dgtNumber,potbase);
digit[idx]:= dg;
dgtPot[idx]:= dgtPows[pot,dg];
PotSum := dgtSumPot[idx+1];
//update sum
while idx>=0 do
begin
inc(PotSum,dgtPot[idx]);
dgtSumPot[idx] := PotSum;
dec(idx);
end;
EXIT;
end;
dec(dgtNumber,(dg-1)*PotBase);
potbase *= Base;
digit[idx]:= 0;
dgtPot[idx] := 0;
dec(pot);
inc(idx);
end;
 
For pot := idx downto 0 do
Begin
dgtPot[idx] :=0;
dgtSumPot[pot] := 1;
end;
digit[idx] := 1;
dgtPot[idx] :=1;
dgtMaxLen := idx+1;
dgtNumber := potbase;
end;
end;
 
procedure OneRun(var s: tmyDigits;base:UInt32;Limit:Int64);
var
i : int64;
cnt : Int32;
begin
Writeln('Base = ',base);
InitMyPots(dgtPows,base);
fillchar(s,SizeOf(s),#0);
s.dgtMaxLen := 1;
 
i := 0;
cnt := 0;
repeat
if s.dgtSumPot[0] = s.dgtNumber then
Begin
Out_Digits(s);
inc(cnt);
end;
IncByOne(s,base);
inc(i);
until (i>=Limit);
writeln ( i,' increments and found ',cnt);
end;
 
var
{$Align 32}
s : tmyDigits;
T0: TDateTime;
base: nativeInt;
Begin
base := 10;
T0 := time;
OneRun(s,base,2646799);
T0 := (time-T0)*86400;
writeln(T0:8:3,' s');
writeln;
 
base := 11;
T0 := time;
OneRun(s,base,100173172);
T0 := (time-T0)*86400;
writeln(T0:8:3,' s');
writeln;
{$IFDEF WINDOWS}
readln;
{$ENDIF}
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Base = 10
dgtNumber 0 = 0 in Base 0
dgtNumber 1 = 1 in Base 1
dgtNumber 2 = 2 in Base 2
dgtNumber 3 = 3 in Base 3
dgtNumber 4 = 4 in Base 4
dgtNumber 5 = 5 in Base 5
dgtNumber 6 = 6 in Base 6
dgtNumber 7 = 7 in Base 7
dgtNumber 8 = 8 in Base 8
dgtNumber 9 = 9 in Base 9
dgtNumber 89 = 89 in Base 89
dgtNumber 135 = 135 in Base 135
dgtNumber 175 = 175 in Base 175
dgtNumber 518 = 518 in Base 518
dgtNumber 598 = 598 in Base 598
dgtNumber 1306 = 1306 in Base 1306
dgtNumber 1676 = 1676 in Base 1676
dgtNumber 2427 = 2427 in Base 2427
dgtNumber 2646798 = 2646798 in Base 2646798
2646799 increments and found 19
0.008 s
 
Base = 11
dgtNumber 0 = 0 in Base 0
dgtNumber 1 = 1 in Base 1
dgtNumber 2 = 2 in Base 2
dgtNumber 3 = 3 in Base 3
dgtNumber 4 = 4 in Base 4
dgtNumber 5 = 5 in Base 5
dgtNumber 6 = 6 in Base 6
dgtNumber 7 = 7 in Base 7
dgtNumber 8 = 8 in Base 8
dgtNumber 9 = 9 in Base 9
dgtNumber 10 = 10 in Base A
dgtNumber 27 = 27 in Base 25
dgtNumber 39 = 39 in Base 36
dgtNumber 109 = 109 in Base 9A
dgtNumber 126 = 126 in Base 105
dgtNumber 525 = 525 in Base 438
dgtNumber 580 = 580 in Base 488
dgtNumber 735 = 735 in Base 609
dgtNumber 1033 = 1033 in Base 85A
dgtNumber 1044 = 1044 in Base 86A
dgtNumber 2746 = 2746 in Base 2077
dgtNumber 59178 = 59178 in Base 40509
dgtNumber 63501 = 63501 in Base 43789
dgtNumber 100173171 = 100173171 in Base 515AA64A
100173172 increments and found 24
0.294 s
</pre>
 
=={{header|Perl}}==
Line 2,271 ⟶ 3,062:
2646798</pre>
 
 
=={{header|RPL}}==
≪ DUP →STR → digits
≪ 0
1 digits SIZE '''FOR''' j
digits j DUP SUB STR→ j ^ +
'''NEXT'''
==
≫ ≫ '<span style="color:blue">DSRM?</span>' STO
≪ → max
≪ { } 0
'''WHILE''' OVER SIZE max < '''REPEAT'''
'''IF''' DUP <span style="color:blue">DSRM?</span> '''THEN''' SWAP OVER + SWAP '''END'''
1 +
'''END''' DROP
≫ ≫ '<span style="color:blue">DSRMN</span>' STO
 
18 <span style="color:blue">DSRMN</span>
{{out}}
<pre>
{ 0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">defdisariums is_disarium(num)= Enumerator.new do |y|
(0..).each do |n|
n = num.to_s
sumi = 0
y << n if n.digits.reverse.sum{|d| d ** (i+=1) } == n
for i in 1..(n.length)
end
sum += n[i-1].to_i**i
end
return sum == num
end
 
puts disariums.take(19).to_a.join(" ")
i = 0
count = 0
while count < 19
if is_disarium(i)
printf("%d ", i)
count += 1
end
i += 1
end
print("\n")
</syntaxhighlight>
{{out}}
Line 2,402 ⟶ 3,205:
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program disarium_numbers;
loop for n in [0..2700000] | disarium n do
print(n);
end loop;
 
op disarium(n);
k := n;
digits := [[k mod 10, k div:= 10](1) : until k=0];
p := #digits+1;
powsum := +/[d ** (p -:= 1) : d in digits];
return powsum = n;
end op;
end program;</syntaxhighlight>
{{out}}
<pre>0
1
2
3
4
5
6
7
8
9
89
135
175
518
598
1306
1676
2427
2646798</pre>
 
=={{header|Sidef}}==
Line 2,412 ⟶ 3,250:
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427]
</pre>
 
=={{header|VTL-2}}==
{{Trans|ALGOL W}}
Finds the first 18 Disarium numbers - computes a table of digit powers up to the fourth power.
<syntaxhighlight lang="vtl2">1000 N=1
1010 D=0
1020 :N*10+D)=D
1030 D=D+1
1040 #=D<10*1020
1050 N=2
1060 :N*10)=0
1070 D=1
1080 :N*10+D)=:N-1*10+D)*D
1090 D=D+1
1100 #=D<10*1080
1120 N=N+1
1130 #=N<5*1060
2000 C=0
2010 T=10
2020 L=1
2030 N=0
2040 #=N=T=0*2070
2050 T=T*10
2060 L=L+1
2070 V=N
2080 P=L
2090 S=0
2100 V=V/10
2110 S=S+:P*10+%
2120 P=P-1
2130 #=V>1*(S-1<N)*2100
2140 #=S=N=0*2180
2150 C=C+1
2160 $=32
2170 ?=N
2180 N=N+1
2190 #=C<18*2040
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427
</pre>
 
Line 2,703 ⟶ 3,499:
 
Found the first 20 Disarium numbers.
</pre>
 
=={{header|VTL-2}}==
{{Trans|ALGOL W}}
Finds the first 18 Disarium numbers - computes a table of digit powers up to the fourth power.
<syntaxhighlight lang="vtl2">1000 N=1
1010 D=0
1020 :N*10+D)=D
1030 D=D+1
1040 #=D<10*1020
1050 N=2
1060 :N*10)=0
1070 D=1
1080 :N*10+D)=:N-1*10+D)*D
1090 D=D+1
1100 #=D<10*1080
1120 N=N+1
1130 #=N<5*1060
2000 C=0
2010 T=10
2020 L=1
2030 N=0
2040 #=N=T=0*2070
2050 T=T*10
2060 L=L+1
2070 V=N
2080 P=L
2090 S=0
2100 V=V/10
2110 S=S+:P*10+%
2120 P=P-1
2130 #=V>1*(S-1<N)*2100
2140 #=S=N=0*2180
2150 C=C+1
2160 $=32
2170 ?=N
2180 N=N+1
2190 #=C<18*2040
</syntaxhighlight>
{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427
</pre>
 
Line 2,712 ⟶ 3,550:
 
As a possible optimization, I tried caching all possible digit powers but there was no perceptible difference in running time for numbers up to 7 digits long.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var limit = 19
Line 2,745 ⟶ 3,583:
 
So, if the Phix example requires 48 minutes to find the 20th number, it would probably take Wren the best part of a day to do the same which is far longer than I have patience for.
<syntaxhighlight lang="ecmascriptwren">var DMAX = 7 // maxmimum digits
var LIMIT = 19 // maximum number of Disariums to find
 
Line 2,933 ⟶ 3,771:
 
I haven't bothered to search all 20 digits numbers up to the unsigned 64 limit as this would take far longer and, of course, be fruitless in any case.
<syntaxhighlight lang="ecmascriptwren">import "./i64" for U64
 
var DMAX = 20 // maxmimum digits
337

edits

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