mathgenerator.basic_math

  1import random
  2
  3
  4def absolute_difference(max_a=100, max_b=100):
  5    r"""Absolute difference between two numbers
  6
  7    | Ex. Problem | Ex. Solution |
  8    | --- | --- |
  9    | $\|22-34\|=$ | $12$ |
 10    """
 11    a = random.randint(-1 * max_a, max_a)
 12    b = random.randint(-1 * max_b, max_b)
 13    absDiff = abs(a - b)
 14
 15    return f'$|{a}-{b}|=$', f"${absDiff}$"
 16
 17
 18def addition(max_sum=99, max_addend=50):
 19    r"""Addition of two numbers
 20
 21    | Ex. Problem | Ex. Solution |
 22    | --- | --- |
 23    | $22+34=$ | $56$ |
 24    """
 25    if max_addend > max_sum:
 26        max_addend = max_sum
 27    a = random.randint(0, max_addend)
 28    # The highest value of b will be no higher than the max_sum minus the first number and no higher than the max_addend as well
 29    b = random.randint(0, min((max_sum - a), max_addend))
 30    c = a + b
 31
 32    problem = f'${a}+{b}=$'
 33    solution = f'${c}$'
 34    return problem, solution
 35
 36
 37def compare_fractions(max_val=10):
 38    r"""Compare Fractions
 39
 40    | Ex. Problem | Ex. Solution |
 41    | --- | --- |
 42    | Which symbol represents the comparison between $\frac{1}{2}$ and $\frac{3}{4}$? | $>$ |
 43    """
 44    a = random.randint(1, max_val)
 45    b = random.randint(1, max_val)
 46    c = random.randint(1, max_val)
 47    d = random.randint(1, max_val)
 48
 49    while (a == b):
 50        b = random.randint(1, max_val)
 51    while (c == d):
 52        d = random.randint(1, max_val)
 53
 54    first = a / b
 55    second = c / d
 56
 57    if (first > second):
 58        solution = ">"
 59    elif (first < second):
 60        solution = "<"
 61    else:
 62        solution = "="
 63
 64    problem = rf"Which symbol represents the comparison between $\frac{{{a}}}{{{b}}}$ and $\frac{{{c}}}{{{d}}}$?"
 65    return problem, solution
 66
 67
 68def cube_root(min_no=1, max_no=1000):
 69    r"""Cube Root
 70
 71    | Ex. Problem | Ex. Solution |
 72    | --- | --- |
 73    | What is the cube root of: $\sqrt[3]{125}=$ to 2 decimal places? | $5$ |
 74    """
 75    b = random.randint(min_no, max_no)
 76    a = b**(1 / 3)
 77
 78    return (rf"What is the cube root of: $\sqrt[3]{{{b}}}=$ to 2 decimal places?", f"${round(a, 2)}$")
 79
 80
 81def divide_fractions(max_val=10):
 82    r"""Divide Fractions
 83
 84    | Ex. Problem | Ex. Solution |
 85    | --- | --- |
 86    | $\frac{7}{9}\div\frac{4}{1}=$ | $\frac{7}{36}$ |
 87    """
 88    a = random.randint(1, max_val)
 89    b = random.randint(1, max_val)
 90
 91    while (a == b):
 92        b = random.randint(1, max_val)
 93
 94    c = random.randint(1, max_val)
 95    d = random.randint(1, max_val)
 96    while (c == d):
 97        d = random.randint(1, max_val)
 98
 99    def calculate_gcd(x, y):
100        while (y):
101            x, y = y, x % y
102        return x
103
104    tmp_n = a * d
105    tmp_d = b * c
106
107    gcd = calculate_gcd(tmp_n, tmp_d)
108    sol_numerator = tmp_n // gcd
109    sol_denominator = tmp_d // gcd
110
111    return rf'$\frac{{{a}}}{{{b}}}\div\frac{{{c}}}{{{d}}}=$', f'$\frac{{{sol_numerator}}}{{{sol_denominator}}}$'
112
113
114def division(max_a=25, max_b=25):
115    r"""Division
116
117    | Ex. Problem | Ex. Solution |
118    | --- | --- |
119    | $216\div24=$ | $9$ |
120    """
121    a = random.randint(1, max_a)
122    b = random.randint(1, max_b)
123
124    divisor = a * b
125    dividend = random.choice([a, b])
126    quotient = int(divisor / dividend)
127
128    return rf'${divisor}\div{dividend}=$', f'${quotient}$'
129
130
131def exponentiation(max_base=20, max_expo=10):
132    r"""Exponentiation
133
134    | Ex. Problem | Ex. Solution |
135    | --- | --- |
136    | $9^{5}=$ | $8$ |
137    """
138    base = random.randint(1, max_base)
139    expo = random.randint(1, max_expo)
140
141    return f'${base}^{{{expo}}}=$', f'${base**expo}$'
142
143
144def factorial(max_input=6):
145    r"""Factorial
146
147    | Ex. Problem | Ex. Solution |
148    | --- | --- |
149    | $4! =$ | $24$ |
150    """
151    a = random.randint(0, max_input)
152    n = a
153    b = 1
154    while a != 1 and n > 0:
155        b *= n
156        n -= 1
157
158    return f'${a}! =$', f'${b}$'
159
160
161def fraction_multiplication(max_val=10):
162    r"""Fraction Multiplication
163
164    | Ex. Problem | Ex. Solution |
165    | --- | --- |
166    | $\frac{3}{10}\cdot\frac{6}{7}=$ | $\frac{9}{35}$ |
167    """
168    a = random.randint(1, max_val)
169    b = random.randint(1, max_val)
170    c = random.randint(1, max_val)
171    d = random.randint(1, max_val)
172
173    while (a == b):
174        b = random.randint(1, max_val)
175
176    while (c == d):
177        d = random.randint(1, max_val)
178
179    def calculate_gcd(x, y):
180        while (y):
181            x, y = y, x % y
182        return x
183
184    tmp_n = a * c
185    tmp_d = b * d
186
187    gcd = calculate_gcd(tmp_n, tmp_d)
188
189    problem = rf"$\frac{{{a}}}{{{b}}}\cdot\frac{{{c}}}{{{d}}}=$"
190    if (tmp_d == 1 or tmp_d == gcd):
191        solution = rf"$\frac{{{tmp_n}}}{{{gcd}}}$"
192    else:
193        solution = rf"$\frac{{{tmp_n//gcd}}}{{{tmp_d//gcd}}}$"
194    return problem, solution
195
196
197def fraction_to_decimal(max_res=99, max_divid=99):
198    r"""Fraction to Decimal
199
200    | Ex. Problem | Ex. Solution |
201    | --- | --- |
202    | $83\div80=$ | $1.04$ |
203    """
204    a = random.randint(0, max_divid)
205    b = random.randint(1, min(max_res, max_divid))
206    c = round(a / b, 2)
207
208    return rf'${a}\div{b}=$', f'${c}$'
209
210
211def greatest_common_divisor(numbers_count=2, max_num=10**3):
212    r"""Greatest Common Divisor of N Numbers ( GCD / HCF )
213
214    | Ex. Problem | Ex. Solution |
215    | --- | --- |
216    | $GCD(488075608, 75348096)=$ | $8$ |
217    """
218
219    def greatestCommonDivisorOfTwoNumbers(number1, number2):
220        number1 = abs(number1)
221        number2 = abs(number2)
222        while number2 > 0:
223            number1, number2 = number2, number1 % number2
224        return number1
225
226    numbers_count = max(numbers_count, 2)
227    numbers = [random.randint(0, max_num)
228               for _ in range(numbers_count)]
229
230    greatestCommonDivisor = greatestCommonDivisorOfTwoNumbers(
231        numbers[0], numbers[1])
232
233    for index in range(1, numbers_count):
234        greatestCommonDivisor = greatestCommonDivisorOfTwoNumbers(
235            numbers[index], greatestCommonDivisor)
236
237    return f'$GCD({",".join(map(str, numbers))})=$', f"${greatestCommonDivisor}$"
238
239
240def is_composite(max_num=250):
241    r"""Is Composite
242
243    | Ex. Problem | Ex. Solution |
244    | --- | --- |
245    | Is $171$ composite? | Yes |
246    """
247    a = random.randint(2, max_num)
248
249    problem = f"Is ${a}$ composite?"
250    if a == 0 or a == 1:
251        return problem, "No"
252    for i in range(2, a):
253        if a % i == 0:
254            return problem, "Yes"
255    solution = "No"
256
257    return problem, solution
258
259
260def is_prime(max_num=100):
261    r"""Is Prime
262
263    | Ex. Problem | Ex. Solution |
264    | --- | --- |
265    | Is $37$ prime? | Yes |
266    """
267    a = random.randint(2, max_num)
268    problem = f"Is ${a}$ prime?"
269    if a == 2:
270        return problem, "Yes"
271    if a % 2 == 0:
272        return problem, "No"
273    for i in range(3, a // 2 + 1, 2):
274        if a % i == 0:
275            return problem, "No"
276    solution = "Yes"
277
278    return problem, solution
279
280
281def multiplication(max_multi=12):
282    r"""Multiplication
283
284    | Ex. Problem | Ex. Solution |
285    | --- | --- |
286    | $10\cdot9=$ | $90$ |
287    """
288    a = random.randint(0, max_multi)
289    b = random.randint(0, max_multi)
290    c = a * b
291
292    return rf'${a}\cdot{b}=$', f'${c}$'
293
294
295def percentage(max_value=99, max_percentage=99):
296    r"""Percentage of a number
297
298    | Ex. Problem | Ex. Solution |
299    | --- | --- |
300    | What is $45$% of $39$? | $17.55$ |
301    """
302    a = random.randint(1, max_percentage)
303    b = random.randint(1, max_value)
304    problem = f"What is ${a}$% of ${b}$?"
305    percentage = a / 100 * b
306    formatted_float = "{:.2f}".format(percentage)
307    solution = f"${formatted_float}$"
308
309    return problem, solution
310
311
312def percentage_difference(max_value=200, min_value=0):
313    r"""Percentage difference between two numbers
314
315    | Ex. Problem | Ex. Solution |
316    | --- | --- |
317    | What is the percentage difference between $2$ and $10$? | $133.33$ |
318    """
319    value_a = random.randint(min_value, max_value)
320    value_b = random.randint(min_value, max_value)
321
322    diff = 2 * (abs(value_a - value_b) / abs(value_a + value_b)) * 100
323    diff = round(diff, 2)
324
325    problem = f"What is the percentage difference between ${value_a}$ and ${value_b}$?"
326    solution = f'${diff}$%'
327    return problem, solution
328
329
330def percentage_error(max_value=100, min_value=-100):
331    r"""Percentage error
332
333    | Ex. Problem | Ex. Solution |
334    | --- | --- |
335    | Find the percentage error when observed value equals $32$ and exact value equals $81$. | $60.49$% |
336    """
337    observed_value = random.randint(min_value, max_value)
338    exact_value = random.randint(min_value, max_value)
339
340    if observed_value * exact_value < 0:
341        observed_value *= -1
342
343    error = (abs(observed_value - exact_value) / abs(exact_value)) * 100
344    error = round(error, 2)
345
346    problem = f"Find the percentage error when observed value equals ${observed_value}$ and exact value equals ${exact_value}$."
347    solution = f'${error}$%'
348    return problem, solution
349
350
351def power_of_powers(max_base=50, max_power=10):
352    r"""Power of Powers
353
354    | Ex. Problem | Ex. Solution |
355    | --- | --- |
356    | Simplify $18^{10^{8}}$ | $18^{80}$ |
357    """
358    base = random.randint(1, max_base)
359    power1 = random.randint(1, max_power)
360    power2 = random.randint(1, max_power)
361    step = power1 * power2
362
363    problem = f"Simplify ${base}^{{{power1}^{{{power2}}}}}$"
364    solution = f"${base}^{{{step}}}$"
365    return problem, solution
366
367
368def square(max_square_num=20):
369    r"""Square
370
371    | Ex. Problem | Ex. Solution |
372    | --- | --- |
373    | $17^2=$ | $289$ |
374    """
375    a = random.randint(1, max_square_num)
376    b = a ** 2
377
378    return f'${a}^2=$', f'${b}$'
379
380
381def square_root(min_no=1, max_no=12):
382    r"""Square Root
383
384    | Ex. Problem | Ex. Solution |
385    | --- | --- |
386    | $\sqrt{64}=$ | $8$ |
387    """
388    b = random.randint(min_no, max_no)
389    a = b ** 2
390
391    return rf'$\sqrt{{{a}}}=$', f'${b}$'
392
393
394def simplify_square_root(max_variable=100):
395    r"""Simplify Square Root
396
397    | Ex. Problem | Ex. Solution |
398    | --- | --- |
399    | $\sqrt{63}$ | $3\sqrt{7}$ |
400    """
401    y = x = random.randint(1, max_variable)
402    factors = {}
403    f = 2
404    while x != 1:
405        if x % f == 0:
406            if f not in factors:
407                factors[f] = 0
408            factors[f] += 1
409            x /= f
410        else:
411            f += 1
412    a = b = 1
413    for i in factors.keys():
414        if factors[i] & 1 == 0:
415            a *= i ** (factors[i] // 2)
416        else:
417            a *= i ** ((factors[i] - 1) // 2)
418            b *= i
419    if a == 1 or b == 1:
420        return simplify_square_root(max_variable)
421    else:
422        return rf'$\sqrt{{{y}}}$', rf'${a}\sqrt{{{b}}}$'
423
424
425def subtraction(max_minuend=99, max_diff=99):
426    r"""Subtraction of two numbers
427
428    | Ex. Problem | Ex. Solution |
429    | --- | --- |
430    | $54-22=$ | $32$ |
431    """
432    a = random.randint(0, max_minuend)
433    b = random.randint(max(0, (a - max_diff)), a)
434    c = a - b
435
436    return f'${a}-{b}=$', f'${c}$'
def absolute_difference(max_a=100, max_b=100):
 5def absolute_difference(max_a=100, max_b=100):
 6    r"""Absolute difference between two numbers
 7
 8    | Ex. Problem | Ex. Solution |
 9    | --- | --- |
10    | $\|22-34\|=$ | $12$ |
11    """
12    a = random.randint(-1 * max_a, max_a)
13    b = random.randint(-1 * max_b, max_b)
14    absDiff = abs(a - b)
15
16    return f'$|{a}-{b}|=$', f"${absDiff}$"

Absolute difference between two numbers

Ex. Problem Ex. Solution
$|22-34|=$ $12$
def addition(max_sum=99, max_addend=50):
19def addition(max_sum=99, max_addend=50):
20    r"""Addition of two numbers
21
22    | Ex. Problem | Ex. Solution |
23    | --- | --- |
24    | $22+34=$ | $56$ |
25    """
26    if max_addend > max_sum:
27        max_addend = max_sum
28    a = random.randint(0, max_addend)
29    # The highest value of b will be no higher than the max_sum minus the first number and no higher than the max_addend as well
30    b = random.randint(0, min((max_sum - a), max_addend))
31    c = a + b
32
33    problem = f'${a}+{b}=$'
34    solution = f'${c}$'
35    return problem, solution

Addition of two numbers

Ex. Problem Ex. Solution
$22+34=$ $56$
def compare_fractions(max_val=10):
38def compare_fractions(max_val=10):
39    r"""Compare Fractions
40
41    | Ex. Problem | Ex. Solution |
42    | --- | --- |
43    | Which symbol represents the comparison between $\frac{1}{2}$ and $\frac{3}{4}$? | $>$ |
44    """
45    a = random.randint(1, max_val)
46    b = random.randint(1, max_val)
47    c = random.randint(1, max_val)
48    d = random.randint(1, max_val)
49
50    while (a == b):
51        b = random.randint(1, max_val)
52    while (c == d):
53        d = random.randint(1, max_val)
54
55    first = a / b
56    second = c / d
57
58    if (first > second):
59        solution = ">"
60    elif (first < second):
61        solution = "<"
62    else:
63        solution = "="
64
65    problem = rf"Which symbol represents the comparison between $\frac{{{a}}}{{{b}}}$ and $\frac{{{c}}}{{{d}}}$?"
66    return problem, solution

Compare Fractions

Ex. Problem Ex. Solution
Which symbol represents the comparison between $\frac{1}{2}$ and $\frac{3}{4}$? $>$
def cube_root(min_no=1, max_no=1000):
69def cube_root(min_no=1, max_no=1000):
70    r"""Cube Root
71
72    | Ex. Problem | Ex. Solution |
73    | --- | --- |
74    | What is the cube root of: $\sqrt[3]{125}=$ to 2 decimal places? | $5$ |
75    """
76    b = random.randint(min_no, max_no)
77    a = b**(1 / 3)
78
79    return (rf"What is the cube root of: $\sqrt[3]{{{b}}}=$ to 2 decimal places?", f"${round(a, 2)}$")

Cube Root

Ex. Problem Ex. Solution
What is the cube root of: $\sqrt[3]{125}=$ to 2 decimal places? $5$
def divide_fractions(max_val=10):
 82def divide_fractions(max_val=10):
 83    r"""Divide Fractions
 84
 85    | Ex. Problem | Ex. Solution |
 86    | --- | --- |
 87    | $\frac{7}{9}\div\frac{4}{1}=$ | $\frac{7}{36}$ |
 88    """
 89    a = random.randint(1, max_val)
 90    b = random.randint(1, max_val)
 91
 92    while (a == b):
 93        b = random.randint(1, max_val)
 94
 95    c = random.randint(1, max_val)
 96    d = random.randint(1, max_val)
 97    while (c == d):
 98        d = random.randint(1, max_val)
 99
100    def calculate_gcd(x, y):
101        while (y):
102            x, y = y, x % y
103        return x
104
105    tmp_n = a * d
106    tmp_d = b * c
107
108    gcd = calculate_gcd(tmp_n, tmp_d)
109    sol_numerator = tmp_n // gcd
110    sol_denominator = tmp_d // gcd
111
112    return rf'$\frac{{{a}}}{{{b}}}\div\frac{{{c}}}{{{d}}}=$', f'$\frac{{{sol_numerator}}}{{{sol_denominator}}}$'

Divide Fractions

Ex. Problem Ex. Solution
$\frac{7}{9}\div\frac{4}{1}=$ $\frac{7}{36}$
def division(max_a=25, max_b=25):
115def division(max_a=25, max_b=25):
116    r"""Division
117
118    | Ex. Problem | Ex. Solution |
119    | --- | --- |
120    | $216\div24=$ | $9$ |
121    """
122    a = random.randint(1, max_a)
123    b = random.randint(1, max_b)
124
125    divisor = a * b
126    dividend = random.choice([a, b])
127    quotient = int(divisor / dividend)
128
129    return rf'${divisor}\div{dividend}=$', f'${quotient}$'

Division

Ex. Problem Ex. Solution
$216\div24=$ $9$
def exponentiation(max_base=20, max_expo=10):
132def exponentiation(max_base=20, max_expo=10):
133    r"""Exponentiation
134
135    | Ex. Problem | Ex. Solution |
136    | --- | --- |
137    | $9^{5}=$ | $8$ |
138    """
139    base = random.randint(1, max_base)
140    expo = random.randint(1, max_expo)
141
142    return f'${base}^{{{expo}}}=$', f'${base**expo}$'

Exponentiation

Ex. Problem Ex. Solution
$9^{5}=$ $8$
def factorial(max_input=6):
145def factorial(max_input=6):
146    r"""Factorial
147
148    | Ex. Problem | Ex. Solution |
149    | --- | --- |
150    | $4! =$ | $24$ |
151    """
152    a = random.randint(0, max_input)
153    n = a
154    b = 1
155    while a != 1 and n > 0:
156        b *= n
157        n -= 1
158
159    return f'${a}! =$', f'${b}$'

Factorial

Ex. Problem Ex. Solution
$4! =$ $24$
def fraction_multiplication(max_val=10):
162def fraction_multiplication(max_val=10):
163    r"""Fraction Multiplication
164
165    | Ex. Problem | Ex. Solution |
166    | --- | --- |
167    | $\frac{3}{10}\cdot\frac{6}{7}=$ | $\frac{9}{35}$ |
168    """
169    a = random.randint(1, max_val)
170    b = random.randint(1, max_val)
171    c = random.randint(1, max_val)
172    d = random.randint(1, max_val)
173
174    while (a == b):
175        b = random.randint(1, max_val)
176
177    while (c == d):
178        d = random.randint(1, max_val)
179
180    def calculate_gcd(x, y):
181        while (y):
182            x, y = y, x % y
183        return x
184
185    tmp_n = a * c
186    tmp_d = b * d
187
188    gcd = calculate_gcd(tmp_n, tmp_d)
189
190    problem = rf"$\frac{{{a}}}{{{b}}}\cdot\frac{{{c}}}{{{d}}}=$"
191    if (tmp_d == 1 or tmp_d == gcd):
192        solution = rf"$\frac{{{tmp_n}}}{{{gcd}}}$"
193    else:
194        solution = rf"$\frac{{{tmp_n//gcd}}}{{{tmp_d//gcd}}}$"
195    return problem, solution

Fraction Multiplication

Ex. Problem Ex. Solution
$\frac{3}{10}\cdot\frac{6}{7}=$ $\frac{9}{35}$
def fraction_to_decimal(max_res=99, max_divid=99):
198def fraction_to_decimal(max_res=99, max_divid=99):
199    r"""Fraction to Decimal
200
201    | Ex. Problem | Ex. Solution |
202    | --- | --- |
203    | $83\div80=$ | $1.04$ |
204    """
205    a = random.randint(0, max_divid)
206    b = random.randint(1, min(max_res, max_divid))
207    c = round(a / b, 2)
208
209    return rf'${a}\div{b}=$', f'${c}$'

Fraction to Decimal

Ex. Problem Ex. Solution
$83\div80=$ $1.04$
def greatest_common_divisor(numbers_count=2, max_num=1000):
212def greatest_common_divisor(numbers_count=2, max_num=10**3):
213    r"""Greatest Common Divisor of N Numbers ( GCD / HCF )
214
215    | Ex. Problem | Ex. Solution |
216    | --- | --- |
217    | $GCD(488075608, 75348096)=$ | $8$ |
218    """
219
220    def greatestCommonDivisorOfTwoNumbers(number1, number2):
221        number1 = abs(number1)
222        number2 = abs(number2)
223        while number2 > 0:
224            number1, number2 = number2, number1 % number2
225        return number1
226
227    numbers_count = max(numbers_count, 2)
228    numbers = [random.randint(0, max_num)
229               for _ in range(numbers_count)]
230
231    greatestCommonDivisor = greatestCommonDivisorOfTwoNumbers(
232        numbers[0], numbers[1])
233
234    for index in range(1, numbers_count):
235        greatestCommonDivisor = greatestCommonDivisorOfTwoNumbers(
236            numbers[index], greatestCommonDivisor)
237
238    return f'$GCD({",".join(map(str, numbers))})=$', f"${greatestCommonDivisor}$"

Greatest Common Divisor of N Numbers ( GCD / HCF )

Ex. Problem Ex. Solution
$GCD(488075608, 75348096)=$ $8$
def is_composite(max_num=250):
241def is_composite(max_num=250):
242    r"""Is Composite
243
244    | Ex. Problem | Ex. Solution |
245    | --- | --- |
246    | Is $171$ composite? | Yes |
247    """
248    a = random.randint(2, max_num)
249
250    problem = f"Is ${a}$ composite?"
251    if a == 0 or a == 1:
252        return problem, "No"
253    for i in range(2, a):
254        if a % i == 0:
255            return problem, "Yes"
256    solution = "No"
257
258    return problem, solution

Is Composite

Ex. Problem Ex. Solution
Is $171$ composite? Yes
def is_prime(max_num=100):
261def is_prime(max_num=100):
262    r"""Is Prime
263
264    | Ex. Problem | Ex. Solution |
265    | --- | --- |
266    | Is $37$ prime? | Yes |
267    """
268    a = random.randint(2, max_num)
269    problem = f"Is ${a}$ prime?"
270    if a == 2:
271        return problem, "Yes"
272    if a % 2 == 0:
273        return problem, "No"
274    for i in range(3, a // 2 + 1, 2):
275        if a % i == 0:
276            return problem, "No"
277    solution = "Yes"
278
279    return problem, solution

Is Prime

Ex. Problem Ex. Solution
Is $37$ prime? Yes
def multiplication(max_multi=12):
282def multiplication(max_multi=12):
283    r"""Multiplication
284
285    | Ex. Problem | Ex. Solution |
286    | --- | --- |
287    | $10\cdot9=$ | $90$ |
288    """
289    a = random.randint(0, max_multi)
290    b = random.randint(0, max_multi)
291    c = a * b
292
293    return rf'${a}\cdot{b}=$', f'${c}$'

Multiplication

Ex. Problem Ex. Solution
$10\cdot9=$ $90$
def percentage(max_value=99, max_percentage=99):
296def percentage(max_value=99, max_percentage=99):
297    r"""Percentage of a number
298
299    | Ex. Problem | Ex. Solution |
300    | --- | --- |
301    | What is $45$% of $39$? | $17.55$ |
302    """
303    a = random.randint(1, max_percentage)
304    b = random.randint(1, max_value)
305    problem = f"What is ${a}$% of ${b}$?"
306    percentage = a / 100 * b
307    formatted_float = "{:.2f}".format(percentage)
308    solution = f"${formatted_float}$"
309
310    return problem, solution

Percentage of a number

Ex. Problem Ex. Solution
What is $45$% of $39$? $17.55$
def percentage_difference(max_value=200, min_value=0):
313def percentage_difference(max_value=200, min_value=0):
314    r"""Percentage difference between two numbers
315
316    | Ex. Problem | Ex. Solution |
317    | --- | --- |
318    | What is the percentage difference between $2$ and $10$? | $133.33$ |
319    """
320    value_a = random.randint(min_value, max_value)
321    value_b = random.randint(min_value, max_value)
322
323    diff = 2 * (abs(value_a - value_b) / abs(value_a + value_b)) * 100
324    diff = round(diff, 2)
325
326    problem = f"What is the percentage difference between ${value_a}$ and ${value_b}$?"
327    solution = f'${diff}$%'
328    return problem, solution

Percentage difference between two numbers

Ex. Problem Ex. Solution
What is the percentage difference between $2$ and $10$? $133.33$
def percentage_error(max_value=100, min_value=-100):
331def percentage_error(max_value=100, min_value=-100):
332    r"""Percentage error
333
334    | Ex. Problem | Ex. Solution |
335    | --- | --- |
336    | Find the percentage error when observed value equals $32$ and exact value equals $81$. | $60.49$% |
337    """
338    observed_value = random.randint(min_value, max_value)
339    exact_value = random.randint(min_value, max_value)
340
341    if observed_value * exact_value < 0:
342        observed_value *= -1
343
344    error = (abs(observed_value - exact_value) / abs(exact_value)) * 100
345    error = round(error, 2)
346
347    problem = f"Find the percentage error when observed value equals ${observed_value}$ and exact value equals ${exact_value}$."
348    solution = f'${error}$%'
349    return problem, solution

Percentage error

Ex. Problem Ex. Solution
Find the percentage error when observed value equals $32$ and exact value equals $81$. $60.49$%
def power_of_powers(max_base=50, max_power=10):
352def power_of_powers(max_base=50, max_power=10):
353    r"""Power of Powers
354
355    | Ex. Problem | Ex. Solution |
356    | --- | --- |
357    | Simplify $18^{10^{8}}$ | $18^{80}$ |
358    """
359    base = random.randint(1, max_base)
360    power1 = random.randint(1, max_power)
361    power2 = random.randint(1, max_power)
362    step = power1 * power2
363
364    problem = f"Simplify ${base}^{{{power1}^{{{power2}}}}}$"
365    solution = f"${base}^{{{step}}}$"
366    return problem, solution

Power of Powers

Ex. Problem Ex. Solution
Simplify $18^{10^{8}}$ $18^{80}$
def square(max_square_num=20):
369def square(max_square_num=20):
370    r"""Square
371
372    | Ex. Problem | Ex. Solution |
373    | --- | --- |
374    | $17^2=$ | $289$ |
375    """
376    a = random.randint(1, max_square_num)
377    b = a ** 2
378
379    return f'${a}^2=$', f'${b}$'

Square

Ex. Problem Ex. Solution
$17^2=$ $289$
def square_root(min_no=1, max_no=12):
382def square_root(min_no=1, max_no=12):
383    r"""Square Root
384
385    | Ex. Problem | Ex. Solution |
386    | --- | --- |
387    | $\sqrt{64}=$ | $8$ |
388    """
389    b = random.randint(min_no, max_no)
390    a = b ** 2
391
392    return rf'$\sqrt{{{a}}}=$', f'${b}$'

Square Root

Ex. Problem Ex. Solution
$\sqrt{64}=$ $8$
def simplify_square_root(max_variable=100):
395def simplify_square_root(max_variable=100):
396    r"""Simplify Square Root
397
398    | Ex. Problem | Ex. Solution |
399    | --- | --- |
400    | $\sqrt{63}$ | $3\sqrt{7}$ |
401    """
402    y = x = random.randint(1, max_variable)
403    factors = {}
404    f = 2
405    while x != 1:
406        if x % f == 0:
407            if f not in factors:
408                factors[f] = 0
409            factors[f] += 1
410            x /= f
411        else:
412            f += 1
413    a = b = 1
414    for i in factors.keys():
415        if factors[i] & 1 == 0:
416            a *= i ** (factors[i] // 2)
417        else:
418            a *= i ** ((factors[i] - 1) // 2)
419            b *= i
420    if a == 1 or b == 1:
421        return simplify_square_root(max_variable)
422    else:
423        return rf'$\sqrt{{{y}}}$', rf'${a}\sqrt{{{b}}}$'

Simplify Square Root

Ex. Problem Ex. Solution
$\sqrt{63}$ $3\sqrt{7}$
def subtraction(max_minuend=99, max_diff=99):
426def subtraction(max_minuend=99, max_diff=99):
427    r"""Subtraction of two numbers
428
429    | Ex. Problem | Ex. Solution |
430    | --- | --- |
431    | $54-22=$ | $32$ |
432    """
433    a = random.randint(0, max_minuend)
434    b = random.randint(max(0, (a - max_diff)), a)
435    c = a - b
436
437    return f'${a}-{b}=$', f'${c}$'

Subtraction of two numbers

Ex. Problem Ex. Solution
$54-22=$ $32$