Smallest power of 6 whose decimal expansion contains n: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 10:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F smallest_six(n)
V p = BigInt(1)
L String(n) !C String(p)
Line 17:
 
L(n) 22
print(‘#2: #.’.format(n, smallest_six(n)))</langsyntaxhighlight>
 
{{out}}
Line 48:
{{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.
<langsyntaxhighlight 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 #
PROC right pad = ( STRING s, INT len )STRING:
Line 98:
OD
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 127:
=={{header|ALGOL W}}==
Algol W doesn't have integers larger than 32 bits, however we can handle the required numbers with arrays of digits.
<langsyntaxhighlight 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 %
% as Algol W does not have integers longer than 32 bits, the powers %
Line 184:
for d := digits( p ) step -1 until 1 do writeon( i_w := 1, s_w := 0, powers( p, d ) )
end for_n
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 212:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SMALLEST_POWER_OF_6_WHOSE_DECIMAL_EXPANSION_CONTAINS_N.AWK
BEGIN {
Line 227:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 256:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <string.h>
#include <gmp.h>
Line 289:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 317:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
#include <string>
Line 339:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 366:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This program uses the bigint type that comes with PCLU.
% It is in "misc.lib"
%
Line 400:
stream$putl(po, "")
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 0: 6^9 = 10077696
Line 426:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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
[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}}
<pre>
Line 538:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: formatting kernel lists lists.lazy math math.functions
present sequences tools.memory.private ;
 
Line 547:
present powers-of-6 [ present subseq? ] with lfilter car ;
 
22 [ dup smallest commas "%2d %s\n" printf ] each-integer</langsyntaxhighlight>
{{out}}
<pre>
Line 577:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<langsyntaxhighlight lang="freebasic">
Print !"\ntrabajando...\n"
Print !"M¡nima potencia de 6 cuya expansi¢n decimal contiene n:\n"
Line 597:
Print !"\n--- terminado, pulsa RETURN---"
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 644:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 674:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (isInfixOf)
import Text.Printf (printf)
 
Line 692:
concatMap
(printf "%2d: %d\n" <*> smallest)
[0 .. 21]</langsyntaxhighlight>
 
{{out}}
Line 723:
 
gojq provides unbounded-precision integer arithmetic and is therefore appropriate for this task.
<langsyntaxhighlight lang="jq"># To preserve precision:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
Line 730:
| tostring as $n
| first(range(0;infinite) as $i | 6 | power($i) | . as $p | tostring | (index($n) // empty)
| [$in,$i,$p] )</langsyntaxhighlight>
{{out}}
<pre>
Line 759:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
digcontains(n, dig) = contains(String(Char.(digits(n))), String(Char.(dig)))
Line 775:
println(rpad(n, 5), format(findpow6containing(n), commas=true))
end
</langsyntaxhighlight>{{out}}
<pre>
0 10,077,696
Line 802:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[SmallestPowerContainingN]
SmallestPowerContainingN[n_Integer] := Module[{i = 1, test},
While[True,
Line 811:
]
]
Grid[SmallestPowerContainingN /@ Range[0, 21]]</langsyntaxhighlight>
{{out}}
<pre>0 9 10077696
Line 838:
=={{header|Nim}}==
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
import bignum
 
Line 856:
echo "Smallest values of k such that 6^k contains n:"
for n, (k, s) in results:
echo &"{n:2}: 6^{k:<2} = {s}"</langsyntaxhighlight>
 
{{out}}
Line 886:
{{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.
<langsyntaxhighlight lang="pascal">program PotOf6;
//First occurence of a numberstring with max DIGTIS digits in 6^n
{$IFDEF FPC}
Line 1,085:
writeln(i:10,' ',number,'^',foundIndex-1:5,' ',foundStr);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,145:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util 'first';
Line 1,155:
my $e = first { 6**$_ =~ /$n/ } 0..1000;
printf "%7d: 6^%-3s %s\n", $n, $e, comma 6**$e;
}</langsyntaxhighlight>
{{out}}
<pre> 0: 6^9 10,077,696
Line 1,185:
(Related recent task: [[Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared#Phix]])
 
<!--<langsyntaxhighlight Phixlang="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: #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:
<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>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 1,261:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def smallest_six(n):
p = 1
while str(n) not in str(p): p *= 6
Line 1,267:
for n in range(22):
print("{:2}: {}".format(n, smallest_six(n)))</langsyntaxhighlight>
{{out}}
<pre> 0: 10077696
Line 1,294:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @po6 = ^Inf .map: *.exp: 6;
Line 1,301:
sprintf "%3d: 6%-4s %s", $n, .&super, comma @po6[$_]
given @po6.first: *.contains($n), :k
};</langsyntaxhighlight>
{{out}}
<pre> 0: 6⁹ 10,077,696
Line 1,328:
 
=={{header|REXX}}==
<langsyntaxhighlight 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 */
parse arg hi . /*obtain optional argument from the CL.*/
Line 1,349:
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 ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,380:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,403:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,436:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/fmt" for Fmt
 
Line 1,451:
i = i + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,482:
=={{header|Yabasic}}==
{{trans|Python}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Smallest_power_of_6_whose_decimal_expansion_contains_n
// by Galileo, 05/2022
 
Line 1,494:
end sub
for n = 0 to 21 : print n, ": ", str$(smallest_six(n), "%1.f") : next</langsyntaxhighlight>
{{out}}
<pre>0: 10077696
10,333

edits