Smallest numbers: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Added Go) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(20 intermediate revisions by 14 users not shown) | |||
Line 2:
;Task:
Smallest
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V numLimit = 51
[Int = Int] resultSet
V base = 1
L resultSet.len != numLimit
V result = String(BigInt(base) ^ base)
L(i) 0 .< numLimit
I String(i) C result & i !C resultSet
resultSet[i] = base
base++
L(i) sorted(resultSet.keys())
print(resultSet[i], end' ‘ ’)</syntaxhighlight>
{{out}}
<pre>
9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23
</pre>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses ALGOL 68G's
<
# start with powers up to 20^20, if this proves insufficient, the kk array will be extended #
FLEX[ 1 : 20 ]STRING kk;
Line 34 ⟶ 60:
IF i MOD 10 = 9 THEN print( ( newline ) ) FI
OD
END</
{{out}}
<pre>
Line 46 ⟶ 72:
=={{header|F_Sharp|F#}}==
<
// Smallest number: Nigel Galloway. April 13th., 2021
let rec fG n g=match bigint.DivRem(n,if g<10 then 10I else 100I) with (_,n) when (int n)=g->true |(n,_) when n=0I->false |(n,_)->fG n g
{0..50}|>Seq.iter(fun g->printf "%d " (1+({1..0x0FFFFFFF}|>Seq.map(fun n->(bigint n)**n)|>Seq.findIndex(fun n->fG n g)))); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 58 ⟶ 84:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
math.functions present sequences ;
Line 65 ⟶ 91:
51 <iota> [ smallest ] map 10 group
[ [ "%3d" printf ] each nl ] each</
{{out}}
<pre>
Line 78 ⟶ 104:
=={{header|FreeBASIC}}==
Reuses some code from [[Arbitrary-precision_integers_(included)#FreeBASIC]].
<
Dim Shared As Zstring * 100000000 outtext
Line 112 ⟶ 138:
loop until is_substring( Power(str(k), k), str(i) )
print k;" ";
next i</
{{out}}<pre> 9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23</pre>
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 148 ⟶ 174:
}
}
}</
{{out}}
Line 157 ⟶ 183:
18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23
</pre>
=={{header|J}}==
N,K:
<syntaxhighlight lang="j"> (,.1>.{.@I.@(+./@E.&":"0/ ^~))i.51x
0 9
1 1
2 3
3 5
4 2
5 4
6 4
7 3
8 7
9 9
10 10
11 11
12 5
13 19
14 22
15 26
16 8
17 17
18 16
19 19
20 9
21 8
22 13
23 7
24 17
25 4
26 17
27 3
28 11
29 18
30 13
31 5
32 23
33 17
34 18
35 7
36 17
37 15
38 9
39 18
40 16
41 17
42 9
43 7
44 12
45 28
46 6
47 23
48 9
49 24
50 23</syntaxhighlight>
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
The integer precision of stedolan jq is insufficient for this task.<syntaxhighlight lang="jq">
# if the input and $b are integers, then gojq will preserve precision
def power($b): . as $a | reduce range(0; $b) as $i (1; . * $a);
def smallest_k:
tostring as $n
| first( range(1; infinite) | select( power(.) | tostring | contains($n))) ;
</syntaxhighlight>
<syntaxhighlight lang="jq">
# Formatting
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
</syntaxhighlight>The task:<syntaxhighlight lang="jq">
def task($n):
[range(0; $n) | smallest_k | lpad(3) ]
| nwise(10)
| join(" ");
task(51)</syntaxhighlight>
{{out}}
As for Factor, for example.
=={{header|Julia}}==
<
foreach(p -> print(rpad(p[2], 4), p[1] % 17 == 0 ? "\n" : ""), enumerate(map(hasinktok, 0:50)))
</
<pre>
9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8
17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17
18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[FindSmallestk]
FindSmallestk[n_Integer] := Module[{digs, id, out},
id = IntegerDigits[n];
Do[
digs = IntegerDigits[k^k];
If[Length[SequenceCases[digs, id, 1]] > 0, out = k; Break[]]
,
{k, 1, \[Infinity]}
];
out
]
Multicolumn[FindSmallestk /@ Range[0, 50], Appearance -> "Horizontal"]</syntaxhighlight>
{{out}}
<pre>9 1 3 5 2 4 4
3 7 9 10 11 5 19
22 26 8 17 16 19 9
8 13 7 17 4 17 3
11 18 13 5 23 17 18
7 17 15 9 18 16 17
9 7 12 28 6 23 9
24 23
</pre>
=={{header|Nim}}==
{{libheader|bignum}}
<
import bignum
Line 188 ⟶ 322:
for n, k in results:
stdout.write &"{n:2} → {k:<2} ", if (n + 1) mod 9 == 0: '\n' else: ' '
echo()</
{{out}}
Line 202 ⟶ 336:
{{works with|Free Pascal}}
made like Phix but own multiplikation to BASE 1E9 [[Smallest_power_of_6_whose_decimal_expansion_contains_n#Pascal|here]]
<
//First occurence of a numberstring with max DIGTIS digits in k^k
{$IFDEF FPC}
Line 379 ⟶ 513:
Out_Results(number,found);
end.
</syntaxhighlight>
{{out}}
<pre>
Line 416 ⟶ 550:
</pre>
===gmp-version===
<
//First occurence of a numberstring with max DIGTIS digits in k^k
{$IFDEF FPC}
Line 523 ⟶ 657:
until number>9604;// found >=decLimit;
Out_Results(number,found);
end.</
{{out}}
<pre>
Line 535 ⟶ 669:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 542 ⟶ 676:
sub smallest { first { ipow($_,$_) =~ /$_[0]/ } 1..1e4 }
say join ' ', map { smallest($_) } 0..50;</
{{out}}
<pre>9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23</pre>
Line 550 ⟶ 684:
(Related recent tasks: [[Smallest_power_of_6_whose_decimal_expansion_contains_n#Phix|here]] and
[[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|here]])
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">51</span> <span style="color: #000080;font-style:italic;">-- (tested to 1,000,000)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
Line 588 ⟶ 723:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 600 ⟶ 735:
Testing to 1,000,000 took 12mins 35s.
===gmp version===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">51</span> <span style="color: #000080;font-style:italic;">-- (tested to 1,000,000)</span>
<span style="color: #008080;">include</span> <span style="color: #
<span style="color: #
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">(),</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">)</span>
Line 629 ⟶ 765:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<!--</
Same results, but nearly 30 times faster, finishing the 1,000,000 test in just 26.6s
=={{header|Python}}==
Interactive script which takes the upper bound as input :
<syntaxhighlight lang="python">
#Aamrun, 4th October 2021
import sys
if len(sys.argv)!=2:
print("Usage : python " + sys.argv[0] + " <whole number>")
exit()
numLimit = int(sys.argv[1])
resultSet = {}
base = 1
while len(resultSet)!=numLimit:
result = base**base
for i in range(0,numLimit):
if str(i) in str(result) and i not in resultSet:
resultSet[i] = base
base+=1
[print(resultSet[i], end=' ') for i in sorted(resultSet)]
</syntaxhighlight>
{{out}}
<pre>
C:\My Projects\BGI>python rosetta9.py 51
9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23
</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery">
[ stack ] is candidates ( --> s )
[ stack ] is results ( --> s )
[ [] swap times
[ i^ number$
nested join ]
candidates put
[] results put
0
[ 1+ dup
dup ** number$
candidates share
reverse witheach
[ over 2dup findseq
swap found iff
[ dip over
$->n drop
join nested
results take
join
results put
candidates take
i pluck drop
candidates put ]
else drop ]
drop
candidates share
[] = until ]
drop
candidates release
results take
sortwith
[ 1 peek swap 1 peek < ]
[] swap
witheach [ 0 peek join ] ] is task ( n --> [ )
51 task echo</syntaxhighlight>
{{out}}
<pre>[ 9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23 ]</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
state @powers = '', |map { $_ ** $_ }, 1 .. *;
Line 639 ⟶ 854:
}
.say for (^51).map(&smallest).batch(10)».fmt('%2d');</
{{out}}
<pre>
Line 651 ⟶ 866:
=={{header|REXX}}==
Code was added to display the count of unique numbers found.
<syntaxhighlight lang="rexx">/*REXX pgm finds the smallest positive integer K where K**K contains N, N < 51 */
numeric digits 200 /*ensure enough decimal digs for k**k */
parse arg hi cols . /*obtain optional argument from the CL.*/
Line 657 ⟶ 873:
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 6 /*width of a number in any column. */
say ' N │'center(
say '─────┼'center(""
$=;
do j=0 for hi /*look for a power of K that contains N*/
do k=1 until pos(j, k**k)>0 /*calculate a bunch of powers (K**K). */
end /*k*/
if !.k==. then do; u= u+1; !.k=; end /*Is unique? Then bump unique counter.*/
c= commas(k) /*maybe add commas to the powe of six. */
$= $ right(c, max(w, length(c) ) ) /*add a K (power) ──► list, allow big#*/
if
say center(idx, 5)'│'substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
Line 672 ⟶ 890:
if $\=='' then say center(idx, 5)"│"substr($,2) /*possible display any residual output.*/
say '─────┴'center(""
say
say commas(u) ' unique numbers found.'
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</
{{out|output|text= when using the default inputs:}}
<pre>
Line 687 ⟶ 907:
50 │ 23
─────┴───────────────────────────────────────────────────────────────────────────
23 unique numbers found.
</pre>
=={{header|Ring}}==
<
load "bignumber.ring"
Line 723 ⟶ 945:
num2 = string(num2)
return FuncPower(num1,num2)
</syntaxhighlight>
{{out}}
<pre>
Line 735 ⟶ 957:
23
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« { }
0 50 '''FOR''' n
1
'''WHILE''' DUP DUP ^ →STR n →STR POS NOT
'''REPEAT''' 1 + '''END'''
+
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 17 16 19 9 8 13 7 17 4 17 3 11 18 13 5 23 17 18 7 17 15 9 18 16 17 9 7 12 28 6 23 9 24 23}
</pre>
=={{header|Ruby}}==
Using a hash as memo:
<syntaxhighlight lang="ruby">memo = Hash.new{|h, k| h[k] = (k**k).to_s }
res = (0..50).map{|n| (1..).detect{|m| memo[m].include? n.to_s} }
res.each_slice(10){|slice| puts "%4d"*slice.size % slice }
</syntaxhighlight>
{{out}}
<pre> 9 1 3 5 2 4 4 3 7 9
10 11 5 19 22 26 8 17 16 19
9 8 13 7 17 4 17 3 11 18
13 5 23 17 18 7 17 15 9 18
16 17 9 7 12 28 6 23 9 24
23
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">0..50 -> map {|n| 1..Inf -> first {|k| Str(k**k).contains(n) } }.say</syntaxhighlight>
{{out}}
<pre>
[9, 1, 3, 5, 2, 4, 4, 3, 7, 9, 10, 11, 5, 19, 22, 26, 8, 17, 16, 19, 9, 8, 13, 7, 17, 4, 17, 3, 11, 18, 13, 5, 23, 17, 18, 7, 17, 15, 9, 18, 16, 17, 9, 7, 12, 28, 6, 23, 9, 24, 23]
</pre>
=={{header|Wren}}==
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "./
var res = []
Line 758 ⟶ 1,015:
}
System.print("The smallest positive integers K where K ^ K contains N (0..50) are:")
{{out}}
|