14
\$\begingroup\$

We'll define the N-exponential potential of a positive integer M as the count of prefixes of MN that are perfect N-powers.

The prefixes of an integer are all the contiguous subsequences of digits that start with the first one, interpreted as numbers in base 10. For example, the prefixes of 2744 are 2, 27, 274 and 2744.

A prefix P is a perfect N-power if there exists an integer K such that KN = P. For example, 81 is a perfect 4-power because 34 = 81.


Given two strictly positive integers M and N, compute the N-exponential potential of M according to the definition above.

For instance, the 2-exponential potential of 13 is 3 because 132 is 169, and 1, 16 and 169 are all perfect squares.

Test cases

Naturally, the outputs will nearly always be pretty small because powers are... well... exponentially growing functions and having multiple perfect-power prefixes is rather rare.

M, N     -> Output

8499, 2  -> 1
4,    10 -> 2
5,    9  -> 2
6,    9  -> 2
13,   2  -> 3
\$\endgroup\$
2
  • \$\begingroup\$ hello, how the output of : ( 4, 10 ) is 2 and not 1? because 4 power 10 is 1048576, 1 is a perfect power but not 10 \$\endgroup\$
    – Ali ISSA
    Commented Mar 18, 2018 at 20:51
  • \$\begingroup\$ @AliISSA Hi, The output for 4, 10 is 2, because 1 is a perfect 10-power and 1048576 is also a perfect 10-power (while 10, 104, 1048, 10485 and 104857 are not). Thus, there are 2 valid prefixes, so the output is 2. \$\endgroup\$
    – Mr. Xcoder
    Commented Mar 18, 2018 at 20:58

15 Answers 15

10
\$\begingroup\$

Brachylog, 12 bytes

{^a₀.&b~b^}ᶜ

Try it online!

Explanation

{^a₀.&b~b^}ᶜ
{         }ᶜ  Count the number of ways the following can succeed:
  a₀            A prefix of
 ^                the first {input} to the power of the second {input}
    .&          produces the same output with the same input as
       ~b         any number
         ^        to the power of
      b           all inputs but the first (i.e. the second input)
\$\endgroup\$
6
\$\begingroup\$

Jelly, 10 bytes

*DḌƤÆE%Ḅċ0

Try it online!

How it works

*DḌƤÆE%Ḅċ0  Main link. Left argument: m. Right argument: n.

*           Compute m**n.
 D          Generate its decimal digits.
  ḌƤ        Convert prefixes back to integers.
    ÆE      Get the exponents of each prefix's prime factorization.
      %     Take all exponents modulo n.
            For a perfect n-th power, all moduli will be 0.
       Ḅ    Convert from binary to integer, mapping (only) arrays of 0's to 0.
        ċ0  Count the zeroes.
\$\endgroup\$
3
\$\begingroup\$

Haskell, 56 bytes

0%n=0
x%n=sum[1|t<-[1..x],t^n==x]+div x 10%n
m#n=(m^n)%n

Try it online!

Extracts the prefixes arithmetically by repeated \x->div x 10. I tried expressing the last line point-free but didn't find a shorter expression.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 8 bytes

mηÓ¹%O0¢

Try it online!

Uses Dennis's Jelly 10-byte algorithm. Inputs are in reversed order.

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 38 bytes

#!/usr/bin/perl -p
/ /;$_=grep/^${\++$n**$'}/,($`**$')x$`

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 73 bytes

m#n=sum[1|c<-scanl(\s c->s++[c])"0"$show$m^n,any(==read c)$map(^n)[1..m]]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 9), 105 bytes

m->n->{int c=1,k=m;for(;--k>0;)if((""+(int)Math.pow(m,n)).matches((int)Math.pow(k,n)+".*"))c++;return c;}

Try it online!

Credits

\$\endgroup\$
1
  • 1
    \$\begingroup\$ .startsWith(""+(int)Math.pow(k,n)) can be .matches((int)Math.pow(k,n)+".*") for -1 byte. \$\endgroup\$ Commented Mar 19, 2018 at 10:02
1
\$\begingroup\$

Perl 6, 40 bytes

{1+(^$^m X**$^n).grep({$m**$n~~/^$^p/})}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ If you assign a Callable to a &foo variable you can call it as you would a subroutine foo( 'bar' ) or foo 'bar' no need to include &. I mean you didn't write it as &say(&f(|$_)) (say is not special in any way) \$\endgroup\$ Commented Mar 20, 2018 at 16:17
  • \$\begingroup\$ @BradGilbertb2gills I only use Perl 6 for code golf and still have a lot to learn, so thanks for the tip. \$\endgroup\$
    – nwellnhof
    Commented Mar 20, 2018 at 16:40
0
\$\begingroup\$

Jelly, 14 bytes

*DḌƤ*İ}ær⁵%1¬S

Try it online! or see the test suite

How it works

*DḌƤ*İ}ær⁵%1¬S - Main link. Arguments: n, m (integers)  e.g. 13, 2
*              - Power. Raise x to the power y               169
 D             - Convert to a list of digits                 [1 6 9]
   Ƥ           - Convert each Ƥrefix
  Ḍ            - Back to an integer                          [1 16 169]
     İ         - Calculate the İnverse of
      }        - The right argument                          0.5
    *          - Raise each element to that power            [1 4 13]
       ær⁵     - Round each to 10 ** -10                     [1 4 13]
               - to remove precision errors
          %1   - Take the decimal part of each               [0 0 0]
            ¬  - Logical NOT each                            [1 1 1]
             S - Sum                                         3
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 31 bytes

{+/o=⌊o←(÷⍵)*⍨10⊥¨,\10⊥⍣¯1⊢⍺*⍵}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 83 bytes

import Data.List
m#n=sum[1|x<-read<$>(tail$inits$show(m^n)),x`elem`((^n)<$>[1..m])]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 60 bytes

->m,n{c=1;s=m**n;c+=(0..m).count{|j|j**n==s}while 0<s/=10;c}

a lot of it is to deal with floating point errors

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 89 bytes

m,n->1+(1..m-1).count{"${Math.pow(m+0.0,n)}".startsWith("${Math.pow(it+0.0,n).toInt()}")}

Try it online!

In the test cases, passed in n as double values (2.0, 10.0, 9.0) so that I don't have to convert to double when calling Math.pow().

\$\endgroup\$
0
\$\begingroup\$

Python 2, 83 71 70 bytes

n,m=input();s=n**m;k=0
while s:k+=round(s**(1./m))**m==s;s/=10
print k

Try it online!

Thx for 1 from ovs.

\$\endgroup\$
1
  • \$\begingroup\$ @ovs: d'oh! been coding too much javascript lately... was thinking I'd need math.round() lol \$\endgroup\$
    – Chas Brown
    Commented Mar 20, 2018 at 5:02
0
\$\begingroup\$

Jelly, 9 bytes

*DḌƤœ&*€L

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.