Jump to content

Own digits power sum: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 21:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V MAX_BASE = 10
V POWER_DIGIT = (0 .< MAX_BASE).map(_ -> (0 .< :MAX_BASE).map(_ -> 1))
V USED_DIGITS = (0 .< MAX_BASE).map(_ -> 0)
Line 73:
print(‘Own digits power sums for N = 3 to 9 inclusive:’)
L(n) NUMBERS
print(n)</langsyntaxhighlight>
 
{{out}}
Line 106:
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.
<langsyntaxhighlight lang="algol68">BEGIN
# 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;
Line 245:
OD;
print( ( "Considered ", whole( try count, 0 ), " digit combinations" ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 278:
Takes about 1.9 seconds to run (GCC 9.3.0 -O3).
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 333:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 343:
{{trans|Pascal}}
Down now to 14ms.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 434:
for (i = 0; i <= j; ++i) printf("%lld\n", numbers[i]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 442:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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)
{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}}
<pre>
Line 458:
</pre>
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
dim as uinteger N, curr, temp, dig, sum
 
Line 473:
next curr
next N
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 484:
{{libheader|Go-rcu}}
Takes about 16.5 seconds to run including compilation time.
<langsyntaxhighlight lang="go">package main
 
import (
Line 535:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 545:
Down to about 128 ms now including compilation time. Actual run time only 8 ms!
{{trans|Pascal}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 644:
fmt.Println(n)
}
}</langsyntaxhighlight>
 
{{out}}
Line 653:
=={{header|Haskell}}==
Using a function from the Combinations with Repetitions task:
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
------------------- OWN DIGITS POWER SUM -----------------
Line 696:
 
digits :: Show a => a -> [Int]
digits n = (\x -> read [x] :: Int) <$> show n</langsyntaxhighlight>
{{Out}}
<pre>N ∈ [3 .. 8]
Line 734:
 
(*) gojq requires significantly more time and memory to run this program.
<langsyntaxhighlight lang="jq">def maxBase: 10;
 
# The global object:
Line 799:
 
"Own digits power sums for N = 3 to 9 inclusive:",
main</langsyntaxhighlight>
{{out}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function isowndigitspowersum(n::Integer, base=10)
dig = digits(n, base=base)
exponent = length(dig)
Line 813:
isowndigitspowersum(i) && println(i)
end
</langsyntaxhighlight>{{out}}
<pre>
153
Line 840:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">cf = Compile[{{n, _Integer}}, Module[{digs, len},
digs = IntegerDigits[n];
len = Length[digs];
Total[digs^len] == n
], CompilationTarget -> "C"];
Select[Range[100, 100000000 - 1], cf]</langsyntaxhighlight>
{{out}}
<pre>{153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477}</pre>
Line 852:
===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.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 890:
$pm->wait_all_children;
 
say $_ for sort { $a <=> $b } map { @$_ } values %own_dps;</langsyntaxhighlight>
{{out}}
<pre>153
Line 918:
===Combinatorics===
Leverage the fact that all combinations of digits give same DPS. Much faster than brute force, as only non-redundant values tested.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'sum';
Line 933:
}
 
print join "\n", sort { $a <=> $b } @own_dps;</langsyntaxhighlight>
{{out}}
<pre>153
Line 959:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,000:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,025:
===Python :: Procedural===
==== slower ====
<langsyntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum """
 
def isowndigitspowersum(integer):
Line 1,037:
if isowndigitspowersum(i):
print(i)
</langsyntaxhighlight>{{out}}Same as Wren example. Takes over a half hour to run.
==== faster ====
{{trans|Wren}} Same output.
<langsyntaxhighlight lang="python">""" Rosetta code task: Own_digits_power_sum (recursive method)"""
 
MAX_BASE = 10
Line 1,095:
print('Own digits power sums for N = 3 to 9 inclusive:')
for n in NUMBERS:
print(n)</langsyntaxhighlight>
 
===Python :: Functional===
Using a function from the Combinations with Repetitions task:
<langsyntaxhighlight lang="python">'''Own digit power sums'''
 
from itertools import accumulate, chain, islice, repeat
Line 1,192:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>N ∈ [3 .. 8]
Line 1,221:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>(3..8).map: -> $p {
my %pow = (^10).map: { $_ => $_ ** $p };
my $start = 10 ** ($p - 1);
Line 1,236:
}
.say for unique sort @temp;
}</langsyntaxhighlight>
{{out}}
<pre>153
Line 1,259:
=== Combinations with repetitions ===
Using code from [[Combinations with repetitions]] task, a version that runs relatively quickly, and scales well.
<syntaxhighlight lang="raku" perl6line>proto combs_with_rep (UInt, @ ) { * }
multi combs_with_rep (0, @ ) { () }
multi combs_with_rep ($, []) { () }
Line 1,274:
}
}
}</langsyntaxhighlight>
{{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>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Own digits power sum for N = 3 to 9 inclusive:" + nl
Line 1,299:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,331:
=={{header|Ruby}}==
Repeated combinations allow for N=18 in less than a minute.
<langsyntaxhighlight lang="ruby">DIGITS = (0..9).to_a
range = (3..18)
 
Line 1,343:
end
 
puts "Own digits power sums for N = #{range}:", res</langsyntaxhighlight>
{{out}}
<pre>Own digits power sums for 3..18
Line 1,386:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func armstrong_numbers(n, base=10) {
 
var D = @(^base)
Line 1,405:
for n in (3..10) {
say ("For n = #{'%2d' % n}: ", armstrong_numbers(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,420:
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="vbnet">Option Strict On
Option Explicit On
 
Line 1,611:
 
 
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 1,644:
{{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.
<langsyntaxhighlight lang="ecmascript">import "./math" for Int
 
var powers = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Line 1,680:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,713:
{{libheader|Wren-seq}}
Astonishing speed-up. Runtime now only 0.5 seconds!
<langsyntaxhighlight lang="ecmascript">import "./seq" for Lst
 
var maxBase = 10
Line 1,772:
numbers.sort()
System.print("Own digits power sums for N = 3 to 9 inclusive:")
System.print(numbers.map { |n| n.toString }.join("\n"))</langsyntaxhighlight>
 
{{out}}
Line 1,781:
=={{header|XPL0}}==
Slow (14.4 second) recursive solution on a Pi4.
<langsyntaxhighlight XPL0lang="xpl0">int Num, NumLen, PowTbl(10, 10);
proc PowSum(Lev, Sum); \Display own digits power sum
int Lev, Sum, Dig;
Line 1,808:
NumLen:= 1;
PowSum(9-1, 0);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

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