Disarium numbers: Difference between revisions

Add C# implementation
(Added Chipmunk Basic)
(Add C# implementation)
(3 intermediate revisions by 3 users not shown)
Line 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 2,254 ⟶ 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 3,162 ⟶ 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,453 ⟶ 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,462 ⟶ 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,495 ⟶ 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,683 ⟶ 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
338

edits