Disarium numbers: Difference between revisions
Thundergnat (talk | contribs) m (→{{header|Raku}}: correction) |
(Added Wren) |
||
Line 40: | Line 40: | ||
<pre>0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 |
<pre>0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 |
||
2646798</pre> |
2646798</pre> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
Well, for once, we can brute force the first 19 Disarium numbers without resorting to BigInt or GMP. Surprisingly, much quicker than Raku at 3.35 seconds. |
|||
Taking a rain check on the 20th number which is 20 digits long (too big for Wren's 53 bit integers) to see whether more efficient methods emerge. |
|||
<lang ecmascript>import "./math" for Int |
|||
var limit = 19 |
|||
var count = 0 |
|||
var disarium = [] |
|||
var n = 0 |
|||
while (count < limit) { |
|||
var sum = 0 |
|||
var digits = Int.digits(n) |
|||
for (i in 0...digits.count) sum = sum + digits[i].pow(i+1) |
|||
if (sum == n) { |
|||
disarium.add(n) |
|||
count = count + 1 |
|||
} |
|||
n = n + 1 |
|||
} |
|||
System.print("The first 19 Disarium numbers are:") |
|||
System.print(disarium)</lang> |
|||
{{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> |
Revision as of 17:42, 12 February 2022
A Disarium number is an integer where the sum of each digit raised to to power of its position in the number, is equal to the number.
- E.G.
135 is a Disarium number:
11 + 32 + 53 == 1 + 9 + 125 == 135
There are a finite number of Disarium numbers.
- Task
- Find and display the first 18 Disarium numbers.
- Stretch
- Find and display all 20 Disarium numbers.
- See also
- Geeks for Geeks - Disarium numbers
- OEIS:A032799 - Numbers n such that n equals the sum of its digits raised to the consecutive powers (1,2,3,...)
- Related task: Narcissistic decimal number
- Related task: Own digits power sum Which seems to be the same task as Narcissistic decimal number...
Raku
Not an efficient algorithm. First 18 in less than 1/4 second. 19th in around 45 seconds. Pretty much unusable for the 20th. <lang perl6>my $disarium = (^∞).hyper.map: { $_ if $_ == sum .polymod(10 xx *).reverse Z** 1..* };
put $disarium[^18]; put $disarium[18];</lang>
- Output:
0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798
Wren
Well, for once, we can brute force the first 19 Disarium numbers without resorting to BigInt or GMP. Surprisingly, much quicker than Raku at 3.35 seconds.
Taking a rain check on the 20th number which is 20 digits long (too big for Wren's 53 bit integers) to see whether more efficient methods emerge. <lang ecmascript>import "./math" for Int
var limit = 19 var count = 0 var disarium = [] var n = 0 while (count < limit) {
var sum = 0 var digits = Int.digits(n) for (i in 0...digits.count) sum = sum + digits[i].pow(i+1) if (sum == n) { disarium.add(n) count = count + 1 } n = n + 1
} System.print("The first 19 Disarium numbers are:") System.print(disarium)</lang>
- Output:
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]