CoCalc Shared FilesPersonal / POW / Spring 2019 #9 / Pow #9.ipynbOpen in CoCalc with one click!
Author: Peter Francis
Views : 18
In [9]:
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.

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

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

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

In [1]:
import numpy as np import itertools as it import matplotlib.pyplot as plt
In [2]:
def digits(*numbers): output = np.array([], dtype=np.int) for i in numbers: if str(i)[0] == '-': j = str(i)[1:] else: j=i output = np.union1d(np.array([int(d) for d in str(j)]),output) return output

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

In [3]:
digits(-345)
array([3, 4, 5])
In [4]:
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, NN. After all pairs have been checked, it displays NN.

In [5]:
N = np.array([]) for i in it.combinations(list(np.arange(100,1000))+list(np.arange(-999,-101)),2): a = i[0] b = i[1] 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[0],i[1]))])) 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 nn 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 NN by slightly altering the code above. Scince the largest number the digits of aa and bb could possibly sum to is 9+8+7+6+5+4=399+8+7+6+5+4=39, we will make another array PP of length 39+1,39+1, that counts the number of times each index of PP is summed to (the indeces start at 0).

In [6]:
N = np.array([]) P = np.array([0]*(39+1)) for i in it.combinations(list(np.arange(100,1000))+list(np.arange(-999,-101)),2): a = i[0] b = i[1] 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 nn versus nn.

In [7]:
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 nn was, the best guess is n=27n=27. \square

A little More\text{\Large A little More}

We might now consider the following more abstract question:

Question: For pp, rr-digit numbers aa and bb, where the prpr digits I used to write aa and bb were all different, if a+ba + b was another rr-digit number, what are the possible values of the sum nn of the digits in aa and bb and what is the most probable sum?

We can easily rewrite the code cell above as a function of pp and rr.

In [8]:
def para(p,r): N = np.array([]) P = np.array([0]*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<rp100 < rp\leq 10. Since rr and pp are integers, we can calculate para\texttt{para} for all possible inputs! The cell below will calculate para\texttt{para} for some of the other possible inputs and display NN (the array of all possible digit sums), and the frequency bar plot that was discussed earlier. It will also display the 'best guess' for nn, which is the index of PP where maximum of PP occurs. (If the maximum of PP is in more than one index, the code below will display only the first index).