15
\$\begingroup\$

At time of writing, my reputation is \$16,256\$. As I noted in chat,

Oh cool my rep is the concatenation of two powers of 2: 16,256

Or even the concatenation of a power of 2 and its square, which is much more interesting

which then spawned a CMC about checking if a number has this property.


Given an integer \$n > 0\$, considered a decimal integer, and a power \$r > 1\$, return two distinct values which determine whether \$n\$ can be expressed as the concatenation of a power of \$r\$ and its square or not. For example, \$n = 16256\$ and \$r = 2\$ returns true (the concatenation of \$2^4\$ and \$(2^4)^2\$), while \$n = 39\$ and \$r = 2\$ does not. Note however that \$n = 39\$, \$r = 3\$ is true. The power of \$r\$ may be \$0\$, meaning that \$n = 11\$ is true for all \$r\$

The power of \$r\$ will always come "before" its square, so \$n = 62525, r = 5\$ is false.

You will never get an input \$n\$ where its validity depends on ignoring leading \$0\$s or not (for example \$101\$ is true for all \$r\$ if ignoring leading \$0\$s and false otherwise). However, you may still get inputs with the digit \$0\$ in (e.g. \$n = 1024, r = 2\$) where leading \$0\$s have no bearing on the validity of \$n\$ being such a concatenation.

Input and output may be in any accepted method and this is so the shortest code in bytes wins.

Test cases

    n    r    1
   39    3    1
  525    5    1
  864    8    1
16256    2    1
   11    r    1
  416    7    0
   39    2    0
   15    5    0
 1024    4    0
62525    5    0

Feel free to suggest more test cases.

\$\endgroup\$
17
  • \$\begingroup\$ Related, Related \$\endgroup\$ Sep 22, 2020 at 19:39
  • \$\begingroup\$ As is usual with my challenges, brownie points for beating my Jelly answer at 13 bytes \$\endgroup\$ Sep 22, 2020 at 19:41
  • 2
    \$\begingroup\$ Missing r value in 11 r 1? I guess it should be 1. \$\endgroup\$
    – Adám
    Sep 22, 2020 at 19:59
  • 2
    \$\begingroup\$ @Adám That's supposed to demonstrate that its 1 for all r \$\endgroup\$ Sep 22, 2020 at 19:59
  • 1
    \$\begingroup\$ @pxeger So long as there's no overlap between the possible outputs, you may choose any two sets of values to represent whether \$n\$ can be expressed in this way. That includes empty vs non-empty lists, truthy/falsey values etc. \$\endgroup\$ Sep 23, 2020 at 15:12

16 Answers 16

9
\$\begingroup\$

APL (Dyalog Unicode), 21 bytes (SBCS)

Anonymous infix lambda, taking \$r\$ as left argument and \$n\$ as right argument. Requires ⎕IO←0 (zero-based indexing).

{⍵∊(⊢⍎⍤,⍥⍕¨×⍨)⍺*⍳⌊⍟⍵}

Try it online! (Dyalog Extended as polyfill for version 18.0)

{} "dfn", is \$r\$ and is \$n\$:

⍟⍵ natural log of \$n\$ (to avoid overflow)

 round that down

ɩntegers zero through one less than that

⍺* raise \$r\$ to those powers

() apply the following monadic function to that:

  ×⍨ multiply those with themselves (i.e. square them)

  ¨ for each unmodified argument and its corresponding square:

   ⍥⍕… stringify the argument and its square before

    ⍤, concatenating them, and then

      evaluating the result

⍵∊ is the original argument a member of that?

   

\$\endgroup\$
8
\$\begingroup\$

05AB1E, 9 bytes

ÝmεDn«}¹å

Try it online! This is a little inefficient, so don't try the larger falsey test cases.

Commented:

           # implicit input, n first, r second
Ý          # inclusive range from 0 to n
 m         # raise r to all of these powers
  ε   }    # map over the powers ...
   D       #   duplicate power
    n      #   square it
     «     #   and concatenate
       ¹   # push the first input (n)
        å  # is this in the list?
\$\endgroup\$
8
\$\begingroup\$

Python 3, 63 \$\cdots\$ 56 54 bytes

Saved 4 bytes thanks to ovs!!!
Saved a byte porting Arnauld's golf of Shaggy's JavaScript answer!!!
Saved 2 bytes thanks to pxeger!!!

f=lambda n,r,p=1:p>n or(n-int(f'{p}{p*p}'))*f(n,r,r*p)

Try it online!

Returns a falsey if \$n\$ can be expressed as the concatenation of a power of \$r\$ and its square or truthy otherwise.

\$\endgroup\$
2
  • \$\begingroup\$ -2 bytes \$\endgroup\$
    – pxeger
    Sep 23, 2020 at 15:24
  • \$\begingroup\$ @pxeger Nice one with the multiplication beats logical and again - thanks! :D \$\endgroup\$
    – Noodle9
    Sep 23, 2020 at 16:08
6
\$\begingroup\$

Brachylog, 9 bytes

Takes r as input and n as output. Unifies if truthy, otherwise fails.

;A^gj^₂ᵗc

Try it online!

How it works

;A^gj^₂ᵗc with implicit r as input
;A^       r^some number
   gj     [r^some number, r^some number]
     ^₂ᵗ  [r^some number, r^some number^2]
        c concatenated is the output n
\$\endgroup\$
6
\$\begingroup\$

R + pryr, 43 39 bytes

Edit: -4 bytes thanks to pajonk

Or R, 43 bytes

pryr::f(any(n==paste0(s<-r^(0:n),s^2)))

Try it online!

A nice function that is naturally short thanks to R's vectorization.

s<-r^(0:n) generates a vector of all powers-of-r from 0..n (the <- here is an R assignment operator, similar to =),

paste0(s,s^2) generates a character vector of all these powers pasted onto their squares (the 0 in paste0 instructs the function not to use a space in the concatenation),

any(n==...) finally checks to see whether n is equal to any of the elements of the vector, conveniently coercing n into character form to do this.

pryr::f(...) is a shorter way to express function(n,r) (from the pryr library), that 'guesses' the arguments using the body of the function definition (presumably by the order-of-appearance of unassigned variables: I can't actually find any explanation in the manual page, but anyway it seems to work...!).

\$\endgroup\$
3
  • 1
    \$\begingroup\$ With pryr package installed you can get 4 bytes less: Try it online! \$\endgroup\$
    – pajonk
    Sep 23, 2020 at 11:27
  • 2
    \$\begingroup\$ Thanks! I was kind-of aware that pryr::f was a 1-byte shorter than function, but I never realized how cleverly its 'argument guessing' could save the bytes of the arguments, too! Yet again, I'm accidentally learning something useful by golfing... \$\endgroup\$ Sep 23, 2020 at 11:39
  • \$\begingroup\$ @pajonk that does change the language of the answer from "R" to "R + pryr" according to the way we distinguish languages \$\endgroup\$
    – Giuseppe
    Sep 23, 2020 at 14:49
4
\$\begingroup\$

JavaScript, 47 44 38 bytes

n=>g=(r,x=1)=>x<n&&[x]+x*x==n|g(r,x*r)

-6 bytes thanks to Arnauld.

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 38 bytes \$\endgroup\$
    – Arnauld
    Sep 22, 2020 at 20:55
  • 1
    \$\begingroup\$ Nice one, thanks, @Arnauld. Been a good while since I golfed drunk in JS! \$\endgroup\$
    – Shaggy
    Sep 22, 2020 at 21:04
3
\$\begingroup\$

Japt -x, 11 bytes

ÆVpXã¥X+²s

Try it

ÆVpXã¥X+²s     :Implicit input of integers U=n and V=r
Æ               :Map each X in the range [0,U)
 VpX            :  Raise V to the power of X
    Ã           :End map
     £          :Map each X
      ¥         :  Test U for equality with
       X+       :  X appended with
         ²      :  X squared
          s     :  Converted to a string
                :Implicit output of sum of resulting array
\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 52 bytes

#^2+10^IntegerLength[#^2]#&[#2^0~Range~#]~MemberQ~#&

Try it online!

\$\endgroup\$
3
\$\begingroup\$

PHP, 63 bytes

function($n,$r){while($n>$b=($a=$r**$x++).$a*$a);return$n==$b;}

Try it online!

Or... put another way...

PHP, 63 bytes

function($n,$r){while(0<$b=$n<=>($a=$r**$x++).$a*$a);return$b;}

Try it online!

Can't seem to get away from this number...

PHP, 63 bytes

function($n,$r){while($n>$a=$r**$x.$r**($x++*2));return$n==$a;}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Javascript (V8), 63 60 59 53 51 43 bytes

-3 from Neil

-2 and -8 from Shaggy

n=>r=>[...n+n].some((_,i)=>[p=r**i]+p*p==n)

Takes input via currying: f("16256")(2). Works quickly and for all values within the safe integer limit (\$2^{52}-1\$). Returns true or false.

Old

n=>r=>[...n+n].map((a,i)=>[s=r**i]+s*s).indexOf(n)
n=>r=>[...Array(+n)].map((a,i)=>""+(p=r**i)+p*p).indexOf(n)
\$\endgroup\$
10
  • \$\begingroup\$ This outputs a consistent truthy value for false and either a truthy or falsey value for true. You may want to check if that's acceptable. \$\endgroup\$
    – Shaggy
    Sep 22, 2020 at 20:49
  • \$\begingroup\$ @Shaggy It always gives -1 for false, and any nonzero number otherwise. I thought that's acceptable but I'll go check. (Either way, it's just two more bytes for a consistent truthy/falsy) \$\endgroup\$ Sep 22, 2020 at 20:54
  • 1
    \$\begingroup\$ @RedwolfPrograms, it returns 0 in the case of n=11, r=1. \$\endgroup\$
    – Shaggy
    Sep 22, 2020 at 21:07
  • 1
    \$\begingroup\$ Ah, looks like I was hung up on phrasing; that works for me now. Think you might be able to save a few bytes with n=>r=>[...n+n].map((a,i)=>[s=r**i]+s*s).indexOf(n). \$\endgroup\$
    – Shaggy
    Sep 22, 2020 at 23:08
  • 1
    \$\begingroup\$ You can get down to 43 by using some instead of map & indexOf and checking for equality with n in the callback. \$\endgroup\$
    – Shaggy
    Sep 23, 2020 at 8:44
3
\$\begingroup\$

Rust, 72 70 bytes

|n,r|(0..n).any(|i|format!("{}{}",r.pow(i),r.pow(2*i))==n.to_string())

Try it online!

A port of ovs's 05AB1E answer. Thanks to ovs for helping save 2 bytes!

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

SNOBOL4 (CSNOBOL4), 96 bytes

	N =INPUT
	R =INPUT
N	Z =R ^ X
	Y =EQ(N,Z Z ^ 2) 1	:S(O)
	X =LE(Z,N) X + 1	:S(N)
O	OUTPUT =Y
END

Try it online!

Prints 1 for Truthy, and an empty line for Falsey.

	N =INPUT			;* Input n
	R =INPUT			;* input R
N	Z =R ^ X			;* set Z = R^X (X starts as "" or 0)
	Y =EQ(N,Z Z ^ 2) 1	:S(O)	;* If N = Z concatenated to Z^2, set Y = 1 and goto O
	X =LE(Z,N) X + 1	:S(N)	;* If Z <= N, increment X and goto N, else:
O	OUTPUT =Y			;* print Y, which is '' unless N == Z Z^2
END
\$\endgroup\$
2
\$\begingroup\$

MathGolf, 11 bytes

r#mÆ‼░²░+l╧

Try it online. (The two test cases with the largest \$n\$ are timing out.)

Explanation

r            # Push a list in the range [0, (implicit) input `n`)
 #           # Take (implicit) input `r` to the power of each value in this list
  m          # Map over this list,
   Æ         # Using the following five commands:
    ‼        #  Apply the following two commands on the stack separately:
     ░       #   Convert the value to a string
      ²      #   Square the value
       ░     #  Convert the squared value to a string a well
        +    #  Concatenate the two strings together
         l   # After the map: push the first input `r` as string
          ╧  # And check if this string is in the list
             # (after which the entire stack joined together is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

Jelly, 9 bytes

Uses the evaluation (V) trick from Unrelated String's answer - go give an upvote!

*⁹ŻżḤ$¤Vċ

A dyadic Link accepting an integer \$r>1\$ on the left and an integer \$n>0\$ on the right which yields 1 if \$n\$ can be expressed as the concatenation of a power of \$r\$ and its square, or 0 if not.

Try it online! Or see the test-suite (large \$n\$ excluded due to speed).

How?

*⁹ŻżḤ$¤Vċ - Link: r; n
      ¤   - nilad followed by link(s) as a nilad:
 ⁹        -   chain's right argument, n
  Ż       -   zero-range -> [0,1,2,...,n]
     $    -   last two links as a monad:
    Ḥ     -     double -> [0,2,4,...,2n]
   ż      -     zip -> [[0,0],[1,2],[2,4],...,[n,2n]]
*         - (r) exponentiate (that) (vectorises)
       V  - evaluate (e.g. [9,81] -> 981) (vectorises)
        ċ - count occurrences (of n)
\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 9 bytes

Ḷ*@ż²$Vi⁸

Try it online!

-1 thanks to Jonathan Allan

Elided the two larger test cases for the sake of being able to run.

Adapted from my own answer to the CMC. I've also attempted to adapt one of HyperNeutrino's cleverer answers, but it comes out to the same length on account of needing to handle the [11, r]:

Jelly, 10 9 bytes

ḶżḤ$*@Vi⁸

Try it online!

I save on an @ and an by reversing the arguments, but then it takes 2 bytes to handle an exponent of 0, taking it right back up to 10 9:

Jelly, 10 9 bytes

*Ɱ;1ż²$Vi

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice, I didn't think of V, using that I now finally have a 9 (wasn't going to post until I had a 10 since I was sure 10 must be possible). \$\endgroup\$ Sep 23, 2020 at 13:03
  • 1
    \$\begingroup\$ Ah I didn't notice at first, but your second one can be golfed to 9 too ;Ḥ$€ -> żḤ$ :) \$\endgroup\$ Sep 23, 2020 at 13:12
  • \$\begingroup\$ Funnily enough, that's something HyperNeutrino already used in a different one of his CMC answers--took me this long to figure out why! Thanks \$\endgroup\$ Sep 23, 2020 at 13:28
2
\$\begingroup\$

Rockstar, 129 bytes

listen to N
listen to R
X's0
O's0
while N-X
let X be+1
P's1
Y's0
while X-Y
let P be*R-0
let Y be+1

let O be+P+""+P*P is N

say O

Try it here (Code will need to be pasted in, with n on the first line of input and r on the second)

\$\endgroup\$

Your Answer

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

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