mathgenerator.computer_science

  1import random
  2import math
  3
  4def floating_point_binary_to_decimal(mantissa_length=8, exponent_length=4):
  5    r"""Floating Point Binary to Decimal
  6
  7    | Ex. Problem | Ex. Solution |
  8    | --- | --- |
  9    | There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long | $011100000010 = 3.5$ |
 10
 11    """
 12    # TODO Could do with some work ensuring the number is not too small or too big.
 13    mantissa = ''
 14    for x in range(mantissa_length):
 15        mantissa += str(random.randint(0,1))
 16    exponent = ''
 17    for x in range(exponent_length):
 18        exponent += str(random.randint(0,1))
 19    decimal_mantissa = int(mantissa, 2)
 20    if (decimal_mantissa & (1 << (mantissa_length - 1))) != 0:
 21        decimal_mantissa = decimal_mantissa - (1 << mantissa_length)
 22    decimal_exponent = int(exponent, 2)
 23    if (decimal_exponent & (1 << (exponent_length - 1))) != 0:
 24        decimal_exponent = decimal_exponent - (1 << exponent_length)
 25    answer = decimal_mantissa * 2**(decimal_exponent - (mantissa_length - 1))
 26
 27    problem = f"There is a floating point binary number ${mantissa}{exponent}$ where the signed mantissa is {mantissa_length} bits long and the signed exponent is {exponent_length} bits long"
 28    solution = f'${mantissa}{exponent} = {answer}$'
 29
 30    return problem, solution
 31
 32def binary_addition(max_sum=256, max_addend=128):
 33    r"""Binary Addition
 34
 35    | Ex. Problem | Ex. Solution |
 36    | --- | --- |
 37    | In binary, calculate: $101 + 110110 = $ | $111011$ |
 38    """
 39    if max_addend > max_sum:
 40        max_addend = max_sum
 41    a = random.randint(0, max_addend)
 42    b = random.randint(0, min((max_sum - a), max_addend))
 43    c = a + b
 44
 45    problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ "
 46    solution = f'${bin(c).replace('0b', "")}$'
 47    return problem, solution
 48
 49def bcd_to_decimal(max_number=10000):
 50    r"""Binary Coded Decimal to Integer
 51
 52    | Ex. Problem | Ex. Solution |
 53    | --- | --- |
 54    | Integer of Binary Coded Decimal $4 =$ | $17801$ |
 55    """
 56    n = random.randint(1000, max_number)
 57    binstring = ''
 58    while True:
 59        q, r = divmod(n, 10)
 60        nibble = bin(r).replace('0b', "")
 61        while len(nibble) < 4:
 62            nibble = '0' + nibble
 63        binstring = nibble + binstring
 64        if q == 0:
 65            break
 66        else:
 67            n = q
 68
 69    problem = f"Integer of Binary Coded Decimal ${n} =$ "
 70    solution = f'${int(binstring, 2)}$'
 71    return problem, solution
 72
 73
 74def binary_2s_complement(maxDigits=10):
 75    r"""Binary 2's Complement
 76
 77    | Ex. Problem | Ex. Solution |
 78    | --- | --- |
 79    | 2's complement of $1011 = $ | $101$ |
 80    """
 81    digits = random.randint(1, maxDigits)
 82    question = ''.join([str(random.randint(0, 1))
 83                        for i in range(digits)]).lstrip('0')
 84
 85    answer = []
 86    for i in question:
 87        answer.append(str(int(not bool(int(i)))))
 88
 89    carry = True
 90    j = len(answer) - 1
 91    while j >= 0:
 92        if answer[j] == '0':
 93            answer[j] = '1'
 94            carry = False
 95            break
 96        answer[j] = '0'
 97        j -= 1
 98
 99    if j == 0 and carry is True:
100        answer.insert(0, '1')
101
102    problem = f"2's complement of ${question} = $"
103    solution = ''.join(answer).lstrip('0')
104    return problem, f'${solution}$'
105
106
107def binary_complement_1s(maxDigits=10):
108    r"""Binary Complement 1s
109
110    | Ex. Problem | Ex. Solution |
111    | --- | --- |
112    | $1111001 = $ | $0000110$ |
113    """
114    question = ''.join([
115        str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits))
116    ])
117    answer = ''.join(["0" if digit == "1" else "1" for digit in question])
118
119    problem = f'${question} = $'
120    return problem, f'${answer}$'
121
122
123def binary_to_decimal(max_dig=10):
124    r"""Binary to Decimal
125
126    | Ex. Problem | Ex. Solution |
127    | --- | --- |
128    | $000110$ | $6$ |
129    """
130    problem = ''.join(
131        [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))])
132    solution = f'${int(problem, 2)}$'
133    return f'${problem}$', solution
134
135
136def binary_to_hex(max_dig=10):
137    r"""Binary to Hexidecimal
138
139    | Ex. Problem | Ex. Solution |
140    | --- | --- |
141    | $010101$ | $0x15$ |
142    """
143    problem = ''.join(
144        [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))])
145    solution = f'${hex(int(problem, 2))}$'
146    return f'${problem}$', solution
147
148
149def decimal_to_bcd(max_number=10000):
150    r"""Decimal to Binary Coded Decimal
151
152    | Ex. Problem | Ex. Solution |
153    | --- | --- |
154    | BCD of Decimal Number $6575 = $ | $191015$ |
155    """
156    n = random.randint(1000, max_number)
157    x = n
158    # binstring = ''
159    bcdstring = ''
160    while x > 0:
161        nibble = x % 16
162        bcdstring = str(nibble) + bcdstring
163        x >>= 4
164
165    problem = f"BCD of Decimal Number ${n} = $"
166    return problem, f'${bcdstring}$'
167
168
169def decimal_to_binary(max_dec=99):
170    r"""Decimal to Binary
171
172    | Ex. Problem | Ex. Solution |
173    | --- | --- |
174    | Binary of $4 = $ | $100$ |
175    """
176    a = random.randint(1, max_dec)
177    b = bin(a).replace("0b", "")
178
179    problem = f'Binary of ${a} = $'
180    solution = f'${b}$'
181    return problem, solution
182
183
184def decimal_to_hexadeci(max_dec=1000):
185    r"""Decimal to Hexadecimal
186
187    | Ex. Problem | Ex. Solution |
188    | --- | --- |
189    | Hexadecimal of $410 = $ | $0x19a$ |
190    """
191    a = random.randint(0, max_dec)
192    b = hex(a)
193
194    problem = f"Hexadecimal of ${a} = $"
195    solution = f"${b}$"
196    return problem, solution
197
198
199def decimal_to_octal(max_decimal=4096):
200    r"""Decimal to Octal
201
202    | Ex. Problem | Ex. Solution |
203    | --- | --- |
204    | The decimal number $3698$ in octal is: | $0o7162$ |
205    """
206    x = random.randint(0, max_decimal)
207
208    problem = f"The decimal number ${x}$ in octal is: "
209    solution = f'${oct(x)}$'
210
211    return problem, solution
212
213
214def fibonacci_series(min_no=1):
215    r"""Fibonacci Series
216
217    | Ex. Problem | Ex. Solution |
218    | --- | --- |
219    | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ |
220    """
221    n = random.randint(min_no, 20)
222
223    def createFibList(n):
224        list = []
225        for i in range(n):
226            if i < 2:
227                list.append(i)
228            else:
229                val = list[i - 1] + list[i - 2]
230                list.append(val)
231        return list
232
233    fibList = createFibList(n)
234
235    problem = "The Fibonacci Series of the first ${n}$ numbers is ?"
236    solution = ', '.join(map(str, fibList))
237    return problem, f'${solution}$'
238
239
240def modulo_division(max_res=99, max_modulo=99):
241    r"""Modulo Division
242
243    | Ex. Problem | Ex. Solution |
244    | --- | --- |
245    | $43$ % $33 = $ | $10$ |
246    """
247    a = random.randint(0, max_modulo)
248    b = random.randint(0, min(max_res, max_modulo))
249    c = a % b if b != 0 else 0
250
251    problem = f'${a}$ % ${b} = $'
252    solution = f'${c}$'
253    return problem, solution
254
255
256def nth_fibonacci_number(max_n=100):
257    r"""nth Fibonacci number
258
259    | Ex. Problem | Ex. Solution |
260    | --- | --- |
261    | What is the 85th Fibonacci number? | $259695496911123328$ |
262    """
263    gratio = (1 + math.sqrt(5)) / 2
264    n = random.randint(1, max_n)
265
266    problem = f"What is the {n}th Fibonacci number?"
267    solution = int(
268        (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5)))
269
270    return problem, f'${solution}$'
271
272
273def nth_tribonacci_number(min_length=1, max_length=80):
274    r"""nth Tribonacci number
275
276    | Ex. Problem | Ex. Solution |
277    | --- | --- |
278    | What is the 14th Tribonacci number? | $504$ |
279    """
280
281    tribDict = {0: 0, 1: 0, 2: 1}
282
283    def recTrib(i):
284        if i not in tribDict:
285            tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3)
286        return tribDict[i]
287
288    n = random.randint(min_length, max_length)
289    problem = f"What is the {n}th Tribonacci number?"
290    solution = recTrib(n - 1)
291    return problem, f'${solution}$'
292
293
294def tribonacci_series(min_length=1, max_length=80):
295    r"""Fibonacci Series
296
297    | Ex. Problem | Ex. Solution |
298    | --- | --- |
299    | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ |
300    """
301
302    tribDict = {0: 0, 1: 0, 2: 1}
303
304    def createTribSeries(i):
305        tribSeries = []
306        for idx in range(i):
307            if idx not in tribDict:
308                tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3]
309            tribSeries.append(tribDict[idx])
310        return tribSeries
311
312    n = random.randint(min_length, max_length)
313    tribSeries = createTribSeries(n)
314    problem = "The Tribonacci Series of the first ${n}$ numbers is ?"
315    solution = ', '.join(map(str, tribSeries))
316    return problem, f'${solution}$'
317
318def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2):
319    r""" Calculating the Cylinder, Head and Sector numbers for a given LBA
320    | Ex. Problem | Ex. Solution |
321    | --- | --- |
322    | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ |
323    """
324    lba = random.randint(0, max_lba)
325    cylinder = math.floor(lba/(sectors_per_track*number_of_heads));
326    t = cylinder*(sectors_per_track*number_of_heads);
327    t = lba-t;
328    head = math.floor(t/(sectors_per_track));
329    sector = t - (head * lba) + 1;
330
331    problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?"
332    solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$"
333    return problem, solution
def floating_point_binary_to_decimal(mantissa_length=8, exponent_length=4):
 5def floating_point_binary_to_decimal(mantissa_length=8, exponent_length=4):
 6    r"""Floating Point Binary to Decimal
 7
 8    | Ex. Problem | Ex. Solution |
 9    | --- | --- |
10    | There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long | $011100000010 = 3.5$ |
11
12    """
13    # TODO Could do with some work ensuring the number is not too small or too big.
14    mantissa = ''
15    for x in range(mantissa_length):
16        mantissa += str(random.randint(0,1))
17    exponent = ''
18    for x in range(exponent_length):
19        exponent += str(random.randint(0,1))
20    decimal_mantissa = int(mantissa, 2)
21    if (decimal_mantissa & (1 << (mantissa_length - 1))) != 0:
22        decimal_mantissa = decimal_mantissa - (1 << mantissa_length)
23    decimal_exponent = int(exponent, 2)
24    if (decimal_exponent & (1 << (exponent_length - 1))) != 0:
25        decimal_exponent = decimal_exponent - (1 << exponent_length)
26    answer = decimal_mantissa * 2**(decimal_exponent - (mantissa_length - 1))
27
28    problem = f"There is a floating point binary number ${mantissa}{exponent}$ where the signed mantissa is {mantissa_length} bits long and the signed exponent is {exponent_length} bits long"
29    solution = f'${mantissa}{exponent} = {answer}$'
30
31    return problem, solution

Floating Point Binary to Decimal

Ex. Problem Ex. Solution
There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long $011100000010 = 3.5$
def binary_addition(max_sum=256, max_addend=128):
33def binary_addition(max_sum=256, max_addend=128):
34    r"""Binary Addition
35
36    | Ex. Problem | Ex. Solution |
37    | --- | --- |
38    | In binary, calculate: $101 + 110110 = $ | $111011$ |
39    """
40    if max_addend > max_sum:
41        max_addend = max_sum
42    a = random.randint(0, max_addend)
43    b = random.randint(0, min((max_sum - a), max_addend))
44    c = a + b
45
46    problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ "
47    solution = f'${bin(c).replace('0b', "")}$'
48    return problem, solution

Binary Addition

Ex. Problem Ex. Solution
In binary, calculate: $101 + 110110 = $ $111011$
def bcd_to_decimal(max_number=10000):
50def bcd_to_decimal(max_number=10000):
51    r"""Binary Coded Decimal to Integer
52
53    | Ex. Problem | Ex. Solution |
54    | --- | --- |
55    | Integer of Binary Coded Decimal $4 =$ | $17801$ |
56    """
57    n = random.randint(1000, max_number)
58    binstring = ''
59    while True:
60        q, r = divmod(n, 10)
61        nibble = bin(r).replace('0b', "")
62        while len(nibble) < 4:
63            nibble = '0' + nibble
64        binstring = nibble + binstring
65        if q == 0:
66            break
67        else:
68            n = q
69
70    problem = f"Integer of Binary Coded Decimal ${n} =$ "
71    solution = f'${int(binstring, 2)}$'
72    return problem, solution

Binary Coded Decimal to Integer

Ex. Problem Ex. Solution
Integer of Binary Coded Decimal $4 =$ $17801$
def binary_2s_complement(maxDigits=10):
 75def binary_2s_complement(maxDigits=10):
 76    r"""Binary 2's Complement
 77
 78    | Ex. Problem | Ex. Solution |
 79    | --- | --- |
 80    | 2's complement of $1011 = $ | $101$ |
 81    """
 82    digits = random.randint(1, maxDigits)
 83    question = ''.join([str(random.randint(0, 1))
 84                        for i in range(digits)]).lstrip('0')
 85
 86    answer = []
 87    for i in question:
 88        answer.append(str(int(not bool(int(i)))))
 89
 90    carry = True
 91    j = len(answer) - 1
 92    while j >= 0:
 93        if answer[j] == '0':
 94            answer[j] = '1'
 95            carry = False
 96            break
 97        answer[j] = '0'
 98        j -= 1
 99
100    if j == 0 and carry is True:
101        answer.insert(0, '1')
102
103    problem = f"2's complement of ${question} = $"
104    solution = ''.join(answer).lstrip('0')
105    return problem, f'${solution}$'

Binary 2's Complement

Ex. Problem Ex. Solution
2's complement of $1011 = $ $101$
def binary_complement_1s(maxDigits=10):
108def binary_complement_1s(maxDigits=10):
109    r"""Binary Complement 1s
110
111    | Ex. Problem | Ex. Solution |
112    | --- | --- |
113    | $1111001 = $ | $0000110$ |
114    """
115    question = ''.join([
116        str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits))
117    ])
118    answer = ''.join(["0" if digit == "1" else "1" for digit in question])
119
120    problem = f'${question} = $'
121    return problem, f'${answer}$'

Binary Complement 1s

Ex. Problem Ex. Solution
$1111001 = $ $0000110$
def binary_to_decimal(max_dig=10):
124def binary_to_decimal(max_dig=10):
125    r"""Binary to Decimal
126
127    | Ex. Problem | Ex. Solution |
128    | --- | --- |
129    | $000110$ | $6$ |
130    """
131    problem = ''.join(
132        [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))])
133    solution = f'${int(problem, 2)}$'
134    return f'${problem}$', solution

Binary to Decimal

Ex. Problem Ex. Solution
$000110$ $6$
def binary_to_hex(max_dig=10):
137def binary_to_hex(max_dig=10):
138    r"""Binary to Hexidecimal
139
140    | Ex. Problem | Ex. Solution |
141    | --- | --- |
142    | $010101$ | $0x15$ |
143    """
144    problem = ''.join(
145        [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))])
146    solution = f'${hex(int(problem, 2))}$'
147    return f'${problem}$', solution

Binary to Hexidecimal

Ex. Problem Ex. Solution
$010101$ $0x15$
def decimal_to_bcd(max_number=10000):
150def decimal_to_bcd(max_number=10000):
151    r"""Decimal to Binary Coded Decimal
152
153    | Ex. Problem | Ex. Solution |
154    | --- | --- |
155    | BCD of Decimal Number $6575 = $ | $191015$ |
156    """
157    n = random.randint(1000, max_number)
158    x = n
159    # binstring = ''
160    bcdstring = ''
161    while x > 0:
162        nibble = x % 16
163        bcdstring = str(nibble) + bcdstring
164        x >>= 4
165
166    problem = f"BCD of Decimal Number ${n} = $"
167    return problem, f'${bcdstring}$'

Decimal to Binary Coded Decimal

Ex. Problem Ex. Solution
BCD of Decimal Number $6575 = $ $191015$
def decimal_to_binary(max_dec=99):
170def decimal_to_binary(max_dec=99):
171    r"""Decimal to Binary
172
173    | Ex. Problem | Ex. Solution |
174    | --- | --- |
175    | Binary of $4 = $ | $100$ |
176    """
177    a = random.randint(1, max_dec)
178    b = bin(a).replace("0b", "")
179
180    problem = f'Binary of ${a} = $'
181    solution = f'${b}$'
182    return problem, solution

Decimal to Binary

Ex. Problem Ex. Solution
Binary of $4 = $ $100$
def decimal_to_hexadeci(max_dec=1000):
185def decimal_to_hexadeci(max_dec=1000):
186    r"""Decimal to Hexadecimal
187
188    | Ex. Problem | Ex. Solution |
189    | --- | --- |
190    | Hexadecimal of $410 = $ | $0x19a$ |
191    """
192    a = random.randint(0, max_dec)
193    b = hex(a)
194
195    problem = f"Hexadecimal of ${a} = $"
196    solution = f"${b}$"
197    return problem, solution

Decimal to Hexadecimal

Ex. Problem Ex. Solution
Hexadecimal of $410 = $ $0x19a$
def decimal_to_octal(max_decimal=4096):
200def decimal_to_octal(max_decimal=4096):
201    r"""Decimal to Octal
202
203    | Ex. Problem | Ex. Solution |
204    | --- | --- |
205    | The decimal number $3698$ in octal is: | $0o7162$ |
206    """
207    x = random.randint(0, max_decimal)
208
209    problem = f"The decimal number ${x}$ in octal is: "
210    solution = f'${oct(x)}$'
211
212    return problem, solution

Decimal to Octal

Ex. Problem Ex. Solution
The decimal number $3698$ in octal is: $0o7162$
def fibonacci_series(min_no=1):
215def fibonacci_series(min_no=1):
216    r"""Fibonacci Series
217
218    | Ex. Problem | Ex. Solution |
219    | --- | --- |
220    | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ |
221    """
222    n = random.randint(min_no, 20)
223
224    def createFibList(n):
225        list = []
226        for i in range(n):
227            if i < 2:
228                list.append(i)
229            else:
230                val = list[i - 1] + list[i - 2]
231                list.append(val)
232        return list
233
234    fibList = createFibList(n)
235
236    problem = "The Fibonacci Series of the first ${n}$ numbers is ?"
237    solution = ', '.join(map(str, fibList))
238    return problem, f'${solution}$'

Fibonacci Series

Ex. Problem Ex. Solution
The Fibonacci Series of the first ${n}$ numbers is ? $0, 1, 1, 2, 3, 5, 8, 13, 21$
def modulo_division(max_res=99, max_modulo=99):
241def modulo_division(max_res=99, max_modulo=99):
242    r"""Modulo Division
243
244    | Ex. Problem | Ex. Solution |
245    | --- | --- |
246    | $43$ % $33 = $ | $10$ |
247    """
248    a = random.randint(0, max_modulo)
249    b = random.randint(0, min(max_res, max_modulo))
250    c = a % b if b != 0 else 0
251
252    problem = f'${a}$ % ${b} = $'
253    solution = f'${c}$'
254    return problem, solution

Modulo Division

Ex. Problem Ex. Solution
$43$ % $33 = $ $10$
def nth_fibonacci_number(max_n=100):
257def nth_fibonacci_number(max_n=100):
258    r"""nth Fibonacci number
259
260    | Ex. Problem | Ex. Solution |
261    | --- | --- |
262    | What is the 85th Fibonacci number? | $259695496911123328$ |
263    """
264    gratio = (1 + math.sqrt(5)) / 2
265    n = random.randint(1, max_n)
266
267    problem = f"What is the {n}th Fibonacci number?"
268    solution = int(
269        (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5)))
270
271    return problem, f'${solution}$'

nth Fibonacci number

Ex. Problem Ex. Solution
What is the 85th Fibonacci number? $259695496911123328$
def nth_tribonacci_number(min_length=1, max_length=80):
274def nth_tribonacci_number(min_length=1, max_length=80):
275    r"""nth Tribonacci number
276
277    | Ex. Problem | Ex. Solution |
278    | --- | --- |
279    | What is the 14th Tribonacci number? | $504$ |
280    """
281
282    tribDict = {0: 0, 1: 0, 2: 1}
283
284    def recTrib(i):
285        if i not in tribDict:
286            tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3)
287        return tribDict[i]
288
289    n = random.randint(min_length, max_length)
290    problem = f"What is the {n}th Tribonacci number?"
291    solution = recTrib(n - 1)
292    return problem, f'${solution}$'

nth Tribonacci number

Ex. Problem Ex. Solution
What is the 14th Tribonacci number? $504$
def tribonacci_series(min_length=1, max_length=80):
295def tribonacci_series(min_length=1, max_length=80):
296    r"""Fibonacci Series
297
298    | Ex. Problem | Ex. Solution |
299    | --- | --- |
300    | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ |
301    """
302
303    tribDict = {0: 0, 1: 0, 2: 1}
304
305    def createTribSeries(i):
306        tribSeries = []
307        for idx in range(i):
308            if idx not in tribDict:
309                tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3]
310            tribSeries.append(tribDict[idx])
311        return tribSeries
312
313    n = random.randint(min_length, max_length)
314    tribSeries = createTribSeries(n)
315    problem = "The Tribonacci Series of the first ${n}$ numbers is ?"
316    solution = ', '.join(map(str, tribSeries))
317    return problem, f'${solution}$'

Fibonacci Series

Ex. Problem Ex. Solution
The Tribonacci Series of the first $8$ numbers is ? $0, 0, 1, 1, 2, 4, 7, 13$
def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2):
319def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2):
320    r""" Calculating the Cylinder, Head and Sector numbers for a given LBA
321    | Ex. Problem | Ex. Solution |
322    | --- | --- |
323    | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ |
324    """
325    lba = random.randint(0, max_lba)
326    cylinder = math.floor(lba/(sectors_per_track*number_of_heads));
327    t = cylinder*(sectors_per_track*number_of_heads);
328    t = lba-t;
329    head = math.floor(t/(sectors_per_track));
330    sector = t - (head * lba) + 1;
331
332    problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?"
333    solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$"
334    return problem, solution

Calculating the Cylinder, Head and Sector numbers for a given LBA

Ex. Problem Ex. Solution
If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? Cylinder: $62$, Head: $0$, Sector: $12$