 CoCalc Shared FilesPersonal / POW / Spring 2019 #9 / Pow #9.ipynb
Author: Peter Francis
Views : 18
In :


p=1. r=1. N=[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]. The maximum of P is n=1. p=2. r=1. N=[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.]. The maximum of P is n=9. p=3. r=1. N=[ 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.]. The maximum of P is n=13. p=4. r=1. N=[ 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.]. The maximum of P is n=18. p=5. r=1. N=[10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.]. The maximum of P is n=23. p=6. r=1. N=[15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]. The maximum of P is n=27. p=7. r=1. N=[21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.]. The maximum of P is n=33. p=8. r=1. N=[28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.]. The maximum of P is n=36. p=9. r=1. N=[36. 37. 38. 39. 40. 41. 42. 43. 44. 45.]. The maximum of P is n=45. p=10. r=1. N=[45.]. The maximum of P is n=45. p=1. r=2. N=[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.]. The maximum of P is n=9. p=2. r=2. N=[ 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.]. The maximum of P is n=18. p=3. r=2. N=[15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]. The maximum of P is n=27. p=4. r=2. N=[28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.]. The maximum of P is n=36. p=5. r=2. N=[45.]. The maximum of P is n=45. p=1. r=3. N=[ 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.]. The maximum of P is n=14. p=2. r=3. N=[15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]. The maximum of P is n=27. p=3. r=3. N=[36. 37. 38. 39. 40. 41. 42. 43. 44. 45.]. The maximum of P is n=45. p=1. r=4. N=[ 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.]. The maximum of P is n=18. p=2. r=4. N=[28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.]. The maximum of P is n=36. p=1. r=5. N=[10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.]. The maximum of P is n=23. p=2. r=5.

$\text{\Huge Digital Computing}$ $Peter\ Francis$

Question: Last weekend, I added two three-digit numbers $a$ and $b$, where the six digits I used to write $a$ and $b$ were all different. $a + b$ was another three-digit number. I added the digits in $a$ and $b$ to obtain another number $n$. What can you say about $n$?

Below, I will give a solution to the original question as well as expanding the problem to other possible variations.

In :
import numpy as np
import itertools as it
import matplotlib.pyplot as plt

In :
def digits(*numbers):
output = np.array([], dtype=np.int)
for i in numbers:
if str(i) == '-':
j = str(i)[1:]
else:
j=i
output = np.union1d(np.array([int(d) for d in str(j)]),output)
return output


The function $\texttt{digits}$ returns the digits of the numbers inputed as elements of an array. For example,

In :
digits(-345)

array([3, 4, 5])
In :
digits(1234,476)

array([1, 2, 3, 4, 6, 7])

The following cell loops through every possible pair of two three-digit integers. If the digits in the two numbers are all different and the two numbers sum to a three digit number, the sum of their digits is added to an array, $N$. After all pairs have been checked, it displays $N$.

In :
N = np.array([])
for i in it.combinations(list(np.arange(100,1000))+list(np.arange(-999,-101)),2):
a = i
b = i
if len(digits(a,b))==6 and np.abs(a+b)<1000 and np.abs(a+b)>99:
N = np.union1d(N,([sum(digits(i,i))]))
print(N)

[15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]

So, we know that $n$ must have been one of the integers listed above. We can go a step further and find the number of possible pairs that sum to a given entry in $N$ by slightly altering the code above. Scince the largest number the digits of $a$ and $b$ could possibly sum to is $9+8+7+6+5+4=39$, we will make another array $P$ of length $39+1,$ that counts the number of times each index of $P$ is summed to (the indeces start at 0).

In :
N = np.array([])
P = np.array(*(39+1))
for i in it.combinations(list(np.arange(100,1000))+list(np.arange(-999,-101)),2):
a = i
b = i
if len(digits(a,b))==6 and np.abs(a+b)<1000 and np.abs(a+b)>99:
sum_of_digits = sum(digits(a,b))
N = np.union1d(N,([sum_of_digits]))
P[sum_of_digits] = P[sum_of_digits] + 1
print("N=",N)
print("P=",P)

N= [15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.] P= [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 864 840 1632 2352 3864 4528 6992 7464 9632 10464 11736 11808 13416 11664 11616 10176 9352 7296 6472 4328 3360 2232 1392 672 600]

We can graph the frequency of summing to $n$ versus $n$.

In :
plt.bar(range(len(P)), P, 1/1.5, color="blue");
plt.ylabel('Frequency');
plt.xlabel('n'); From this, we can see that if you were to want to guess what $n$ was, the best guess is $n=27$. $\square$

$\text{\Large A little More}$

We might now consider the following more abstract question:

Question: For $p$, $r$-digit numbers $a$ and $b$, where the $pr$ digits I used to write $a$ and $b$ were all different, if $a + b$ was another $r$-digit number, what are the possible values of the sum $n$ of the digits in $a$ and $b$ and what is the most probable sum?

We can easily rewrite the code cell above as a function of $p$ and $r$.

In :
def para(p,r):
N = np.array([])
P = np.array(*46)
if r != 1:
lower_bound = 10**(r-1)
up = 1
if r == 1:
lower_bound = 0
up = 0
for i in it.combinations(list(np.arange(lower_bound,10**r))+list(np.arange(-1*10**r+1,-1*lower_bound+up)),p):
array_of_digits = digits(*tuple([i[x] for x in range(p)]))
if len(array_of_digits)==r*p and np.abs(sum(i))<10**(r) and np.abs(sum(i))>lower_bound-1:
sum_of_digits = sum(array_of_digits)
N = np.union1d(N,([sum_of_digits]))
P[sum_of_digits] = P[sum_of_digits] + 1
return (N,P)


Clearly, we must have that the number of digits, $0 < rp\leq 10$. Since $r$ and $p$ are integers, we can calculate $\texttt{para}$ for all possible inputs! The cell below will calculate $\texttt{para}$ for some of the other possible inputs and display $N$ (the array of all possible digit sums), and the frequency bar plot that was discussed earlier. It will also display the 'best guess' for $n$, which is the index of $P$ where maximum of $P$ occurs. (If the maximum of $P$ is in more than one index, the code below will display only the first index).