I came up with the following exercise with the intention of learning Mathematica but I am already stuck in the beginning. Let $ n,b $ denote positive integers with $ n>0 $ and $ b\geq2 $. My aim is to check whether the base-$ b $ representation of $ n $ contains the same digits in base $ b $.

For instance, $ n=63_{10} $ has the representation $ n=111111_2 $ in base $ b=2 $ and $ n=124_{10} $ is $ n=444_5 $ in base $ b=5 $.

I know that Mathematica can easily convert between two different bases using BaseForm[expr, base], but I have no idea how to efficiently verify whether the digits of the resulting number are all the same. I was looking for functions that work with repunits, but I found none. Any help is appreciated!

  • 2
    $\begingroup$ Have a look at IntegerDigits (and, if you should ever need it, its inverse FromDigits). For example, IntegerDigits[63, 2] returns {1,1,1,1,1,1} and IntegerDigits[124, 5] returns {4,4,4}. $\endgroup$ – Henrik Schumacher Dec 17 '18 at 9:05

As @HenrikSchumacher mentioned, IntegerDigits works:

allSameQ[n_, b_] := Equal @@ IntegerDigits[n, b]

allSameQ @@@ {{63, 2}, {124, 5}}
{True, True}
  • $\begingroup$ That's perfect, thank you! $\endgroup$ – Klangen Dec 17 '18 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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