Smallest power of 6 whose decimal expansion contains n: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10: | Line 10: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F smallest_six(n) |
||
V p = BigInt(1) |
V p = BigInt(1) |
||
L String(n) !C String(p) |
L String(n) !C String(p) |
||
Line 17: | Line 17: | ||
L(n) 22 |
L(n) 22 |
||
print(‘#2: #.’.format(n, smallest_six(n)))</ |
print(‘#2: #.’.format(n, smallest_six(n)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 48: | Line 48: | ||
{{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 large integers, the default precision is sufficient for this task. Also uses the ALGOL 68G specific string in string procedure. |
Uses ALGOL 68G's LONG LONG INT large integers, the default precision is sufficient for this task. Also uses the ALGOL 68G specific string in string procedure. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find the smallest k such that the decimal representation of 6^k contains n for 0 <= n <= 21 # |
||
# returns s blank-padded on the right to at least len characters # |
# returns s blank-padded on the right to at least len characters # |
||
PROC right pad = ( STRING s, INT len )STRING: |
PROC right pad = ( STRING s, INT len )STRING: |
||
Line 98: | Line 98: | ||
OD |
OD |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 127: | Line 127: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Algol W doesn't have integers larger than 32 bits, however we can handle the required numbers with arrays of digits. |
Algol W doesn't have integers larger than 32 bits, however we can handle the required numbers with arrays of digits. |
||
< |
<syntaxhighlight lang="algolw">begin % find the smallest power of 6 that contains n for 0 <= n <= 21 % |
||
% we assume that powers of 6 upto 6^32 will be sufficient % |
% we assume that powers of 6 upto 6^32 will be sufficient % |
||
% as Algol W does not have integers longer than 32 bits, the powers % |
% as Algol W does not have integers longer than 32 bits, the powers % |
||
Line 184: | Line 184: | ||
for d := digits( p ) step -1 until 1 do writeon( i_w := 1, s_w := 0, powers( p, d ) ) |
for d := digits( p ) step -1 until 1 do writeon( i_w := 1, s_w := 0, powers( p, d ) ) |
||
end for_n |
end for_n |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 212: | Line 212: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SMALLEST_POWER_OF_6_WHOSE_DECIMAL_EXPANSION_CONTAINS_N.AWK |
# syntax: GAWK -f SMALLEST_POWER_OF_6_WHOSE_DECIMAL_EXPANSION_CONTAINS_N.AWK |
||
BEGIN { |
BEGIN { |
||
Line 227: | Line 227: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 256: | Line 256: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <gmp.h> |
#include <gmp.h> |
||
Line 289: | Line 289: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 317: | Line 317: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
#include <string> |
#include <string> |
||
Line 339: | Line 339: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 366: | Line 366: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% This program uses the bigint type that comes with PCLU. |
||
% It is in "misc.lib" |
% It is in "misc.lib" |
||
% |
% |
||
Line 400: | Line 400: | ||
stream$putl(po, "") |
stream$putl(po, "") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0: 6^9 = 10077696 |
<pre> 0: 6^9 = 10077696 |
||
Line 426: | Line 426: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Nigel Galloway. April 9th., 2021 |
// Nigel Galloway. April 9th., 2021 |
||
let rec fN i g e l=match l%i=g,l/10I with (true,_)->e |(_,l) when l=0I->fN i g (e*6I) (e*6I) |(_,l)->fN i g e l |
let rec fN i g e l=match l%i=g,l/10I with (true,_)->e |(_,l) when l=0I->fN i g (e*6I) (e*6I) |(_,l)->fN i g e l |
||
[0I..99I]|>Seq.iter(fun n->printfn "%2d %A" (int n)(fN(if n>9I then 100I else 10I) n 1I 1I)) |
[0I..99I]|>Seq.iter(fun n->printfn "%2d %A" (int n)(fN(if n>9I then 100I else 10I) n 1I 1I)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 538: | Line 538: | ||
=={{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 kernel lists lists.lazy math math.functions |
||
present sequences tools.memory.private ; |
present sequences tools.memory.private ; |
||
Line 547: | Line 547: | ||
present powers-of-6 [ present subseq? ] with lfilter car ; |
present powers-of-6 [ present subseq? ] with lfilter car ; |
||
22 [ dup smallest commas "%2d %s\n" printf ] each-integer</ |
22 [ dup smallest commas "%2d %s\n" printf ] each-integer</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 577: | Line 577: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|Ring}} |
{{trans|Ring}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Print !"\ntrabajando...\n" |
Print !"\ntrabajando...\n" |
||
Print !"M¡nima potencia de 6 cuya expansi¢n decimal contiene n:\n" |
Print !"M¡nima potencia de 6 cuya expansi¢n decimal contiene n:\n" |
||
Line 597: | Line 597: | ||
Print !"\n--- terminado, pulsa RETURN---" |
Print !"\n--- terminado, pulsa RETURN---" |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 644: | Line 644: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 674: | Line 674: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (isInfixOf) |
||
import Text.Printf (printf) |
import Text.Printf (printf) |
||
Line 692: | Line 692: | ||
concatMap |
concatMap |
||
(printf "%2d: %d\n" <*> smallest) |
(printf "%2d: %d\n" <*> smallest) |
||
[0 .. 21]</ |
[0 .. 21]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 723: | Line 723: | ||
gojq provides unbounded-precision integer arithmetic and is therefore appropriate for this task. |
gojq provides unbounded-precision integer arithmetic and is therefore appropriate for this task. |
||
< |
<syntaxhighlight lang="jq"># To preserve precision: |
||
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in); |
||
Line 730: | Line 730: | ||
| tostring as $n |
| tostring as $n |
||
| first(range(0;infinite) as $i | 6 | power($i) | . as $p | tostring | (index($n) // empty) |
| first(range(0;infinite) as $i | 6 | power($i) | . as $p | tostring | (index($n) // empty) |
||
| [$in,$i,$p] )</ |
| [$in,$i,$p] )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 759: | Line 759: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Formatting |
||
digcontains(n, dig) = contains(String(Char.(digits(n))), String(Char.(dig))) |
digcontains(n, dig) = contains(String(Char.(digits(n))), String(Char.(dig))) |
||
Line 775: | Line 775: | ||
println(rpad(n, 5), format(findpow6containing(n), commas=true)) |
println(rpad(n, 5), format(findpow6containing(n), commas=true)) |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
0 10,077,696 |
0 10,077,696 |
||
Line 802: | Line 802: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[SmallestPowerContainingN] |
||
SmallestPowerContainingN[n_Integer] := Module[{i = 1, test}, |
SmallestPowerContainingN[n_Integer] := Module[{i = 1, test}, |
||
While[True, |
While[True, |
||
Line 811: | Line 811: | ||
] |
] |
||
] |
] |
||
Grid[SmallestPowerContainingN /@ Range[0, 21]]</ |
Grid[SmallestPowerContainingN /@ Range[0, 21]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 9 10077696 |
<pre>0 9 10077696 |
||
Line 838: | Line 838: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
< |
<syntaxhighlight lang="nim">import strformat, strutils |
||
import bignum |
import bignum |
||
Line 856: | Line 856: | ||
echo "Smallest values of k such that 6^k contains n:" |
echo "Smallest values of k such that 6^k contains n:" |
||
for n, (k, s) in results: |
for n, (k, s) in results: |
||
echo &"{n:2}: 6^{k:<2} = {s}"</ |
echo &"{n:2}: 6^{k:<2} = {s}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 886: | Line 886: | ||
{{Works with|Free Pascal}} |
{{Works with|Free Pascal}} |
||
Doing long multiplikation like in primorial task.<BR>I used to check every numberstring one after the other on one 6^ n string.Gets really slow on high n<BR>After a closer look into [[Phix|Smallest_power_of_6_whose_decimal_expansion_contains_n#Phix]] I applied a slghtly modified version of Pete, to get down < 10 secs on my 2200G for DIGITS = 7.TIO.RUN is slower. |
Doing long multiplikation like in primorial task.<BR>I used to check every numberstring one after the other on one 6^ n string.Gets really slow on high n<BR>After a closer look into [[Phix|Smallest_power_of_6_whose_decimal_expansion_contains_n#Phix]] I applied a slghtly modified version of Pete, to get down < 10 secs on my 2200G for DIGITS = 7.TIO.RUN is slower. |
||
< |
<syntaxhighlight lang="pascal">program PotOf6; |
||
//First occurence of a numberstring with max DIGTIS digits in 6^n |
//First occurence of a numberstring with max DIGTIS digits in 6^n |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
Line 1,085: | Line 1,085: | ||
writeln(i:10,' ',number,'^',foundIndex-1:5,' ',foundStr); |
writeln(i:10,' ',number,'^',foundIndex-1:5,' ',foundStr); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,145: | Line 1,145: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use List::Util 'first'; |
use List::Util 'first'; |
||
Line 1,155: | Line 1,155: | ||
my $e = first { 6**$_ =~ /$n/ } 0..1000; |
my $e = first { 6**$_ =~ /$n/ } 0..1000; |
||
printf "%7d: 6^%-3s %s\n", $n, $e, comma 6**$e; |
printf "%7d: 6^%-3s %s\n", $n, $e, comma 6**$e; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0: 6^9 10,077,696 |
<pre> 0: 6^9 10,077,696 |
||
Line 1,185: | Line 1,185: | ||
(Related recent task: [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix]]) |
(Related recent task: [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix]]) |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">22</span> <span style="color: #000080;font-style:italic;">-- (tested to 10,000,000)</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">lim</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">22</span> <span style="color: #000080;font-style:italic;">-- (tested to 10,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> |
<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 1,231: | Line 1,231: | ||
<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;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%2d %29s = 6^%d\n"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pwr</span><span style="color: #0000FF;">}),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span> |
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">printf</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"%2d %29s = 6^%d\n"</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pwr</span><span style="color: #0000FF;">}),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 1,261: | Line 1,261: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def smallest_six(n): |
||
p = 1 |
p = 1 |
||
while str(n) not in str(p): p *= 6 |
while str(n) not in str(p): p *= 6 |
||
Line 1,267: | Line 1,267: | ||
for n in range(22): |
for n in range(22): |
||
print("{:2}: {}".format(n, smallest_six(n)))</ |
print("{:2}: {}".format(n, smallest_six(n)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0: 10077696 |
<pre> 0: 10077696 |
||
Line 1,294: | Line 1,294: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; |
||
my @po6 = ^Inf .map: *.exp: 6; |
my @po6 = ^Inf .map: *.exp: 6; |
||
Line 1,301: | Line 1,301: | ||
sprintf "%3d: 6%-4s %s", $n, .&super, comma @po6[$_] |
sprintf "%3d: 6%-4s %s", $n, .&super, comma @po6[$_] |
||
given @po6.first: *.contains($n), :k |
given @po6.first: *.contains($n), :k |
||
};</ |
};</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0: 6⁹ 10,077,696 |
<pre> 0: 6⁹ 10,077,696 |
||
Line 1,328: | Line 1,328: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm finds the smallest (decimal) power of 6 which contains N, where N < 22. */ |
||
numeric digits 100 /*ensure enough decimal digs for 6**N */ |
numeric digits 100 /*ensure enough decimal digs for 6**N */ |
||
parse arg hi . /*obtain optional argument from the CL.*/ |
parse arg hi . /*obtain optional argument from the CL.*/ |
||
Line 1,349: | Line 1,349: | ||
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 input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 1,380: | Line 1,380: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,403: | Line 1,403: | ||
see "done..." + nl |
see "done..." + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,436: | Line 1,436: | ||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/big" for BigInt |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,451: | Line 1,451: | ||
i = i + 1 |
i = i + 1 |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,482: | Line 1,482: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_power_of_6_whose_decimal_expansion_contains_n |
||
// by Galileo, 05/2022 |
// by Galileo, 05/2022 |
||
Line 1,494: | Line 1,494: | ||
end sub |
end sub |
||
for n = 0 to 21 : print n, ": ", str$(smallest_six(n), "%1.f") : next</ |
for n = 0 to 21 : print n, ": ", str$(smallest_six(n), "%1.f") : next</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0: 10077696 |
<pre>0: 10077696 |