Smallest power of 6 whose decimal expansion contains n: Difference between revisions
Smallest power of 6 whose decimal expansion contains n (view source)
Revision as of 15:19, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10:
{{trans|Python}}
<
V p = BigInt(1)
L String(n) !C String(p)
Line 17:
L(n) 22
print(‘#2: #.’.format(n, smallest_six(n)))</
{{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.
<
# 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</
{{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.
<
% 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.</
{{out}}
<pre>
Line 212:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SMALLEST_POWER_OF_6_WHOSE_DECIMAL_EXPANSION_CONTAINS_N.AWK
BEGIN {
Line 227:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 256:
=={{header|C}}==
<
#include <string.h>
#include <gmp.h>
Line 289:
return 0;
}</
{{out}}
Line 317:
=={{header|C++}}==
<
#include <iomanip>
#include <string>
Line 339:
}
return 0;
}</
{{out}}
Line 366:
=={{header|CLU}}==
<
% It is in "misc.lib"
%
Line 400:
stream$putl(po, "")
end
end start_up</
{{out}}
<pre> 0: 6^9 = 10077696
Line 426:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 538:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
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</
{{out}}
<pre>
Line 577:
=={{header|FreeBASIC}}==
{{trans|Ring}}
<
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>
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 644:
}
}
}</
{{out}}
Line 674:
=={{header|Haskell}}==
<
import Text.Printf (printf)
Line 692:
concatMap
(printf "%2d: %d\n" <*> smallest)
[0 .. 21]</
{{out}}
Line 723:
gojq provides unbounded-precision integer arithmetic and is therefore appropriate for this task.
<
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] )</
{{out}}
<pre>
Line 759:
=={{header|Julia}}==
<
digcontains(n, dig) = contains(String(Char.(digits(n))), String(Char.(dig)))
Line 775:
println(rpad(n, 5), format(findpow6containing(n), commas=true))
end
</
<pre>
0 10,077,696
Line 802:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
SmallestPowerContainingN[n_Integer] := Module[{i = 1, test},
While[True,
Line 811:
]
]
Grid[SmallestPowerContainingN /@ Range[0, 21]]</
{{out}}
<pre>0 9 10077696
Line 838:
=={{header|Nim}}==
{{libheader|bignum}}
<
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}"</
{{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.
<
//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.</
{{out}}
<pre>
Line 1,145:
=={{header|Perl}}==
<
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;
}</
{{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]])
<!--<
<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>
<!--</
{{out}}
Line 1,261:
=={{header|Python}}==
<
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)))</
{{out}}
<pre> 0: 10077696
Line 1,294:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my @po6 = ^Inf .map: *.exp: 6;
Line 1,301:
sprintf "%3d: 6%-4s %s", $n, .&super, comma @po6[$_]
given @po6.first: *.contains($n), :k
};</
{{out}}
<pre> 0: 6⁹ 10,077,696
Line 1,328:
=={{header|REXX}}==
<
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 ?</
{{out|output|text= when using the default input:}}
<pre>
Line 1,380:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,403:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,436:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,451:
i = i + 1
}
}</
{{out}}
Line 1,482:
=={{header|Yabasic}}==
{{trans|Python}}
<
// by Galileo, 05/2022
Line 1,494:
end sub
for n = 0 to 21 : print n, ": ", str$(smallest_six(n), "%1.f") : next</
{{out}}
<pre>0: 10077696
|