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 (
 79        rf"What is the cube root of: $\sqrt[3]{{{b}}}=$ to 2 decimal places?",
 80        f"${round(a, 2)}$")
 81
 82
 83def divide_fractions(max_val=10):
 84    r"""Divide Fractions
 85
 86    | Ex. Problem | Ex. Solution |
 87    | --- | --- |
 88    | $\frac{7}{9}\div\frac{4}{1}=$ | $\frac{7}{36}$ |
 89    """
 90    a = random.randint(1, max_val)
 91    b = random.randint(1, max_val)
 92
 93    while (a == b):
 94        b = random.randint(1, max_val)
 95
 96    c = random.randint(1, max_val)
 97    d = random.randint(1, max_val)
 98    while (c == d):
 99        d = random.randint(1, max_val)
100
101    def calculate_gcd(x, y):
102        while (y):
103            x, y = y, x % y
104        return x
105
106    tmp_n = a * d
107    tmp_d = b * c
108
109    gcd = calculate_gcd(tmp_n, tmp_d)
110    sol_numerator = tmp_n // gcd
111    sol_denominator = tmp_d // gcd
112
113    return rf'$\frac{{{a}}}{{{b}}}\div\frac{{{c}}}{{{d}}}=$', f'$\frac{{{sol_numerator}}}{{{sol_denominator}}}$'
114
115
116def division(max_a=25, max_b=25):
117    r"""Division
118
119    | Ex. Problem | Ex. Solution |
120    | --- | --- |
121    | $216\div24=$ | $9$ |
122    """
123    a = random.randint(1, max_a)
124    b = random.randint(1, max_b)
125
126    divisor = a * b
127    dividend = random.choice([a, b])
128    quotient = int(divisor / dividend)
129
130    return rf'${divisor}\div{dividend}=$', f'${quotient}$'
131
132
133def exponentiation(max_base=20, max_expo=10):
134    r"""Exponentiation
135
136    | Ex. Problem | Ex. Solution |
137    | --- | --- |
138    | $9^{5}=$ | $8$ |
139    """
140    base = random.randint(1, max_base)
141    expo = random.randint(1, max_expo)
142
143    return f'${base}^{{{expo}}}=$', f'${base**expo}$'
144
145
146def factorial(max_input=6):
147    r"""Factorial
148
149    | Ex. Problem | Ex. Solution |
150    | --- | --- |
151    | $4! =$ | $24$ |
152    """
153    a = random.randint(0, max_input)
154    n = a
155    b = 1
156    while a != 1 and n > 0:
157        b *= n
158        n -= 1
159
160    return f'${a}! =$', f'${b}$'
161
162
163def fraction_multiplication(max_val=10):
164    r"""Fraction Multiplication
165
166    | Ex. Problem | Ex. Solution |
167    | --- | --- |
168    | $\frac{3}{10}\cdot\frac{6}{7}=$ | $\frac{9}{35}$ |
169    """
170    a = random.randint(1, max_val)
171    b = random.randint(1, max_val)
172    c = random.randint(1, max_val)
173    d = random.randint(1, max_val)
174
175    while (a == b):
176        b = random.randint(1, max_val)
177
178    while (c == d):
179        d = random.randint(1, max_val)
180
181    def calculate_gcd(x, y):
182        while (y):
183            x, y = y, x % y
184        return x
185
186    tmp_n = a * c
187    tmp_d = b * d
188
189    gcd = calculate_gcd(tmp_n, tmp_d)
190
191    problem = rf"$\frac{{{a}}}{{{b}}}\cdot\frac{{{c}}}{{{d}}}=$"
192    if (tmp_d == 1 or tmp_d == gcd):
193        solution = rf"$\frac{{{tmp_n}}}{{{gcd}}}$"
194    else:
195        solution = rf"$\frac{{{tmp_n//gcd}}}{{{tmp_d//gcd}}}$"
196    return problem, solution
197
198
199def fraction_to_decimal(max_res=99, max_divid=99):
200    r"""Fraction to Decimal
201
202    | Ex. Problem | Ex. Solution |
203    | --- | --- |
204    | $83\div80=$ | $1.04$ |
205    """
206    a = random.randint(0, max_divid)
207    b = random.randint(1, min(max_res, max_divid))
208    c = round(a / b, 2)
209
210    return rf'${a}\div{b}=$', f'${c}$'
211
212
213def greatest_common_divisor(numbers_count=2, max_num=10**3):
214    r"""Greatest Common Divisor of N Numbers ( GCD / HCF )
215
216    | Ex. Problem | Ex. Solution |
217    | --- | --- |
218    | $GCD(488075608, 75348096)=$ | $8$ |
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) 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 (
80        rf"What is the cube root of: $\sqrt[3]{{{b}}}=$ to 2 decimal places?",
81        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):
 84def divide_fractions(max_val=10):
 85    r"""Divide Fractions
 86
 87    | Ex. Problem | Ex. Solution |
 88    | --- | --- |
 89    | $\frac{7}{9}\div\frac{4}{1}=$ | $\frac{7}{36}$ |
 90    """
 91    a = random.randint(1, max_val)
 92    b = random.randint(1, max_val)
 93
 94    while (a == b):
 95        b = random.randint(1, max_val)
 96
 97    c = random.randint(1, max_val)
 98    d = random.randint(1, max_val)
 99    while (c == d):
100        d = random.randint(1, max_val)
101
102    def calculate_gcd(x, y):
103        while (y):
104            x, y = y, x % y
105        return x
106
107    tmp_n = a * d
108    tmp_d = b * c
109
110    gcd = calculate_gcd(tmp_n, tmp_d)
111    sol_numerator = tmp_n // gcd
112    sol_denominator = tmp_d // gcd
113
114    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):
117def division(max_a=25, max_b=25):
118    r"""Division
119
120    | Ex. Problem | Ex. Solution |
121    | --- | --- |
122    | $216\div24=$ | $9$ |
123    """
124    a = random.randint(1, max_a)
125    b = random.randint(1, max_b)
126
127    divisor = a * b
128    dividend = random.choice([a, b])
129    quotient = int(divisor / dividend)
130
131    return rf'${divisor}\div{dividend}=$', f'${quotient}$'

Division

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

Exponentiation

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

Factorial

Ex. Problem Ex. Solution
$4! =$ $24$
def fraction_multiplication(max_val=10):
164def fraction_multiplication(max_val=10):
165    r"""Fraction Multiplication
166
167    | Ex. Problem | Ex. Solution |
168    | --- | --- |
169    | $\frac{3}{10}\cdot\frac{6}{7}=$ | $\frac{9}{35}$ |
170    """
171    a = random.randint(1, max_val)
172    b = random.randint(1, max_val)
173    c = random.randint(1, max_val)
174    d = random.randint(1, max_val)
175
176    while (a == b):
177        b = random.randint(1, max_val)
178
179    while (c == d):
180        d = random.randint(1, max_val)
181
182    def calculate_gcd(x, y):
183        while (y):
184            x, y = y, x % y
185        return x
186
187    tmp_n = a * c
188    tmp_d = b * d
189
190    gcd = calculate_gcd(tmp_n, tmp_d)
191
192    problem = rf"$\frac{{{a}}}{{{b}}}\cdot\frac{{{c}}}{{{d}}}=$"
193    if (tmp_d == 1 or tmp_d == gcd):
194        solution = rf"$\frac{{{tmp_n}}}{{{gcd}}}$"
195    else:
196        solution = rf"$\frac{{{tmp_n//gcd}}}{{{tmp_d//gcd}}}$"
197    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):
200def fraction_to_decimal(max_res=99, max_divid=99):
201    r"""Fraction to Decimal
202
203    | Ex. Problem | Ex. Solution |
204    | --- | --- |
205    | $83\div80=$ | $1.04$ |
206    """
207    a = random.randint(0, max_divid)
208    b = random.randint(1, min(max_res, max_divid))
209    c = round(a / b, 2)
210
211    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):
214def greatest_common_divisor(numbers_count=2, max_num=10**3):
215    r"""Greatest Common Divisor of N Numbers ( GCD / HCF )
216
217    | Ex. Problem | Ex. Solution |
218    | --- | --- |
219    | $GCD(488075608, 75348096)=$ | $8$ |
220    """
221    def greatestCommonDivisorOfTwoNumbers(number1, number2):
222        number1 = abs(number1)
223        number2 = abs(number2)
224        while number2 > 0:
225            number1, number2 = number2, number1 % number2
226        return number1
227
228    numbers_count = max(numbers_count, 2)
229    numbers = [random.randint(0, max_num) 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$