A 37-digit (base ten) positive integer is constituted by writing the first 23 positive integers side by side, as follows:
1234567891011121314151617181920212223
Determine the respective remainders when this number is divided in turn by each of the base ten integers from 2 to 23 inclusively.
Two methods
First Method:
It's easy enough to simply calculate this using the built in mod function operator.
(But what if the number is too large for this to work? see Second Method)
n = 1234567891011121314151617181920212223
for k in range(2,24):
print(k, n%k)
2 1
3 0
4 3
5 3
6 3
7 2
8 7
9 6
10 3
11 5
12 3
13 5
14 9
15 3
16 15
17 13
18 15
19 10
20 3
21 9
22 5
23 6
---
Second Method:
generate a list of the values of the (powers of 10) mod div, where div is the divisor in question. Make a sum of each digit times the the mod value of the appropriate power of 10. For the 37 digit number in question, the largest value the variable "temp" achieved was 1259 for divisor 22. So for these divisors one iteration brought the number of digits down from 37 to 4.
---
def remainder(num,div):
""" The remainder of num divided by div """
num = [int(digit) for digit in str(num)]
length = len(num)
multipliers = []
for i in range(length):
if i == 0:
multipliers.append(1%div)
else:
multipliers.append((10*multipliers[i-1]%div))
temp = 0
for i in range(length):
temp += multipliers[i] * num[::-1][i]
return temp%div
n = 1234567891011121314151617181920212223
for k in range(2,24):
print(k, n%k, remainder(n,k))
2 1 1
3 0 0
4 3 3
5 3 3
6 3 3
7 2 2
8 7 7
9 6 6
10 3 3
11 5 5
12 3 3
13 5 5
14 9 9
15 3 3
16 15 15
17 13 13
18 15 15
19 10 10
20 3 3
21 9 9
22 5 5
23 6 6
|
Posted by Larry
on 2023-01-07 08:34:12 |