Own digits power sum: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 21: Line 21:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V MAX_BASE = 10
<syntaxhighlight lang="11l">V MAX_BASE = 10
V POWER_DIGIT = (0 .< MAX_BASE).map(_ -> (0 .< :MAX_BASE).map(_ -> 1))
V POWER_DIGIT = (0 .< MAX_BASE).map(_ -> (0 .< :MAX_BASE).map(_ -> 1))
V USED_DIGITS = (0 .< MAX_BASE).map(_ -> 0)
V USED_DIGITS = (0 .< MAX_BASE).map(_ -> 0)
Line 73: Line 73:
print(‘Own digits power sums for N = 3 to 9 inclusive:’)
print(‘Own digits power sums for N = 3 to 9 inclusive:’)
L(n) NUMBERS
L(n) NUMBERS
print(n)</lang>
print(n)</syntaxhighlight>


{{out}}
{{out}}
Line 106: Line 106:
Non-recursive, generates the possible combinations ands the own digits power sums in reverse order, without duplication.<br>
Non-recursive, generates the possible combinations ands the own digits power sums in reverse order, without duplication.<br>
Uses ideas from various solutions on this page, particularly the observation that the own digits power sum is independent of the order of the digits. Uses the minimum possible highest digit for the number of digits (width) and maximum number of zeros for the width to avoid some combinations. This trys 73 359 combinations.
Uses ideas from various solutions on this page, particularly the observation that the own digits power sum is independent of the order of the digits. Uses the minimum possible highest digit for the number of digits (width) and maximum number of zeros for the width to avoid some combinations. This trys 73 359 combinations.
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# counts of used digits, check is a copy used to check the number is an own digit power sum #
# counts of used digits, check is a copy used to check the number is an own digit power sum #
[ 0 : 9 ]INT used, check; FOR i FROM 0 TO 9 DO check[ i ] := 0 OD;
[ 0 : 9 ]INT used, check; FOR i FROM 0 TO 9 DO check[ i ] := 0 OD;
Line 245: Line 245:
OD;
OD;
print( ( "Considered ", whole( try count, 0 ), " digit combinations" ) )
print( ( "Considered ", whole( try count, 0 ), " digit combinations" ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 278: Line 278:
Takes about 1.9 seconds to run (GCC 9.3.0 -O3).
Takes about 1.9 seconds to run (GCC 9.3.0 -O3).
{{trans|Wren}}
{{trans|Wren}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <math.h>


Line 333: Line 333:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 343: Line 343:
{{trans|Pascal}}
{{trans|Pascal}}
Down now to 14ms.
Down now to 14ms.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <string.h>


Line 434: Line 434:
for (i = 0; i <= j; ++i) printf("%lld\n", numbers[i]);
for (i = 0; i <= j; ++i) printf("%lld\n", numbers[i]);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 442: Line 442:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Own digits power sum. Nigel Galloway: October 2th., 2021
// Own digits power sum. Nigel Galloway: October 2th., 2021
let fN g=let N=[|for n in 0..9->pown n g|] in let rec fN g=function n when n<10->N.[n]+g |n->fN(N.[n%10]+g)(n/10) in (fun g->fN 0 g)
let fN g=let N=[|for n in 0..9->pown n g|] in let rec fN g=function n when n<10->N.[n]+g |n->fN(N.[n%10]+g)(n/10) in (fun g->fN 0 g)
{3..9}|>Seq.iter(fun g->let fN=fN g in printf $"%d{g} digit are:"; {pown 10 (g-1)..(pown 10 g)-1}|>Seq.iter(fun g->if g=fN g then printf $" %d{g}"); printfn "")
{3..9}|>Seq.iter(fun g->let fN=fN g in printf $"%d{g} digit are:"; {pown 10 (g-1)..(pown 10 g)-1}|>Seq.iter(fun g->if g=fN g then printf $" %d{g}"); printfn "")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 458: Line 458:
</pre>
</pre>
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
dim as uinteger N, curr, temp, dig, sum
dim as uinteger N, curr, temp, dig, sum


Line 473: Line 473:
next curr
next curr
next N
next N
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 484: Line 484:
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
Takes about 16.5 seconds to run including compilation time.
Takes about 16.5 seconds to run including compilation time.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 535: Line 535:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 545: Line 545:
Down to about 128 ms now including compilation time. Actual run time only 8 ms!
Down to about 128 ms now including compilation time. Actual run time only 8 ms!
{{trans|Pascal}}
{{trans|Pascal}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 644: Line 644:
fmt.Println(n)
fmt.Println(n)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 653: Line 653:
=={{header|Haskell}}==
=={{header|Haskell}}==
Using a function from the Combinations with Repetitions task:
Using a function from the Combinations with Repetitions task:
<lang haskell>import Data.List (sort)
<syntaxhighlight lang="haskell">import Data.List (sort)


------------------- OWN DIGITS POWER SUM -----------------
------------------- OWN DIGITS POWER SUM -----------------
Line 696: Line 696:


digits :: Show a => a -> [Int]
digits :: Show a => a -> [Int]
digits n = (\x -> read [x] :: Int) <$> show n</lang>
digits n = (\x -> read [x] :: Int) <$> show n</syntaxhighlight>
{{Out}}
{{Out}}
<pre>N ∈ [3 .. 8]
<pre>N ∈ [3 .. 8]
Line 734: Line 734:


(*) gojq requires significantly more time and memory to run this program.
(*) gojq requires significantly more time and memory to run this program.
<lang jq>def maxBase: 10;
<syntaxhighlight lang="jq">def maxBase: 10;


# The global object:
# The global object:
Line 799: Line 799:


"Own digits power sums for N = 3 to 9 inclusive:",
"Own digits power sums for N = 3 to 9 inclusive:",
main</lang>
main</syntaxhighlight>
{{out}}
{{out}}
As for [[#Wren|Wren]].
As for [[#Wren|Wren]].


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function isowndigitspowersum(n::Integer, base=10)
<syntaxhighlight lang="julia">function isowndigitspowersum(n::Integer, base=10)
dig = digits(n, base=base)
dig = digits(n, base=base)
exponent = length(dig)
exponent = length(dig)
Line 813: Line 813:
isowndigitspowersum(i) && println(i)
isowndigitspowersum(i) && println(i)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
153
153
Line 840: Line 840:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>cf = Compile[{{n, _Integer}}, Module[{digs, len},
<syntaxhighlight lang="mathematica">cf = Compile[{{n, _Integer}}, Module[{digs, len},
digs = IntegerDigits[n];
digs = IntegerDigits[n];
len = Length[digs];
len = Length[digs];
Total[digs^len] == n
Total[digs^len] == n
], CompilationTarget -> "C"];
], CompilationTarget -> "C"];
Select[Range[100, 100000000 - 1], cf]</lang>
Select[Range[100, 100000000 - 1], cf]</syntaxhighlight>
{{out}}
{{out}}
<pre>{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477}</pre>
<pre>{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477}</pre>
Line 852: Line 852:
===Brute Force===
===Brute Force===
Use <code>Parallel::ForkManager</code> to obtain concurrency, trading some code complexity for less-than-infinite run time. Still <b>very</b> slow.
Use <code>Parallel::ForkManager</code> to obtain concurrency, trading some code complexity for less-than-infinite run time. Still <b>very</b> slow.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 890: Line 890:
$pm->wait_all_children;
$pm->wait_all_children;


say $_ for sort { $a <=> $b } map { @$_ } values %own_dps;</lang>
say $_ for sort { $a <=> $b } map { @$_ } values %own_dps;</syntaxhighlight>
{{out}}
{{out}}
<pre>153
<pre>153
Line 918: Line 918:
===Combinatorics===
===Combinatorics===
Leverage the fact that all combinations of digits give same DPS. Much faster than brute force, as only non-redundant values tested.
Leverage the fact that all combinations of digits give same DPS. Much faster than brute force, as only non-redundant values tested.
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use List::Util 'sum';
use List::Util 'sum';
Line 933: Line 933:
}
}


print join "\n", sort { $a <=> $b } @own_dps;</lang>
print join "\n", sort { $a <=> $b } @own_dps;</syntaxhighlight>
{{out}}
{{out}}
<pre>153
<pre>153
Line 959: Line 959:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">minps</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxps</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">minps</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxps</span>
Line 1,000: Line 1,000:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,025: Line 1,025:
===Python :: Procedural===
===Python :: Procedural===
==== slower ====
==== slower ====
<lang python>""" Rosetta code task: Own_digits_power_sum """
<syntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum """


def isowndigitspowersum(integer):
def isowndigitspowersum(integer):
Line 1,037: Line 1,037:
if isowndigitspowersum(i):
if isowndigitspowersum(i):
print(i)
print(i)
</lang>{{out}}Same as Wren example. Takes over a half hour to run.
</syntaxhighlight>{{out}}Same as Wren example. Takes over a half hour to run.
==== faster ====
==== faster ====
{{trans|Wren}} Same output.
{{trans|Wren}} Same output.
<lang python>""" Rosetta code task: Own_digits_power_sum (recursive method)"""
<syntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum (recursive method)"""


MAX_BASE = 10
MAX_BASE = 10
Line 1,095: Line 1,095:
print('Own digits power sums for N = 3 to 9 inclusive:')
print('Own digits power sums for N = 3 to 9 inclusive:')
for n in NUMBERS:
for n in NUMBERS:
print(n)</lang>
print(n)</syntaxhighlight>


===Python :: Functional===
===Python :: Functional===
Using a function from the Combinations with Repetitions task:
Using a function from the Combinations with Repetitions task:
<lang python>'''Own digit power sums'''
<syntaxhighlight lang="python">'''Own digit power sums'''


from itertools import accumulate, chain, islice, repeat
from itertools import accumulate, chain, islice, repeat
Line 1,192: Line 1,192:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>N ∈ [3 .. 8]
<pre>N ∈ [3 .. 8]
Line 1,221: Line 1,221:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>(3..8).map: -> $p {
<syntaxhighlight lang="raku" line>(3..8).map: -> $p {
my %pow = (^10).map: { $_ => $_ ** $p };
my %pow = (^10).map: { $_ => $_ ** $p };
my $start = 10 ** ($p - 1);
my $start = 10 ** ($p - 1);
Line 1,236: Line 1,236:
}
}
.say for unique sort @temp;
.say for unique sort @temp;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>153
<pre>153
Line 1,259: Line 1,259:
=== Combinations with repetitions ===
=== Combinations with repetitions ===
Using code from [[Combinations with repetitions]] task, a version that runs relatively quickly, and scales well.
Using code from [[Combinations with repetitions]] task, a version that runs relatively quickly, and scales well.
<lang perl6>proto combs_with_rep (UInt, @ ) { * }
<syntaxhighlight lang="raku" line>proto combs_with_rep (UInt, @ ) { * }
multi combs_with_rep (0, @ ) { () }
multi combs_with_rep (0, @ ) { () }
multi combs_with_rep ($, []) { () }
multi combs_with_rep ($, []) { () }
Line 1,274: Line 1,274:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051 88593477 146511208 472335975 534494836 912985153</pre>
<pre>153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 24678050 24678051 88593477 146511208 472335975 534494836 912985153</pre>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
see "Own digits power sum for N = 3 to 9 inclusive:" + nl
see "Own digits power sum for N = 3 to 9 inclusive:" + nl
Line 1,299: Line 1,299:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,331: Line 1,331:
=={{header|Ruby}}==
=={{header|Ruby}}==
Repeated combinations allow for N=18 in less than a minute.
Repeated combinations allow for N=18 in less than a minute.
<lang ruby>DIGITS = (0..9).to_a
<syntaxhighlight lang="ruby">DIGITS = (0..9).to_a
range = (3..18)
range = (3..18)


Line 1,343: Line 1,343:
end
end


puts "Own digits power sums for N = #{range}:", res</lang>
puts "Own digits power sums for N = #{range}:", res</syntaxhighlight>
{{out}}
{{out}}
<pre>Own digits power sums for 3..18
<pre>Own digits power sums for 3..18
Line 1,386: Line 1,386:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func armstrong_numbers(n, base=10) {
<syntaxhighlight lang="ruby">func armstrong_numbers(n, base=10) {


var D = @(^base)
var D = @(^base)
Line 1,405: Line 1,405:
for n in (3..10) {
for n in (3..10) {
say ("For n = #{'%2d' % n}: ", armstrong_numbers(n))
say ("For n = #{'%2d' % n}: ", armstrong_numbers(n))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,420: Line 1,420:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
{{Trans|ALGOL 68}}
<lang vbnet>Option Strict On
<syntaxhighlight lang="vbnet">Option Strict On
Option Explicit On
Option Explicit On


Line 1,611: Line 1,611:




End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,644: Line 1,644:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
Includes some simple optimizations to try and quicken up the search. However, getting up to N = 9 still took a little over 4 minutes on my machine.
Includes some simple optimizations to try and quicken up the search. However, getting up to N = 9 still took a little over 4 minutes on my machine.
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang="ecmascript">import "./math" for Int


var powers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
var powers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Line 1,680: Line 1,680:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,713: Line 1,713:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
Astonishing speed-up. Runtime now only 0.5 seconds!
Astonishing speed-up. Runtime now only 0.5 seconds!
<lang ecmascript>import "./seq" for Lst
<syntaxhighlight lang="ecmascript">import "./seq" for Lst


var maxBase = 10
var maxBase = 10
Line 1,772: Line 1,772:
numbers.sort()
numbers.sort()
System.print("Own digits power sums for N = 3 to 9 inclusive:")
System.print("Own digits power sums for N = 3 to 9 inclusive:")
System.print(numbers.map { |n| n.toString }.join("\n"))</lang>
System.print(numbers.map { |n| n.toString }.join("\n"))</syntaxhighlight>


{{out}}
{{out}}
Line 1,781: Line 1,781:
=={{header|XPL0}}==
=={{header|XPL0}}==
Slow (14.4 second) recursive solution on a Pi4.
Slow (14.4 second) recursive solution on a Pi4.
<lang XPL0>int Num, NumLen, PowTbl(10, 10);
<syntaxhighlight lang="xpl0">int Num, NumLen, PowTbl(10, 10);
proc PowSum(Lev, Sum); \Display own digits power sum
proc PowSum(Lev, Sum); \Display own digits power sum
int Lev, Sum, Dig;
int Lev, Sum, Dig;
Line 1,808: Line 1,808:
NumLen:= 1;
NumLen:= 1;
PowSum(9-1, 0);
PowSum(9-1, 0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}