Smallest numbers: Difference between revisions
Content added Content deleted
m (→{{header|J}}: task asks for positive k) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8: | Line 8: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V numLimit = 51 |
||
[Int = Int] resultSet |
[Int = Int] resultSet |
||
Line 24: | Line 24: | ||
L(i) sorted(resultSet.keys()) |
L(i) sorted(resultSet.keys()) |
||
print(resultSet[i], end' ‘ ’)</ |
print(resultSet[i], end' ‘ ’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 34: | Line 34: | ||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
Uses ALGOL 68G's LONG LONG INT which provides large integers (the default precision is sufficient for the task). Also uses the ALGOL 68G string in string procedure. |
Uses ALGOL 68G's LONG LONG INT which provides large integers (the default precision is sufficient for the task). Also uses the ALGOL 68G string in string procedure. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find the smallest k such that the decimal representation of k^k contains n for 0 <= n <= 50 # |
||
# start with powers up to 20^20, if this proves insufficient, the kk array will be extended # |
# start with powers up to 20^20, if this proves insufficient, the kk array will be extended # |
||
FLEX[ 1 : 20 ]STRING kk; |
FLEX[ 1 : 20 ]STRING kk; |
||
Line 60: | Line 60: | ||
IF i MOD 10 = 9 THEN print( ( newline ) ) FI |
IF i MOD 10 = 9 THEN print( ( newline ) ) FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 72: | Line 72: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Smallest number: Nigel Galloway. April 13th., 2021 |
// 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 |
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 "" |
{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> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 84: | Line 84: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: formatting grouping io kernel lists lists.lazy |
||
math.functions present sequences ; |
math.functions present sequences ; |
||
Line 91: | Line 91: | ||
51 <iota> [ smallest ] map 10 group |
51 <iota> [ smallest ] map 10 group |
||
[ [ "%3d" printf ] each nl ] each</ |
[ [ "%3d" printf ] each nl ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 104: | Line 104: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Reuses some code from [[Arbitrary-precision_integers_(included)#FreeBASIC]]. |
Reuses some code from [[Arbitrary-precision_integers_(included)#FreeBASIC]]. |
||
< |
<syntaxhighlight lang="freebasic">#Include once "gmp.bi" |
||
Dim Shared As Zstring * 100000000 outtext |
Dim Shared As Zstring * 100000000 outtext |
||
Line 138: | Line 138: | ||
loop until is_substring( Power(str(k), k), str(i) ) |
loop until is_substring( Power(str(k), k), str(i) ) |
||
print k;" "; |
print k;" "; |
||
next i</ |
next i</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> |
{{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}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 174: | Line 174: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 186: | Line 186: | ||
=={{header|J}}== |
=={{header|J}}== |
||
N,K: |
N,K: |
||
< |
<syntaxhighlight lang="j"> (,.1>.{.@I.@(+./@E.&":"0/ ^~))i.51x |
||
0 9 |
0 9 |
||
1 1 |
1 1 |
||
Line 237: | Line 237: | ||
48 9 |
48 9 |
||
49 24 |
49 24 |
||
50 23</ |
50 23</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
The integer precision of stedolan jq is insufficient for this task.< |
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 |
# 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 power($b): . as $a | reduce range(0; $b) as $i (1; . * $a); |
||
Line 249: | Line 249: | ||
tostring as $n |
tostring as $n |
||
| first( range(1; infinite) | select( power(.) | tostring | contains($n))) ; |
| first( range(1; infinite) | select( power(.) | tostring | contains($n))) ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
# Formatting |
# Formatting |
||
Line 258: | Line 258: | ||
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
||
n; |
n; |
||
</ |
</syntaxhighlight>The task:<syntaxhighlight lang="jq"> |
||
def task($n): |
def task($n): |
||
[range(0; $n) | smallest_k | lpad(3) ] |
[range(0; $n) | smallest_k | lpad(3) ] |
||
Line 264: | Line 264: | ||
| join(" "); |
| join(" "); |
||
task(51)</ |
task(51)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
As for Factor, for example. |
As for Factor, for example. |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">hasinktok(n) = for k in 1:100000 contains("$(BigInt(k)^k)", "$n") && return k end |
||
foreach(p -> print(rpad(p[2], 4), p[1] % 17 == 0 ? "\n" : ""), enumerate(map(hasinktok, 0:50))) |
foreach(p -> print(rpad(p[2], 4), p[1] % 17 == 0 ? "\n" : ""), enumerate(map(hasinktok, 0:50))) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 |
9 1 3 5 2 4 4 3 7 9 10 11 5 19 22 26 8 |
||
Line 280: | Line 280: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[FindSmallestk] |
||
FindSmallestk[n_Integer] := Module[{digs, id, out}, |
FindSmallestk[n_Integer] := Module[{digs, id, out}, |
||
id = IntegerDigits[n]; |
id = IntegerDigits[n]; |
||
Line 291: | Line 291: | ||
out |
out |
||
] |
] |
||
Multicolumn[FindSmallestk /@ Range[0, 50], Appearance -> "Horizontal"]</ |
Multicolumn[FindSmallestk /@ Range[0, 50], Appearance -> "Horizontal"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9 1 3 5 2 4 4 |
<pre>9 1 3 5 2 4 4 |
||
Line 305: | Line 305: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<syntaxhighlight lang="nim">import strformat, strutils |
||
import bignum |
import bignum |
||
Line 322: | Line 322: | ||
for n, k in results: |
for n, k in results: |
||
stdout.write &"{n:2} → {k:<2} ", if (n + 1) mod 9 == 0: '\n' else: ' ' |
stdout.write &"{n:2} → {k:<2} ", if (n + 1) mod 9 == 0: '\n' else: ' ' |
||
echo()</ |
echo()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 336: | Line 336: | ||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
made like Phix but own multiplikation to BASE 1E9 [[Smallest_power_of_6_whose_decimal_expansion_contains_n#Pascal|here]] |
made like Phix but own multiplikation to BASE 1E9 [[Smallest_power_of_6_whose_decimal_expansion_contains_n#Pascal|here]] |
||
< |
<syntaxhighlight lang="pascal">program K_pow_K; |
||
//First occurence of a numberstring with max DIGTIS digits in k^k |
//First occurence of a numberstring with max DIGTIS digits in k^k |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
Line 513: | Line 513: | ||
Out_Results(number,found); |
Out_Results(number,found); |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 550: | Line 550: | ||
</pre> |
</pre> |
||
===gmp-version=== |
===gmp-version=== |
||
< |
<syntaxhighlight lang="pascal">program K_pow_K_gmp; |
||
//First occurence of a numberstring with max DIGTIS digits in k^k |
//First occurence of a numberstring with max DIGTIS digits in k^k |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
Line 657: | Line 657: | ||
until number>9604;// found >=decLimit; |
until number>9604;// found >=decLimit; |
||
Out_Results(number,found); |
Out_Results(number,found); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 669: | Line 669: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 676: | Line 676: | ||
sub smallest { first { ipow($_,$_) =~ /$_[0]/ } 1..1e4 } |
sub smallest { first { ipow($_,$_) =~ /$_[0]/ } 1..1e4 } |
||
say join ' ', map { smallest($_) } 0..50;</ |
say join ' ', map { smallest($_) } 0..50;</syntaxhighlight> |
||
{{out}} |
{{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> |
<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 684: | Line 684: | ||
(Related recent tasks: [[Smallest_power_of_6_whose_decimal_expansion_contains_n#Phix|here]] and |
(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]]) |
[[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix|here]]) |
||
<!--< |
<!--<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: #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;">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> |
||
Line 723: | Line 723: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 735: | Line 735: | ||
Testing to 1,000,000 took 12mins 35s. |
Testing to 1,000,000 took 12mins 35s. |
||
===gmp version=== |
===gmp version=== |
||
<!--< |
<!--<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: #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;">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> |
||
Line 765: | Line 765: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
Same results, but nearly 30 times faster, finishing the 1,000,000 test in just 26.6s |
Same results, but nearly 30 times faster, finishing the 1,000,000 test in just 26.6s |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Interactive script which takes the upper bound as input : |
Interactive script which takes the upper bound as input : |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
#Aamrun, 4th October 2021 |
#Aamrun, 4th October 2021 |
||
Line 795: | Line 795: | ||
[print(resultSet[i], end=' ') for i in sorted(resultSet)] |
[print(resultSet[i], end=' ') for i in sorted(resultSet)] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 802: | Line 802: | ||
</pre> |
</pre> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub smallest ( $n ) { |
||
state @powers = '', |map { $_ ** $_ }, 1 .. *; |
state @powers = '', |map { $_ ** $_ }, 1 .. *; |
||
Line 808: | Line 808: | ||
} |
} |
||
.say for (^51).map(&smallest).batch(10)».fmt('%2d');</ |
.say for (^51).map(&smallest).batch(10)».fmt('%2d');</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 821: | Line 821: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Code was added to display the count of unique numbers found. |
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 */ |
numeric digits 200 /*ensure enough decimal digs for k**k */ |
||
parse arg hi cols . /*obtain optional argument from the CL.*/ |
parse arg hi cols . /*obtain optional argument from the CL.*/ |
||
Line 849: | Line 849: | ||
exit 0 /*stick a fork in it, we're all done. */ |
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 ?</ |
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 866: | Line 866: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "bignumber.ring" |
load "bignumber.ring" |
||
Line 899: | Line 899: | ||
num2 = string(num2) |
num2 = string(num2) |
||
return FuncPower(num1,num2) |
return FuncPower(num1,num2) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 914: | Line 914: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">0..50 -> map {|n| 1..Inf -> first {|k| Str(k**k).contains(n) } }.say</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 924: | Line 924: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/big" for BigInt |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 941: | Line 941: | ||
} |
} |
||
System.print("The smallest positive integers K where K ^ K contains N (0..50) are:") |
System.print("The smallest positive integers K where K ^ K contains N (0..50) are:") |
||
for (chunk in Lst.chunks(res, 17)) Fmt.print("$2d", chunk)</ |
for (chunk in Lst.chunks(res, 17)) Fmt.print("$2d", chunk)</syntaxhighlight> |
||
{{out}} |
{{out}} |