2
$\begingroup$

Well, the title of the question says it all: how to write a code that finds the probability of the digit $k$ in the number $x^n$?

For example, when $x=2$, $n=100$, and $k=7$ we are trying to find how many $7$s there are in the number $2^{100}$. To find the answer I wrote $2^{100}=1267650600228229401496703205376$ and counted the number of $7$s, and did:

$$\frac{\text{number of}\space7\text{s}\space\text{in the number}\space2^{100}}{\text{number of digits}\space 2^{100}}=$$ $$\frac{3}{1+\lfloor\log_{10}\left(2^{100}\right)\rfloor}=\frac{3}{31}\approx0.0967742$$

My thoughts for in the code:

  • The number of digits in a number $p$ can be found using 1+Floor[Log10[p]]
  • The r'th digit in the number $p$ can be found by using IntegerDigits[p][[r]]
  • In order to check a table of numbers for there probability we can use ParallelTable[If[TrueQ[], n, Nothing], {n, ,}]

But how to combine the ideas from above, I do not know.

$\endgroup$
1
  • $\begingroup$ Thinking along the lines of minimizing memory usage (i.e. avoiding overflow by not actually calculating the power), PowerMod and the fact that the number of digits of $x^n$ is given by Floor[n Log10@x] + 1 may be useful here. $\endgroup$ – NonDairyNeutrino Oct 27 '20 at 1:02
2
$\begingroup$
countK[x_, n_, k_] := (
  digits = IntegerDigits[x^n];
  Count[digits, k]/Length[digits]
  )
countK[2, 100, 7]
$\endgroup$
1
  • $\begingroup$ To streamline it a little: countK[x_, n_, k_] := DigitCount[#, 10, k]/IntegerLength@# &[x^n] $\endgroup$ – NonDairyNeutrino Oct 27 '20 at 0:17
1
$\begingroup$
digitFrequency =  NProbability[x == #3, 
   Distributed[x, EmpiricalDistribution @ IntegerDigits[#^#2]]] &;

digitFrequency[2, 100, 7]
 0.0967742
digitFrequency2 =  N @(AssociationThread @@ Transpose@Tally @ # / Length @ # & @
   IntegerDigits[#^#2]) @ #3 /. _Missing -> 0 &;

digitFrequency2[2, 100, 7]
 0.0967742
digitFrequency3 = N @ (Divide @@ Through[{Counts, Length}@
     IntegerDigits [#^#2]]) @ #3 /. _Missing -> 0 &;

digitFrequency3[2, 100, 7]
 0.0967742
$\endgroup$
1
$\begingroup$
probs=DigitCount[#]/IntegerLength[#] &;

Use:

probs[2^100]

{2/31,6/31,2/31,2/31,2/31,5/31,3/31,1/31,2/31,6/31}

The probabilities of digits 1, 2, ..., 9, 0

$\endgroup$

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.