mathgenerator.misc

  1import random
  2import math
  3import numpy as np
  4
  5
  6def arithmetic_progression_sum(max_d=100, max_a=100, max_n=100):
  7    """Arithmetic Progression Sum
  8
  9    | Ex. Problem | Ex. Solution |
 10    | --- | --- |
 11    | Find the sum of first $44$ terms of the AP series: $49, 145, 241 ... $ | $92972.0$ |
 12    """
 13    d = random.randint(-1 * max_d, max_d)
 14    a1 = random.randint(-1 * max_a, max_a)
 15    a2 = a1 + d
 16    a3 = a1 + 2 * d
 17    n = random.randint(4, max_n)
 18    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
 19    an = a1 + (n - 1) * d
 20    solution = n * (a1 + an) / 2
 21
 22    problem = f'Find the sum of first ${n}$ terms of the AP series: ${apString}$'
 23    return problem, f'${solution}$'
 24
 25
 26def arithmetic_progression_term(max_d=100, max_a=100, max_n=100):
 27    """Arithmetic Progression Term
 28
 29    | Ex. Problem | Ex. Solution |
 30    | --- | --- |
 31    | Find term number $12$ of the AP series: $-54, 24, 102 ... $ | $804$ |
 32    """
 33    d = random.randint(-1 * max_d, max_d)
 34    a1 = random.randint(-1 * max_a, max_a)
 35    a2 = a1 + d
 36    a3 = a2 + d
 37    n = random.randint(4, max_n)
 38    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
 39    solution = a1 + ((n - 1) * d)
 40
 41    problem = f'Find term number ${n}$ of the AP series: ${apString}$'
 42    return problem, f'${solution}$'
 43
 44
 45def _fromBaseTenTo(n, to_base):
 46    """Converts a decimal number n to another base, to_base.
 47    Utility of base_conversion()
 48    """
 49    alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 50    assert type(
 51        to_base
 52    ) == int and to_base >= 2 and to_base <= 36, "to_base({}) must be >=2 and <=36"
 53    # trivial cases
 54    if to_base == 2:
 55        return bin(n)[2:]
 56    elif to_base == 8:
 57        return oct(n)[2:]
 58    elif to_base == 10:
 59        return str(n)
 60    elif to_base == 16:
 61        return hex(n)[2:].upper()
 62    res = alpha[n % to_base]
 63    n = n // to_base
 64    while n > 0:
 65        res = alpha[n % to_base] + res
 66        n = n // to_base
 67    return res
 68
 69
 70def base_conversion(max_num=60000, max_base=16):
 71    """Base Conversion
 72
 73    | Ex. Problem | Ex. Solution |
 74    | --- | --- |
 75    | Convert $45204$ from base $10$ to base $4$ | $23002110$ |
 76    """
 77    assert type(
 78        max_num
 79    ) == int and max_num >= 100 and max_num <= 65536, "max_num({}) must be >=100 and <=65536".format(
 80        max_num)
 81    assert type(
 82        max_base
 83    ) == int and max_base >= 2 and max_base <= 36, "max_base({}) must be >= 2 and <=36".format(
 84        max_base)
 85
 86    n = random.randint(40, max_num)
 87    dist = [10] * 10 + [2] * 5 + [16] * 5 + [i for i in range(2, max_base + 1)]
 88    # set this way since converting to/from bases 2,10,16 are more common -- can be changed if needed.
 89    bases = random.choices(dist, k=2)
 90    while bases[0] == bases[1]:
 91        bases = random.choices(dist, k=2)
 92
 93    problem = f"Convert ${_fromBaseTenTo(n, bases[0])}$ from base ${bases[0]}$ to base ${bases[1]}$."
 94    ans = _fromBaseTenTo(n, bases[1])
 95    return problem, f'${ans}$'
 96
 97
 98def _newton_symbol(n, k):
 99    """Utility of binomial_distribution()"""
100    # math.factorial only works with integers starting python 3.9
101    # ref: https://docs.python.org/3/library/math.html#math.factorial
102    n, k = int(n), int(k)   
103    return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
104
105
106def binomial_distribution():
107    """Binomial distribution
108
109    | Ex. Problem | Ex. Solution |
110    | --- | --- |
111    | A manufacturer of metal pistons finds that, on average, $30.56$% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of $20$ pistons will contain no more than $2$ rejected pistons? | $3.17$ |
112    """
113    rejected_fraction = float(random.randint(30, 40)) + random.random()
114    batch = random.randint(10, 20)
115    rejections = random.randint(1, 9)
116
117    answer = 0
118    rejected_fraction = round(rejected_fraction, 2)
119    for i in range(0, rejections + 1):
120        answer += _newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \
121            ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i)))
122
123    answer = round(100 * answer, 2)
124
125    problem = "A manufacturer of metal pistons finds that, on average, ${0:}$% "\
126        "of the pistons they manufacture are rejected because " \
127        "they are incorrectly sized. What is the probability that a "\
128        "batch of ${1:}$ pistons will contain no more than ${2:}$ " \
129        "rejected pistons?".format(rejected_fraction, batch, rejections)
130    return problem, f'${answer}$'
131
132
133def celsius_to_fahrenheit(max_temp=100):
134    """Celsius to Fahrenheit
135
136    | Ex. Problem | Ex. Solution |
137    | --- | --- |
138    | Convert $-46$ degrees Celsius to degrees Fahrenheit | $-50.8$ |
139    """
140    celsius = random.randint(-50, max_temp)
141    fahrenheit = (celsius * (9 / 5)) + 32
142
143    problem = f"Convert ${celsius}$ degrees Celsius to degrees Fahrenheit"
144    solution = f'${fahrenheit}$'
145    return problem, solution
146
147
148def common_factors(max_val=100):
149    """Common Factors
150
151    | Ex. Problem | Ex. Solution |
152    | --- | --- |
153    | Common Factors of $100$ and $44 = $ | $[1, 2, 4]$ |
154    """
155    a = x = random.randint(1, max_val)
156    b = y = random.randint(1, max_val)
157
158    if (x < y):
159        min = x
160    else:
161        min = y
162
163    count = 0
164    arr = []
165
166    for i in range(1, min + 1):
167        if (x % i == 0):
168            if (y % i == 0):
169                count = count + 1
170                arr.append(i)
171
172    problem = f"Common Factors of ${a}$ and ${b} = $"
173    solution = f'${arr}$'
174    return problem, solution
175
176
177def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20):
178    r"""Complex to polar form
179
180    | Ex. Problem | Ex. Solution |
181    | --- | --- |
182    | $19.42(-19.0\theta + i-4.0\theta)$ | $-2.93$ |
183    """
184    num = complex(
185        random.randint(min_real_imaginary_num, max_real_imaginary_num),
186        random.randint(min_real_imaginary_num, max_real_imaginary_num))
187    a = num.real
188    b = num.imag
189    r = round(math.hypot(a, b), 2)
190    theta = round(math.atan2(b, a), 2)
191
192    problem = rf'${r}({a}\theta + i{b}\theta)$'
193    return problem, f'${theta}$'
194
195
196def decimal_to_roman_numerals(max_decimal=3999):
197    """Decimal to Roman Numerals
198
199    | Ex. Problem | Ex. Solution |
200    | --- | --- |
201    | The number $92$ in roman numerals is:  | $XCII$ |
202    """
203    assert 0 <= max_decimal <= 3999, f"max_decimal({max_decimal}) must be <= 3999"
204    x = random.randint(0, max_decimal)
205    x_copy = x
206    roman_dict = {
207        1: "I",
208        5: "V",
209        10: "X",
210        50: "L",
211        100: "C",
212        500: "D",
213        1000: "M"
214    }
215    div = 1
216    while x >= div:
217        div *= 10
218    div /= 10
219    solution = ""
220    while x:
221        last_value = int(x / div)
222        if last_value <= 3:
223            solution += (roman_dict[div] * last_value)
224        elif last_value == 4:
225            solution += (roman_dict[div] + roman_dict[div * 5])
226        elif 5 <= last_value <= 8:
227            solution += (roman_dict[div * 5] + (roman_dict[div] * (last_value - 5)))
228        elif last_value == 9:
229            solution += (roman_dict[div] + roman_dict[div * 10])
230        x = math.floor(x % div)
231        div /= 10
232
233    problem = f"The number ${x_copy}$ in roman numerals is: "
234    return problem, f'${solution}$'
235
236
237def euclidian_norm(maxEltAmt=20):
238    """Euclidian norm or L2 norm of a vector
239
240    | Ex. Problem | Ex. Solution |
241    | --- | --- |
242    | Euclidian norm or L2 norm of the vector $[659.9225071540442, 243.40887829281564, 128.79950053874424, 263.19226900031344]$ is: | $761.97$ |
243    """
244    vec = [
245        random.uniform(0, 1000) for i in range(random.randint(2, maxEltAmt))
246    ]
247    solution = round(math.sqrt(sum([i**2 for i in vec])), 2)
248
249    problem = f"Euclidian norm or L2 norm of the vector ${vec}$ is:"
250    return problem, f'${solution}$'
251
252
253def factors(max_val=1000):
254    """Factors of a number
255
256    | Ex. Problem | Ex. Solution |
257    | --- | --- |
258    | Factors of $176 = $ | $[1, 2, 4, 8, 11, 16, 22, 44, 88, 176]$ |
259    """
260    n = random.randint(1, max_val)
261
262    factors = []
263
264    for i in range(1, int(n**0.5) + 1):
265        if i**2 == n:
266            factors.append(i)
267        elif n % i == 0:
268            factors.append(i)
269            factors.append(n // i)
270        else:
271            pass
272
273    factors.sort()
274
275    problem = f"Factors of ${n} = $"
276    solution = factors
277    return problem, f'${solution}$'
278
279
280def geometric_mean(max_value=100, max_count=4):
281    """Geometric Mean of N Numbers
282
283    | Ex. Problem | Ex. Solution |
284    | --- | --- |
285    | Geometric mean of $3$ numbers $[72, 21, 87] = $ | $50.86$ |
286    """
287    count = random.randint(2, max_count)
288    nums = [random.randint(1, max_value) for i in range(count)]
289    product = np.prod(nums)
290
291    ans = round(product**(1 / count), 2)
292    problem = f"Geometric mean of ${count}$ numbers ${nums} = $"
293    # solution = rf"$({'*'.join(map(str, nums))}^{{\frac{{1}}{{{count}}}}} = {ans}$"
294    solution = f"${ans}$"
295    return problem, solution
296
297
298def geometric_progression(number_values=6,
299                          min_value=2,
300                          max_value=12,
301                          n_term=7,
302                          sum_term=5):
303    """Geometric Progression
304
305    | Ex. Problem | Ex. Solution |
306    | --- | --- |
307    | For the given GP $[11, 44, 176, 704, 2816, 11264]$. Find the value of a common ratio, 7th term value, sum upto 10th term | The value of a is $11$, common ratio is $4$ , 7th term is $45056$, sum upto 10th term is $3844775.0$ |
308    """
309    r = random.randint(min_value, max_value)
310    a = random.randint(min_value, max_value)
311    n_term = random.randint(number_values, number_values + 5)
312    sum_term = random.randint(number_values, number_values + 5)
313    GP = []
314    for i in range(number_values):
315        GP.append(a * (r**i))
316    value_nth_term = a * (r**(n_term - 1))
317    sum_till_nth_term = a * ((r**sum_term - 1) / (r - 1))
318
319    problem = f"For the given GP ${GP}$. Find the value of a common ratio, {n_term}th term value, sum upto {sum_term}th term"
320    solution = "The value of a is ${}$, common ratio is ${}$ , {}th term is ${}$, sum upto {}th term is ${}$".format(
321        a, r, n_term, value_nth_term, sum_term, sum_till_nth_term)
322    return problem, solution
323
324
325def harmonic_mean(max_value=100, max_count=4):
326    """Harmonic Mean of N Numbers
327
328    | Ex. Problem | Ex. Solution |
329    | --- | --- |
330    | Harmonic mean of $4$ numbers $52, 56, 25, 57 = $ | $602.33$ |
331    """
332    count = random.randint(2, max_count)
333    nums = [random.randint(1, max_value) for _ in range(count)]
334    sum = 0
335    for num in nums:
336        sum += (1 / num)
337    ans = round(num / sum, 2)
338
339    problem = f"Harmonic mean of ${count}$ numbers ${', '.join(map(str, nums))} = $"
340    solution = f"${ans}$"
341    return problem, solution
342
343
344def is_leap_year(minNumber=1900, max_number=2099):
345    """Is Leap Year or Not
346
347    | Ex. Problem | Ex. Solution |
348    | --- | --- |
349    | Is $2000$ a leap year? | $2000$ is a leap year |
350    """
351    year = random.randint(minNumber, max_number)
352    problem = f"Is {year} a leap year?"
353    if (year % 4) == 0:
354        if (year % 100) == 0:
355            if (year % 400) == 0:
356                ans = True
357            else:
358                ans = False
359        else:
360            ans = True
361    else:
362        ans = False
363
364    solution = f"{year} is{' not' if not ans else ''} a leap year"
365    return problem, solution
366
367
368def lcm(max_val=20):
369    """LCM (Least Common Multiple)
370
371    | Ex. Problem | Ex. Solution |
372    | --- | --- |
373    | LCM of $3$ and $18 = $ | $18$ |
374    """
375    a = random.randint(1, max_val)
376    b = random.randint(1, max_val)
377    c = a * b
378    x, y = a, b
379
380    while y:
381        x, y = y, x % y
382    d = c // x
383
384    problem = f"LCM of ${a}$ and ${b} =$"
385    solution = f'${d}$'
386    return problem, solution
387
388
389def minutes_to_hours(max_minutes=999):
390    """Convert minutes to hours and minutes
391
392    | Ex. Problem | Ex. Solution |
393    | --- | --- |
394    | Convert $836$ minutes to hours & minutes | $13$ hours and $56$ minutes |
395    """
396    minutes = random.randint(1, max_minutes)
397    ansHours = minutes // 60
398    ansMinutes = minutes % 60
399
400    problem = f"Convert ${minutes}$ minutes to hours & minutes"
401    solution = f"${ansHours}$ hours and ${ansMinutes}$ minutes"
402    return problem, solution
403
404
405def prime_factors(min_val=1, max_val=200):
406    """Prime Factors
407
408    | Ex. Problem | Ex. Solution |
409    | --- | --- |
410    | Find prime factors of $30$ | $2, 3, 5$ |
411    """
412    a = random.randint(min_val, max_val)
413    n = a
414    i = 2
415    factors = []
416
417    while i * i <= n:
418        if n % i:
419            i += 1
420        else:
421            n //= i
422            factors.append(i)
423
424    if n > 1:
425        factors.append(n)
426
427    problem = f"Find prime factors of ${a}$"
428    solution = f"${', '.join(map(str, factors))}$"
429    return problem, solution
430
431
432def product_of_scientific_notations(min_exp_val=-100, max_exp_val=100):
433    r"""Product of scientific notations
434
435    | Ex. Problem | Ex. Solution |
436    | --- | --- |
437    | Product of scientific notations $5.11 \times 10^{67}$ and $3.64 \times 10^{-59} = $ | $1.86 \times 10^{9}$ |
438    """
439    a = [
440        round(random.uniform(1, 10), 2),
441        random.randint(min_exp_val, max_exp_val)
442    ]
443    b = [
444        round(random.uniform(1, 10), 2),
445        random.randint(min_exp_val, max_exp_val)
446    ]
447    c = [a[0] * b[0], a[1] + b[1]]
448
449    if c[0] >= 10:
450        c[0] /= 10
451        c[1] += 1
452
453    problem = rf"Product of scientific notations ${a[0]} \times 10^{{{a[1]}}}$ and ${b[0]} \times 10^{{{b[1]}}} = $"
454    solution = rf'${round(c[0], 2)} \times 10^{{{c[1]}}}$'
455    return problem, solution
456
457
458def profit_loss_percent(max_cp=1000, max_sp=1000):
459    """Profit or Loss Percent
460
461    | Ex. Problem | Ex. Solution |
462    | --- | --- |
463    | Loss percent when $CP = 751$ and $SP = 290$ is: | $61.38$ |
464    """
465    cP = random.randint(1, max_cp)
466    sP = random.randint(1, max_sp)
467    diff = abs(sP - cP)
468    if (sP - cP >= 0):
469        profitOrLoss = "Profit"
470    else:
471        profitOrLoss = "Loss"
472    percent = round(diff / cP * 100, 2)
473
474    problem = f"{profitOrLoss} percent when $CP = {cP}$ and $SP = {sP}$ is: "
475    return problem, f'${percent}$'
476
477
478def quotient_of_power_same_base(max_base=50, max_power=10):
479    """Quotient of Powers with Same Base
480
481    | Ex. Problem | Ex. Solution |
482    | --- | --- |
483    | The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ | $0.04$ |
484    """
485    base = random.randint(1, max_base)
486    power1 = random.randint(1, max_power)
487    power2 = random.randint(1, max_power)
488    step = power1 - power2
489    solution = base**step
490
491    problem = f"The Quotient of ${base}^{{{power1}}}$ and ${base}^{{{power2}}} = " \
492        f"{base}^{{{power1}-{power2}}} = {base}^{{{step}}}$"
493    return problem, f'${solution}$'
494
495
496def quotient_of_power_same_power(max_base=50, max_power=10):
497    """Quotient of Powers with Same Power
498
499    | Ex. Problem | Ex. Solution |
500    | --- | --- |
501    | The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ | $169.84$ |
502    """
503    base1 = random.randint(1, max_base)
504    base2 = random.randint(1, max_base)
505    power = random.randint(1, max_power)
506    step = base1 / base2
507    solution = round(step**power, 2)
508
509    problem = f"The quotient of ${base1}^{{{power}}}$ and ${base2}^{{{power}}} = " \
510        f"({base1}/{base2})^{power} = {step}^{{{power}}}$"
511    return problem, f'${solution}$'
512
513
514def set_operation(min_size=3, max_size=7):
515    """Union, Intersection, Difference of Two Sets
516
517    | Ex. Problem | Ex. Solution |
518    | --- | --- |
519    | Given the two sets $a={1, 2, 4, 5}$, $b={8, 1, 2}$. Find the Union, intersection, $a-b$, $b-a$, and symmetric difference | Union is ${1, 2, 4, 5, 8}$. Intersection is ${1, 2}$, $a-b$ is ${4, 5}$, $b-a$ is ${8}$. Symmetric difference is ${4, 5, 8}$. |
520    """
521    number_variables_a = random.randint(min_size, max_size)
522    number_variables_b = random.randint(min_size, max_size)
523    a = []
524    b = []
525    for _ in range(number_variables_a):
526        a.append(random.randint(1, 10))
527    for _ in range(number_variables_b):
528        b.append(random.randint(1, 10))
529    a = set(a)
530    b = set(b)
531
532    problem = f"Given the two sets $a={a}$, $b={b}$. " + \
533        "Find the Union, intersection, $a-b$, $b-a$, and symmetric difference"
534    solution = f"Union is ${a.union(b)}$. Intersection is ${a.intersection(b)}$" + \
535        f", $a-b$ is ${a.difference(b)}$, $b-a$ is ${b.difference(a)}$." + \
536        f" Symmetric difference is ${a.symmetric_difference(b)}$."
537    return problem, solution
538
539
540def signum_function(min=-999, max=999):
541    """Signum Function
542
543    | Ex. Problem | Ex. Solution |
544    | --- | --- |
545    | Signum of $-229$ is = | $-1$ |
546    """
547    a = random.randint(min, max)
548    b = 0
549    if (a > 0):
550        b = 1
551    if (a < 0):
552        b = -1
553
554    problem = f"Signum of ${a}$ is ="
555    solution = f'${b}$'
556    return problem, solution
557
558
559def surds_comparison(max_value=100, max_root=10):
560    r"""Comparing Surds
561
562    | Ex. Problem | Ex. Solution |
563    | --- | --- |
564    | Fill in the blanks $42^{\frac{1}{2}}$ _ $45^{\frac{1}{5}}$ | $>$ |
565    """
566    radicand1, radicand2 = tuple(random.sample(range(1, max_value), 2))
567    degree1, degree2 = tuple(random.sample(range(1, max_root), 2))
568    first = math.pow(radicand1, 1 / degree1)
569    second = math.pow(radicand2, 1 / degree2)
570
571    solution = "="
572    if first > second:
573        solution = ">"
574    elif first < second:
575        solution = "<"
576
577    problem = rf"Fill in the blanks ${radicand1}^{{\frac{{1}}{{{degree1}}}}}$ _ ${radicand2}^{{\frac{{1}}{{{degree2}}}}}$"
578    return problem, f'${solution}$'
579
580
581def velocity_of_object(max_displacement=1000, max_time=100):
582    """Velocity of object
583
584    | Ex. Problem | Ex. Solution |
585    | --- | --- |
586    | An object travels at uniform velocity a distance of $100 m$ in $4$ seconds. What is the velocity of the car? | $25 m/s$ |
587    """
588
589    displacement = random.randint(1, max_displacement)
590    time_taken = random.randint(1, max_time)
591    velocity = "${} m/s$".format(round(displacement / time_taken, 2))
592
593    problem = f"An object travels at uniform velocity a distance of ${displacement} m$ in ${time_taken}$ seconds. What is the velocity of the car? "
594    return problem, velocity
def arithmetic_progression_sum(max_d=100, max_a=100, max_n=100):
 7def arithmetic_progression_sum(max_d=100, max_a=100, max_n=100):
 8    """Arithmetic Progression Sum
 9
10    | Ex. Problem | Ex. Solution |
11    | --- | --- |
12    | Find the sum of first $44$ terms of the AP series: $49, 145, 241 ... $ | $92972.0$ |
13    """
14    d = random.randint(-1 * max_d, max_d)
15    a1 = random.randint(-1 * max_a, max_a)
16    a2 = a1 + d
17    a3 = a1 + 2 * d
18    n = random.randint(4, max_n)
19    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
20    an = a1 + (n - 1) * d
21    solution = n * (a1 + an) / 2
22
23    problem = f'Find the sum of first ${n}$ terms of the AP series: ${apString}$'
24    return problem, f'${solution}$'

Arithmetic Progression Sum

Ex. Problem Ex. Solution
Find the sum of first $44$ terms of the AP series: $49, 145, 241 ... $ $92972.0$
def arithmetic_progression_term(max_d=100, max_a=100, max_n=100):
27def arithmetic_progression_term(max_d=100, max_a=100, max_n=100):
28    """Arithmetic Progression Term
29
30    | Ex. Problem | Ex. Solution |
31    | --- | --- |
32    | Find term number $12$ of the AP series: $-54, 24, 102 ... $ | $804$ |
33    """
34    d = random.randint(-1 * max_d, max_d)
35    a1 = random.randint(-1 * max_a, max_a)
36    a2 = a1 + d
37    a3 = a2 + d
38    n = random.randint(4, max_n)
39    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
40    solution = a1 + ((n - 1) * d)
41
42    problem = f'Find term number ${n}$ of the AP series: ${apString}$'
43    return problem, f'${solution}$'

Arithmetic Progression Term

Ex. Problem Ex. Solution
Find term number $12$ of the AP series: $-54, 24, 102 ... $ $804$
def base_conversion(max_num=60000, max_base=16):
71def base_conversion(max_num=60000, max_base=16):
72    """Base Conversion
73
74    | Ex. Problem | Ex. Solution |
75    | --- | --- |
76    | Convert $45204$ from base $10$ to base $4$ | $23002110$ |
77    """
78    assert type(
79        max_num
80    ) == int and max_num >= 100 and max_num <= 65536, "max_num({}) must be >=100 and <=65536".format(
81        max_num)
82    assert type(
83        max_base
84    ) == int and max_base >= 2 and max_base <= 36, "max_base({}) must be >= 2 and <=36".format(
85        max_base)
86
87    n = random.randint(40, max_num)
88    dist = [10] * 10 + [2] * 5 + [16] * 5 + [i for i in range(2, max_base + 1)]
89    # set this way since converting to/from bases 2,10,16 are more common -- can be changed if needed.
90    bases = random.choices(dist, k=2)
91    while bases[0] == bases[1]:
92        bases = random.choices(dist, k=2)
93
94    problem = f"Convert ${_fromBaseTenTo(n, bases[0])}$ from base ${bases[0]}$ to base ${bases[1]}$."
95    ans = _fromBaseTenTo(n, bases[1])
96    return problem, f'${ans}$'

Base Conversion

Ex. Problem Ex. Solution
Convert $45204$ from base $10$ to base $4$ $23002110$
def binomial_distribution():
107def binomial_distribution():
108    """Binomial distribution
109
110    | Ex. Problem | Ex. Solution |
111    | --- | --- |
112    | A manufacturer of metal pistons finds that, on average, $30.56$% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of $20$ pistons will contain no more than $2$ rejected pistons? | $3.17$ |
113    """
114    rejected_fraction = float(random.randint(30, 40)) + random.random()
115    batch = random.randint(10, 20)
116    rejections = random.randint(1, 9)
117
118    answer = 0
119    rejected_fraction = round(rejected_fraction, 2)
120    for i in range(0, rejections + 1):
121        answer += _newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \
122            ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i)))
123
124    answer = round(100 * answer, 2)
125
126    problem = "A manufacturer of metal pistons finds that, on average, ${0:}$% "\
127        "of the pistons they manufacture are rejected because " \
128        "they are incorrectly sized. What is the probability that a "\
129        "batch of ${1:}$ pistons will contain no more than ${2:}$ " \
130        "rejected pistons?".format(rejected_fraction, batch, rejections)
131    return problem, f'${answer}$'

Binomial distribution

Ex. Problem Ex. Solution
A manufacturer of metal pistons finds that, on average, $30.56$% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of $20$ pistons will contain no more than $2$ rejected pistons? $3.17$
def celsius_to_fahrenheit(max_temp=100):
134def celsius_to_fahrenheit(max_temp=100):
135    """Celsius to Fahrenheit
136
137    | Ex. Problem | Ex. Solution |
138    | --- | --- |
139    | Convert $-46$ degrees Celsius to degrees Fahrenheit | $-50.8$ |
140    """
141    celsius = random.randint(-50, max_temp)
142    fahrenheit = (celsius * (9 / 5)) + 32
143
144    problem = f"Convert ${celsius}$ degrees Celsius to degrees Fahrenheit"
145    solution = f'${fahrenheit}$'
146    return problem, solution

Celsius to Fahrenheit

Ex. Problem Ex. Solution
Convert $-46$ degrees Celsius to degrees Fahrenheit $-50.8$
def common_factors(max_val=100):
149def common_factors(max_val=100):
150    """Common Factors
151
152    | Ex. Problem | Ex. Solution |
153    | --- | --- |
154    | Common Factors of $100$ and $44 = $ | $[1, 2, 4]$ |
155    """
156    a = x = random.randint(1, max_val)
157    b = y = random.randint(1, max_val)
158
159    if (x < y):
160        min = x
161    else:
162        min = y
163
164    count = 0
165    arr = []
166
167    for i in range(1, min + 1):
168        if (x % i == 0):
169            if (y % i == 0):
170                count = count + 1
171                arr.append(i)
172
173    problem = f"Common Factors of ${a}$ and ${b} = $"
174    solution = f'${arr}$'
175    return problem, solution

Common Factors

Ex. Problem Ex. Solution
Common Factors of $100$ and $44 = $ $[1, 2, 4]$
def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20):
178def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20):
179    r"""Complex to polar form
180
181    | Ex. Problem | Ex. Solution |
182    | --- | --- |
183    | $19.42(-19.0\theta + i-4.0\theta)$ | $-2.93$ |
184    """
185    num = complex(
186        random.randint(min_real_imaginary_num, max_real_imaginary_num),
187        random.randint(min_real_imaginary_num, max_real_imaginary_num))
188    a = num.real
189    b = num.imag
190    r = round(math.hypot(a, b), 2)
191    theta = round(math.atan2(b, a), 2)
192
193    problem = rf'${r}({a}\theta + i{b}\theta)$'
194    return problem, f'${theta}$'

Complex to polar form

Ex. Problem Ex. Solution
$19.42(-19.0\theta + i-4.0\theta)$ $-2.93$
def decimal_to_roman_numerals(max_decimal=3999):
197def decimal_to_roman_numerals(max_decimal=3999):
198    """Decimal to Roman Numerals
199
200    | Ex. Problem | Ex. Solution |
201    | --- | --- |
202    | The number $92$ in roman numerals is:  | $XCII$ |
203    """
204    assert 0 <= max_decimal <= 3999, f"max_decimal({max_decimal}) must be <= 3999"
205    x = random.randint(0, max_decimal)
206    x_copy = x
207    roman_dict = {
208        1: "I",
209        5: "V",
210        10: "X",
211        50: "L",
212        100: "C",
213        500: "D",
214        1000: "M"
215    }
216    div = 1
217    while x >= div:
218        div *= 10
219    div /= 10
220    solution = ""
221    while x:
222        last_value = int(x / div)
223        if last_value <= 3:
224            solution += (roman_dict[div] * last_value)
225        elif last_value == 4:
226            solution += (roman_dict[div] + roman_dict[div * 5])
227        elif 5 <= last_value <= 8:
228            solution += (roman_dict[div * 5] + (roman_dict[div] * (last_value - 5)))
229        elif last_value == 9:
230            solution += (roman_dict[div] + roman_dict[div * 10])
231        x = math.floor(x % div)
232        div /= 10
233
234    problem = f"The number ${x_copy}$ in roman numerals is: "
235    return problem, f'${solution}$'

Decimal to Roman Numerals

Ex. Problem Ex. Solution
The number $92$ in roman numerals is: $XCII$
def euclidian_norm(maxEltAmt=20):
238def euclidian_norm(maxEltAmt=20):
239    """Euclidian norm or L2 norm of a vector
240
241    | Ex. Problem | Ex. Solution |
242    | --- | --- |
243    | Euclidian norm or L2 norm of the vector $[659.9225071540442, 243.40887829281564, 128.79950053874424, 263.19226900031344]$ is: | $761.97$ |
244    """
245    vec = [
246        random.uniform(0, 1000) for i in range(random.randint(2, maxEltAmt))
247    ]
248    solution = round(math.sqrt(sum([i**2 for i in vec])), 2)
249
250    problem = f"Euclidian norm or L2 norm of the vector ${vec}$ is:"
251    return problem, f'${solution}$'

Euclidian norm or L2 norm of a vector

Ex. Problem Ex. Solution
Euclidian norm or L2 norm of the vector $[659.9225071540442, 243.40887829281564, 128.79950053874424, 263.19226900031344]$ is: $761.97$
def factors(max_val=1000):
254def factors(max_val=1000):
255    """Factors of a number
256
257    | Ex. Problem | Ex. Solution |
258    | --- | --- |
259    | Factors of $176 = $ | $[1, 2, 4, 8, 11, 16, 22, 44, 88, 176]$ |
260    """
261    n = random.randint(1, max_val)
262
263    factors = []
264
265    for i in range(1, int(n**0.5) + 1):
266        if i**2 == n:
267            factors.append(i)
268        elif n % i == 0:
269            factors.append(i)
270            factors.append(n // i)
271        else:
272            pass
273
274    factors.sort()
275
276    problem = f"Factors of ${n} = $"
277    solution = factors
278    return problem, f'${solution}$'

Factors of a number

Ex. Problem Ex. Solution
Factors of $176 = $ $[1, 2, 4, 8, 11, 16, 22, 44, 88, 176]$
def geometric_mean(max_value=100, max_count=4):
281def geometric_mean(max_value=100, max_count=4):
282    """Geometric Mean of N Numbers
283
284    | Ex. Problem | Ex. Solution |
285    | --- | --- |
286    | Geometric mean of $3$ numbers $[72, 21, 87] = $ | $50.86$ |
287    """
288    count = random.randint(2, max_count)
289    nums = [random.randint(1, max_value) for i in range(count)]
290    product = np.prod(nums)
291
292    ans = round(product**(1 / count), 2)
293    problem = f"Geometric mean of ${count}$ numbers ${nums} = $"
294    # solution = rf"$({'*'.join(map(str, nums))}^{{\frac{{1}}{{{count}}}}} = {ans}$"
295    solution = f"${ans}$"
296    return problem, solution

Geometric Mean of N Numbers

Ex. Problem Ex. Solution
Geometric mean of $3$ numbers $[72, 21, 87] = $ $50.86$
def geometric_progression(number_values=6, min_value=2, max_value=12, n_term=7, sum_term=5):
299def geometric_progression(number_values=6,
300                          min_value=2,
301                          max_value=12,
302                          n_term=7,
303                          sum_term=5):
304    """Geometric Progression
305
306    | Ex. Problem | Ex. Solution |
307    | --- | --- |
308    | For the given GP $[11, 44, 176, 704, 2816, 11264]$. Find the value of a common ratio, 7th term value, sum upto 10th term | The value of a is $11$, common ratio is $4$ , 7th term is $45056$, sum upto 10th term is $3844775.0$ |
309    """
310    r = random.randint(min_value, max_value)
311    a = random.randint(min_value, max_value)
312    n_term = random.randint(number_values, number_values + 5)
313    sum_term = random.randint(number_values, number_values + 5)
314    GP = []
315    for i in range(number_values):
316        GP.append(a * (r**i))
317    value_nth_term = a * (r**(n_term - 1))
318    sum_till_nth_term = a * ((r**sum_term - 1) / (r - 1))
319
320    problem = f"For the given GP ${GP}$. Find the value of a common ratio, {n_term}th term value, sum upto {sum_term}th term"
321    solution = "The value of a is ${}$, common ratio is ${}$ , {}th term is ${}$, sum upto {}th term is ${}$".format(
322        a, r, n_term, value_nth_term, sum_term, sum_till_nth_term)
323    return problem, solution

Geometric Progression

Ex. Problem Ex. Solution
For the given GP $[11, 44, 176, 704, 2816, 11264]$. Find the value of a common ratio, 7th term value, sum upto 10th term The value of a is $11$, common ratio is $4$ , 7th term is $45056$, sum upto 10th term is $3844775.0$
def harmonic_mean(max_value=100, max_count=4):
326def harmonic_mean(max_value=100, max_count=4):
327    """Harmonic Mean of N Numbers
328
329    | Ex. Problem | Ex. Solution |
330    | --- | --- |
331    | Harmonic mean of $4$ numbers $52, 56, 25, 57 = $ | $602.33$ |
332    """
333    count = random.randint(2, max_count)
334    nums = [random.randint(1, max_value) for _ in range(count)]
335    sum = 0
336    for num in nums:
337        sum += (1 / num)
338    ans = round(num / sum, 2)
339
340    problem = f"Harmonic mean of ${count}$ numbers ${', '.join(map(str, nums))} = $"
341    solution = f"${ans}$"
342    return problem, solution

Harmonic Mean of N Numbers

Ex. Problem Ex. Solution
Harmonic mean of $4$ numbers $52, 56, 25, 57 = $ $602.33$
def is_leap_year(minNumber=1900, max_number=2099):
345def is_leap_year(minNumber=1900, max_number=2099):
346    """Is Leap Year or Not
347
348    | Ex. Problem | Ex. Solution |
349    | --- | --- |
350    | Is $2000$ a leap year? | $2000$ is a leap year |
351    """
352    year = random.randint(minNumber, max_number)
353    problem = f"Is {year} a leap year?"
354    if (year % 4) == 0:
355        if (year % 100) == 0:
356            if (year % 400) == 0:
357                ans = True
358            else:
359                ans = False
360        else:
361            ans = True
362    else:
363        ans = False
364
365    solution = f"{year} is{' not' if not ans else ''} a leap year"
366    return problem, solution

Is Leap Year or Not

Ex. Problem Ex. Solution
Is $2000$ a leap year? $2000$ is a leap year
def lcm(max_val=20):
369def lcm(max_val=20):
370    """LCM (Least Common Multiple)
371
372    | Ex. Problem | Ex. Solution |
373    | --- | --- |
374    | LCM of $3$ and $18 = $ | $18$ |
375    """
376    a = random.randint(1, max_val)
377    b = random.randint(1, max_val)
378    c = a * b
379    x, y = a, b
380
381    while y:
382        x, y = y, x % y
383    d = c // x
384
385    problem = f"LCM of ${a}$ and ${b} =$"
386    solution = f'${d}$'
387    return problem, solution

LCM (Least Common Multiple)

Ex. Problem Ex. Solution
LCM of $3$ and $18 = $ $18$
def minutes_to_hours(max_minutes=999):
390def minutes_to_hours(max_minutes=999):
391    """Convert minutes to hours and minutes
392
393    | Ex. Problem | Ex. Solution |
394    | --- | --- |
395    | Convert $836$ minutes to hours & minutes | $13$ hours and $56$ minutes |
396    """
397    minutes = random.randint(1, max_minutes)
398    ansHours = minutes // 60
399    ansMinutes = minutes % 60
400
401    problem = f"Convert ${minutes}$ minutes to hours & minutes"
402    solution = f"${ansHours}$ hours and ${ansMinutes}$ minutes"
403    return problem, solution

Convert minutes to hours and minutes

Ex. Problem Ex. Solution
Convert $836$ minutes to hours & minutes $13$ hours and $56$ minutes
def prime_factors(min_val=1, max_val=200):
406def prime_factors(min_val=1, max_val=200):
407    """Prime Factors
408
409    | Ex. Problem | Ex. Solution |
410    | --- | --- |
411    | Find prime factors of $30$ | $2, 3, 5$ |
412    """
413    a = random.randint(min_val, max_val)
414    n = a
415    i = 2
416    factors = []
417
418    while i * i <= n:
419        if n % i:
420            i += 1
421        else:
422            n //= i
423            factors.append(i)
424
425    if n > 1:
426        factors.append(n)
427
428    problem = f"Find prime factors of ${a}$"
429    solution = f"${', '.join(map(str, factors))}$"
430    return problem, solution

Prime Factors

Ex. Problem Ex. Solution
Find prime factors of $30$ $2, 3, 5$
def product_of_scientific_notations(min_exp_val=-100, max_exp_val=100):
433def product_of_scientific_notations(min_exp_val=-100, max_exp_val=100):
434    r"""Product of scientific notations
435
436    | Ex. Problem | Ex. Solution |
437    | --- | --- |
438    | Product of scientific notations $5.11 \times 10^{67}$ and $3.64 \times 10^{-59} = $ | $1.86 \times 10^{9}$ |
439    """
440    a = [
441        round(random.uniform(1, 10), 2),
442        random.randint(min_exp_val, max_exp_val)
443    ]
444    b = [
445        round(random.uniform(1, 10), 2),
446        random.randint(min_exp_val, max_exp_val)
447    ]
448    c = [a[0] * b[0], a[1] + b[1]]
449
450    if c[0] >= 10:
451        c[0] /= 10
452        c[1] += 1
453
454    problem = rf"Product of scientific notations ${a[0]} \times 10^{{{a[1]}}}$ and ${b[0]} \times 10^{{{b[1]}}} = $"
455    solution = rf'${round(c[0], 2)} \times 10^{{{c[1]}}}$'
456    return problem, solution

Product of scientific notations

Ex. Problem Ex. Solution
Product of scientific notations $5.11 \times 10^{67}$ and $3.64 \times 10^{-59} = $ $1.86 \times 10^{9}$
def profit_loss_percent(max_cp=1000, max_sp=1000):
459def profit_loss_percent(max_cp=1000, max_sp=1000):
460    """Profit or Loss Percent
461
462    | Ex. Problem | Ex. Solution |
463    | --- | --- |
464    | Loss percent when $CP = 751$ and $SP = 290$ is: | $61.38$ |
465    """
466    cP = random.randint(1, max_cp)
467    sP = random.randint(1, max_sp)
468    diff = abs(sP - cP)
469    if (sP - cP >= 0):
470        profitOrLoss = "Profit"
471    else:
472        profitOrLoss = "Loss"
473    percent = round(diff / cP * 100, 2)
474
475    problem = f"{profitOrLoss} percent when $CP = {cP}$ and $SP = {sP}$ is: "
476    return problem, f'${percent}$'

Profit or Loss Percent

Ex. Problem Ex. Solution
Loss percent when $CP = 751$ and $SP = 290$ is: $61.38$
def quotient_of_power_same_base(max_base=50, max_power=10):
479def quotient_of_power_same_base(max_base=50, max_power=10):
480    """Quotient of Powers with Same Base
481
482    | Ex. Problem | Ex. Solution |
483    | --- | --- |
484    | The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ | $0.04$ |
485    """
486    base = random.randint(1, max_base)
487    power1 = random.randint(1, max_power)
488    power2 = random.randint(1, max_power)
489    step = power1 - power2
490    solution = base**step
491
492    problem = f"The Quotient of ${base}^{{{power1}}}$ and ${base}^{{{power2}}} = " \
493        f"{base}^{{{power1}-{power2}}} = {base}^{{{step}}}$"
494    return problem, f'${solution}$'

Quotient of Powers with Same Base

Ex. Problem Ex. Solution
The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ $0.04$
def quotient_of_power_same_power(max_base=50, max_power=10):
497def quotient_of_power_same_power(max_base=50, max_power=10):
498    """Quotient of Powers with Same Power
499
500    | Ex. Problem | Ex. Solution |
501    | --- | --- |
502    | The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ | $169.84$ |
503    """
504    base1 = random.randint(1, max_base)
505    base2 = random.randint(1, max_base)
506    power = random.randint(1, max_power)
507    step = base1 / base2
508    solution = round(step**power, 2)
509
510    problem = f"The quotient of ${base1}^{{{power}}}$ and ${base2}^{{{power}}} = " \
511        f"({base1}/{base2})^{power} = {step}^{{{power}}}$"
512    return problem, f'${solution}$'

Quotient of Powers with Same Power

Ex. Problem Ex. Solution
The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ $169.84$
def set_operation(min_size=3, max_size=7):
515def set_operation(min_size=3, max_size=7):
516    """Union, Intersection, Difference of Two Sets
517
518    | Ex. Problem | Ex. Solution |
519    | --- | --- |
520    | Given the two sets $a={1, 2, 4, 5}$, $b={8, 1, 2}$. Find the Union, intersection, $a-b$, $b-a$, and symmetric difference | Union is ${1, 2, 4, 5, 8}$. Intersection is ${1, 2}$, $a-b$ is ${4, 5}$, $b-a$ is ${8}$. Symmetric difference is ${4, 5, 8}$. |
521    """
522    number_variables_a = random.randint(min_size, max_size)
523    number_variables_b = random.randint(min_size, max_size)
524    a = []
525    b = []
526    for _ in range(number_variables_a):
527        a.append(random.randint(1, 10))
528    for _ in range(number_variables_b):
529        b.append(random.randint(1, 10))
530    a = set(a)
531    b = set(b)
532
533    problem = f"Given the two sets $a={a}$, $b={b}$. " + \
534        "Find the Union, intersection, $a-b$, $b-a$, and symmetric difference"
535    solution = f"Union is ${a.union(b)}$. Intersection is ${a.intersection(b)}$" + \
536        f", $a-b$ is ${a.difference(b)}$, $b-a$ is ${b.difference(a)}$." + \
537        f" Symmetric difference is ${a.symmetric_difference(b)}$."
538    return problem, solution

Union, Intersection, Difference of Two Sets

Ex. Problem Ex. Solution
Given the two sets $a={1, 2, 4, 5}$, $b={8, 1, 2}$. Find the Union, intersection, $a-b$, $b-a$, and symmetric difference Union is ${1, 2, 4, 5, 8}$. Intersection is ${1, 2}$, $a-b$ is ${4, 5}$, $b-a$ is ${8}$. Symmetric difference is ${4, 5, 8}$.
def signum_function(min=-999, max=999):
541def signum_function(min=-999, max=999):
542    """Signum Function
543
544    | Ex. Problem | Ex. Solution |
545    | --- | --- |
546    | Signum of $-229$ is = | $-1$ |
547    """
548    a = random.randint(min, max)
549    b = 0
550    if (a > 0):
551        b = 1
552    if (a < 0):
553        b = -1
554
555    problem = f"Signum of ${a}$ is ="
556    solution = f'${b}$'
557    return problem, solution

Signum Function

Ex. Problem Ex. Solution
Signum of $-229$ is = $-1$
def surds_comparison(max_value=100, max_root=10):
560def surds_comparison(max_value=100, max_root=10):
561    r"""Comparing Surds
562
563    | Ex. Problem | Ex. Solution |
564    | --- | --- |
565    | Fill in the blanks $42^{\frac{1}{2}}$ _ $45^{\frac{1}{5}}$ | $>$ |
566    """
567    radicand1, radicand2 = tuple(random.sample(range(1, max_value), 2))
568    degree1, degree2 = tuple(random.sample(range(1, max_root), 2))
569    first = math.pow(radicand1, 1 / degree1)
570    second = math.pow(radicand2, 1 / degree2)
571
572    solution = "="
573    if first > second:
574        solution = ">"
575    elif first < second:
576        solution = "<"
577
578    problem = rf"Fill in the blanks ${radicand1}^{{\frac{{1}}{{{degree1}}}}}$ _ ${radicand2}^{{\frac{{1}}{{{degree2}}}}}$"
579    return problem, f'${solution}$'

Comparing Surds

Ex. Problem Ex. Solution
Fill in the blanks $42^{\frac{1}{2}}$ _ $45^{\frac{1}{5}}$ $>$
def velocity_of_object(max_displacement=1000, max_time=100):
582def velocity_of_object(max_displacement=1000, max_time=100):
583    """Velocity of object
584
585    | Ex. Problem | Ex. Solution |
586    | --- | --- |
587    | An object travels at uniform velocity a distance of $100 m$ in $4$ seconds. What is the velocity of the car? | $25 m/s$ |
588    """
589
590    displacement = random.randint(1, max_displacement)
591    time_taken = random.randint(1, max_time)
592    velocity = "${} m/s$".format(round(displacement / time_taken, 2))
593
594    problem = f"An object travels at uniform velocity a distance of ${displacement} m$ in ${time_taken}$ seconds. What is the velocity of the car? "
595    return problem, velocity

Velocity of object

Ex. Problem Ex. Solution
An object travels at uniform velocity a distance of $100 m$ in $4$ seconds. What is the velocity of the car? $25 m/s$