Disarium numbers: Difference between revisions

Add C# implementation
(Added Lua version)
(Add C# implementation)
(15 intermediate revisions by 11 users not shown)
Line 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 440 ⟶ 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 469 ⟶ 553:
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Python entry.</pre>
<pre>
Igual que la entrada de Python.
</pre>
 
==={{header|Run BASIC}}===
Line 560 ⟶ 642:
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>e>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Yabasic}}===
Line 728 ⟶ 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 1,311 ⟶ 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,676 ⟶ 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,937 ⟶ 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}}==
Line 2,075 ⟶ 2,296:
{{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}}==
Line 2,797 ⟶ 3,064:
 
=={{header|RPL}}==
≪ DUP →STR → sdigits
≪ 0
1 sdigits SIZE '''FOR''' j
sdigits j DUP SUB STR→ j ^ +
'''NEXT'''
==
≫ ≫ '<span style="color:blue">DSRM?</span>' STO
==
‘DSRM?’ STO
≪ → nmax
≪ { } 0
'''WHILE''' OVER SIZE nmax < '''REPEAT'''
'''IF''' DUP <span style="color:blue">DSRM?</span> '''THEN''' SWAP OVER + SWAP '''END'''
SWAP OVER + SWAP
END
1 +
'''END''' DROP
≫ ≫ '<span style="color:blue">DSRMN</span>' STO
DROP
 
18 <span style="color:blue">DSRMN</span>
´DSRMN’ STO
18 DSRMN
{{out}}
<pre>
Line 2,945 ⟶ 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,955 ⟶ 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 3,246 ⟶ 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 3,255 ⟶ 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 3,288 ⟶ 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 3,476 ⟶ 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