mathgenerator.misc

  1import random
  2import math
  3import numpy as np
  4
  5
  6def arithmetic_progression_sum(max_d=100,
  7                               max_a=100,
  8                               max_n=100):
  9    """Arithmetic Progression Sum
 10
 11    | Ex. Problem | Ex. Solution |
 12    | --- | --- |
 13    | Find the sum of first $44$ terms of the AP series: $49, 145, 241 ... $ | $92972.0$ |
 14    """
 15    d = random.randint(-1 * max_d, max_d)
 16    a1 = random.randint(-1 * max_a, max_a)
 17    a2 = a1 + d
 18    a3 = a1 + 2 * d
 19    n = random.randint(4, max_n)
 20    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
 21    an = a1 + (n - 1) * d
 22    solution = n * (a1 + an) / 2
 23
 24    problem = f'Find the sum of first ${n}$ terms of the AP series: ${apString}$'
 25    return problem, f'${solution}$'
 26
 27
 28def arithmetic_progression_term(max_d=100,
 29                                max_a=100,
 30                                max_n=100):
 31    """Arithmetic Progression Term
 32
 33    | Ex. Problem | Ex. Solution |
 34    | --- | --- |
 35    | Find term number $12$ of the AP series: $-54, 24, 102 ... $ | $804$ |
 36    """
 37    d = random.randint(-1 * max_d, max_d)
 38    a1 = random.randint(-1 * max_a, max_a)
 39    a2 = a1 + d
 40    a3 = a2 + d
 41    n = random.randint(4, max_n)
 42    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
 43    solution = a1 + ((n - 1) * d)
 44
 45    problem = f'Find term number ${n}$ of the AP series: ${apString}$'
 46    return problem, f'${solution}$'
 47
 48
 49def _fromBaseTenTo(n, to_base):
 50    """Converts a decimal number n to another base, to_base.
 51    Utility of base_conversion()
 52    """
 53    alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 54    assert type(
 55        to_base
 56    ) == int and to_base >= 2 and to_base <= 36, "to_base({}) must be >=2 and <=36"
 57    # trivial cases
 58    if to_base == 2:
 59        return bin(n)[2:]
 60    elif to_base == 8:
 61        return oct(n)[2:]
 62    elif to_base == 10:
 63        return str(n)
 64    elif to_base == 16:
 65        return hex(n)[2:].upper()
 66    res = alpha[n % to_base]
 67    n = n // to_base
 68    while n > 0:
 69        res = alpha[n % to_base] + res
 70        n = n // to_base
 71    return res
 72
 73
 74def base_conversion(max_num=60000, max_base=16):
 75    """Base Conversion
 76
 77    | Ex. Problem | Ex. Solution |
 78    | --- | --- |
 79    | Convert $45204$ from base $10$ to base $4$ | $23002110$ |
 80    """
 81    assert type(
 82        max_num
 83    ) == int and max_num >= 100 and max_num <= 65536, "max_num({}) must be >=100 and <=65536".format(
 84        max_num)
 85    assert type(
 86        max_base
 87    ) == int and max_base >= 2 and max_base <= 36, "max_base({}) must be >= 2 and <=36".format(
 88        max_base)
 89
 90    n = random.randint(40, max_num)
 91    dist = [10] * 10 + [2] * 5 + [16] * 5 + [i for i in range(2, max_base + 1)]
 92    # set this way since converting to/from bases 2,10,16 are more common -- can be changed if needed.
 93    bases = random.choices(dist, k=2)
 94    while bases[0] == bases[1]:
 95        bases = random.choices(dist, k=2)
 96
 97    problem = f"Convert ${_fromBaseTenTo(n, bases[0])}$ from base ${bases[0]}$ to base ${bases[1]}$."
 98    ans = _fromBaseTenTo(n, bases[1])
 99    return problem, f'${ans}$'
100
101
102def _newton_symbol(n, k):
103    """Utility of binomial_distribution()"""
104    return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
105
106
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}$'
132
133
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
147
148
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
176
177
178def complex_to_polar(min_real_imaginary_num=-20,
179                     max_real_imaginary_num=20):
180    r"""Complex to polar form
181
182    | Ex. Problem | Ex. Solution |
183    | --- | --- |
184    | $19.42(-19.0\theta + i-4.0\theta)$ | $-2.93$ |
185    """
186    num = complex(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}$'
195
196
197def decimal_to_roman_numerals(max_decimal=4000):
198    """Decimal to Roman Numerals
199
200    | Ex. Problem | Ex. Solution |
201    | --- | --- |
202    | The number $92$ in roman numerals is:  | $XCII$ |
203    """
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 = [round(random.uniform(1, 10), 2),
440         random.randint(min_exp_val, max_exp_val)]
441    b = [round(random.uniform(1, 10), 2),
442         random.randint(min_exp_val, max_exp_val)]
443    c = [a[0] * b[0], a[1] + b[1]]
444
445    if c[0] >= 10:
446        c[0] /= 10
447        c[1] += 1
448
449    problem = rf"Product of scientific notations ${a[0]} \times 10^{{{a[1]}}}$ and ${b[0]} \times 10^{{{b[1]}}} = $"
450    solution = rf'${round(c[0], 2)} \times 10^{{{c[1]}}}$'
451    return problem, solution
452
453
454def profit_loss_percent(max_cp=1000, max_sp=1000):
455    """Profit or Loss Percent
456
457    | Ex. Problem | Ex. Solution |
458    | --- | --- |
459    | Loss percent when $CP = 751$ and $SP = 290$ is: | $61.38$ |
460    """
461    cP = random.randint(1, max_cp)
462    sP = random.randint(1, max_sp)
463    diff = abs(sP - cP)
464    if (sP - cP >= 0):
465        profitOrLoss = "Profit"
466    else:
467        profitOrLoss = "Loss"
468    percent = round(diff / cP * 100, 2)
469
470    problem = f"{profitOrLoss} percent when $CP = {cP}$ and $SP = {sP}$ is: "
471    return problem, f'${percent}$'
472
473
474def quotient_of_power_same_base(max_base=50, max_power=10):
475    """Quotient of Powers with Same Base
476
477    | Ex. Problem | Ex. Solution |
478    | --- | --- |
479    | The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ | $0.04$ |
480    """
481    base = random.randint(1, max_base)
482    power1 = random.randint(1, max_power)
483    power2 = random.randint(1, max_power)
484    step = power1 - power2
485    solution = base ** step
486
487    problem = f"The Quotient of ${base}^{{{power1}}}$ and ${base}^{{{power2}}} = " \
488        f"{base}^{{{power1}-{power2}}} = {base}^{{{step}}}$"
489    return problem, f'${solution}$'
490
491
492def quotient_of_power_same_power(max_base=50, max_power=10):
493    """Quotient of Powers with Same Power
494
495    | Ex. Problem | Ex. Solution |
496    | --- | --- |
497    | The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ | $169.84$ |
498    """
499    base1 = random.randint(1, max_base)
500    base2 = random.randint(1, max_base)
501    power = random.randint(1, max_power)
502    step = base1 / base2
503    solution = round(step ** power, 2)
504
505    problem = f"The quotient of ${base1}^{{{power}}}$ and ${base2}^{{{power}}} = " \
506        f"({base1}/{base2})^{power} = {step}^{{{power}}}$"
507    return problem, f'${solution}$'
508
509
510def set_operation(min_size=3, max_size=7):
511    """Union, Intersection, Difference of Two Sets
512
513    | Ex. Problem | Ex. Solution |
514    | --- | --- |
515    | 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}$. |
516    """
517    number_variables_a = random.randint(min_size, max_size)
518    number_variables_b = random.randint(min_size, max_size)
519    a = []
520    b = []
521    for _ in range(number_variables_a):
522        a.append(random.randint(1, 10))
523    for _ in range(number_variables_b):
524        b.append(random.randint(1, 10))
525    a = set(a)
526    b = set(b)
527
528    problem = f"Given the two sets $a={a}$, $b={b}$. " + \
529        "Find the Union, intersection, $a-b$, $b-a$, and symmetric difference"
530    solution = f"Union is ${a.union(b)}$. Intersection is ${a.intersection(b)}$" + \
531        f", $a-b$ is ${a.difference(b)}$, $b-a$ is ${b.difference(a)}$." + \
532        f" Symmetric difference is ${a.symmetric_difference(b)}$."
533    return problem, solution
534
535
536def signum_function(min=-999, max=999):
537    """Signum Function
538
539    | Ex. Problem | Ex. Solution |
540    | --- | --- |
541    | Signum of $-229$ is = | $-1$ |
542    """
543    a = random.randint(min, max)
544    b = 0
545    if (a > 0):
546        b = 1
547    if (a < 0):
548        b = -1
549
550    problem = f"Signum of ${a}$ is ="
551    solution = f'${b}$'
552    return problem, solution
553
554
555def surds_comparison(max_value=100, max_root=10):
556    r"""Comparing Surds
557
558    | Ex. Problem | Ex. Solution |
559    | --- | --- |
560    | Fill in the blanks $42^{\frac{1}{2}}$ _ $45^{\frac{1}{5}}$ | $>$ |
561    """
562    radicand1, radicand2 = tuple(random.sample(range(1, max_value), 2))
563    degree1, degree2 = tuple(random.sample(range(1, max_root), 2))
564    first = math.pow(radicand1, 1 / degree1)
565    second = math.pow(radicand2, 1 / degree2)
566
567    solution = "="
568    if first > second:
569        solution = ">"
570    elif first < second:
571        solution = "<"
572
573    problem = rf"Fill in the blanks ${radicand1}^{{\frac{{1}}{{{degree1}}}}}$ _ ${radicand2}^{{\frac{{1}}{{{degree2}}}}}$"
574    return problem, f'${solution}$'
def arithmetic_progression_sum(max_d=100, max_a=100, max_n=100):
 7def arithmetic_progression_sum(max_d=100,
 8                               max_a=100,
 9                               max_n=100):
10    """Arithmetic Progression Sum
11
12    | Ex. Problem | Ex. Solution |
13    | --- | --- |
14    | Find the sum of first $44$ terms of the AP series: $49, 145, 241 ... $ | $92972.0$ |
15    """
16    d = random.randint(-1 * max_d, max_d)
17    a1 = random.randint(-1 * max_a, max_a)
18    a2 = a1 + d
19    a3 = a1 + 2 * d
20    n = random.randint(4, max_n)
21    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
22    an = a1 + (n - 1) * d
23    solution = n * (a1 + an) / 2
24
25    problem = f'Find the sum of first ${n}$ terms of the AP series: ${apString}$'
26    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):
29def arithmetic_progression_term(max_d=100,
30                                max_a=100,
31                                max_n=100):
32    """Arithmetic Progression Term
33
34    | Ex. Problem | Ex. Solution |
35    | --- | --- |
36    | Find term number $12$ of the AP series: $-54, 24, 102 ... $ | $804$ |
37    """
38    d = random.randint(-1 * max_d, max_d)
39    a1 = random.randint(-1 * max_a, max_a)
40    a2 = a1 + d
41    a3 = a2 + d
42    n = random.randint(4, max_n)
43    apString = str(a1) + ', ' + str(a2) + ', ' + str(a3) + ' ... '
44    solution = a1 + ((n - 1) * d)
45
46    problem = f'Find term number ${n}$ of the AP series: ${apString}$'
47    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):
 75def base_conversion(max_num=60000, max_base=16):
 76    """Base Conversion
 77
 78    | Ex. Problem | Ex. Solution |
 79    | --- | --- |
 80    | Convert $45204$ from base $10$ to base $4$ | $23002110$ |
 81    """
 82    assert type(
 83        max_num
 84    ) == int and max_num >= 100 and max_num <= 65536, "max_num({}) must be >=100 and <=65536".format(
 85        max_num)
 86    assert type(
 87        max_base
 88    ) == int and max_base >= 2 and max_base <= 36, "max_base({}) must be >= 2 and <=36".format(
 89        max_base)
 90
 91    n = random.randint(40, max_num)
 92    dist = [10] * 10 + [2] * 5 + [16] * 5 + [i for i in range(2, max_base + 1)]
 93    # set this way since converting to/from bases 2,10,16 are more common -- can be changed if needed.
 94    bases = random.choices(dist, k=2)
 95    while bases[0] == bases[1]:
 96        bases = random.choices(dist, k=2)
 97
 98    problem = f"Convert ${_fromBaseTenTo(n, bases[0])}$ from base ${bases[0]}$ to base ${bases[1]}$."
 99    ans = _fromBaseTenTo(n, bases[1])
100    return problem, f'${ans}$'

Base Conversion

Ex. Problem Ex. Solution
Convert $45204$ from base $10$ to base $4$ $23002110$
def binomial_distribution():
108def binomial_distribution():
109    """Binomial distribution
110
111    | Ex. Problem | Ex. Solution |
112    | --- | --- |
113    | 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$ |
114    """
115    rejected_fraction = float(random.randint(30, 40)) + random.random()
116    batch = random.randint(10, 20)
117    rejections = random.randint(1, 9)
118
119    answer = 0
120    rejected_fraction = round(rejected_fraction, 2)
121    for i in range(0, rejections + 1):
122        answer += _newton_symbol(float(batch), float(i)) * ((rejected_fraction / 100.) ** float(i)) * \
123            ((1 - (rejected_fraction / 100.)) ** (float(batch) - float(i)))
124
125    answer = round(100 * answer, 2)
126
127    problem = "A manufacturer of metal pistons finds that, on average, ${0:}$% "\
128        "of the pistons they manufacture are rejected because " \
129        "they are incorrectly sized. What is the probability that a "\
130        "batch of ${1:}$ pistons will contain no more than ${2:}$ " \
131        "rejected pistons?".format(rejected_fraction, batch, rejections)
132    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):
135def celsius_to_fahrenheit(max_temp=100):
136    """Celsius to Fahrenheit
137
138    | Ex. Problem | Ex. Solution |
139    | --- | --- |
140    | Convert $-46$ degrees Celsius to degrees Fahrenheit | $-50.8$ |
141    """
142    celsius = random.randint(-50, max_temp)
143    fahrenheit = (celsius * (9 / 5)) + 32
144
145    problem = f"Convert ${celsius}$ degrees Celsius to degrees Fahrenheit"
146    solution = f'${fahrenheit}$'
147    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):
150def common_factors(max_val=100):
151    """Common Factors
152
153    | Ex. Problem | Ex. Solution |
154    | --- | --- |
155    | Common Factors of $100$ and $44 = $ | $[1, 2, 4]$ |
156    """
157    a = x = random.randint(1, max_val)
158    b = y = random.randint(1, max_val)
159
160    if (x < y):
161        min = x
162    else:
163        min = y
164
165    count = 0
166    arr = []
167
168    for i in range(1, min + 1):
169        if (x % i == 0):
170            if (y % i == 0):
171                count = count + 1
172                arr.append(i)
173
174    problem = f"Common Factors of ${a}$ and ${b} = $"
175    solution = f'${arr}$'
176    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):
179def complex_to_polar(min_real_imaginary_num=-20,
180                     max_real_imaginary_num=20):
181    r"""Complex to polar form
182
183    | Ex. Problem | Ex. Solution |
184    | --- | --- |
185    | $19.42(-19.0\theta + i-4.0\theta)$ | $-2.93$ |
186    """
187    num = complex(random.randint(min_real_imaginary_num, max_real_imaginary_num),
188                  random.randint(min_real_imaginary_num, max_real_imaginary_num))
189    a = num.real
190    b = num.imag
191    r = round(math.hypot(a, b), 2)
192    theta = round(math.atan2(b, a), 2)
193
194    problem = rf'${r}({a}\theta + i{b}\theta)$'
195    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=4000):
198def decimal_to_roman_numerals(max_decimal=4000):
199    """Decimal to Roman Numerals
200
201    | Ex. Problem | Ex. Solution |
202    | --- | --- |
203    | The number $92$ in roman numerals is:  | $XCII$ |
204    """
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 = [round(random.uniform(1, 10), 2),
441         random.randint(min_exp_val, max_exp_val)]
442    b = [round(random.uniform(1, 10), 2),
443         random.randint(min_exp_val, max_exp_val)]
444    c = [a[0] * b[0], a[1] + b[1]]
445
446    if c[0] >= 10:
447        c[0] /= 10
448        c[1] += 1
449
450    problem = rf"Product of scientific notations ${a[0]} \times 10^{{{a[1]}}}$ and ${b[0]} \times 10^{{{b[1]}}} = $"
451    solution = rf'${round(c[0], 2)} \times 10^{{{c[1]}}}$'
452    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):
455def profit_loss_percent(max_cp=1000, max_sp=1000):
456    """Profit or Loss Percent
457
458    | Ex. Problem | Ex. Solution |
459    | --- | --- |
460    | Loss percent when $CP = 751$ and $SP = 290$ is: | $61.38$ |
461    """
462    cP = random.randint(1, max_cp)
463    sP = random.randint(1, max_sp)
464    diff = abs(sP - cP)
465    if (sP - cP >= 0):
466        profitOrLoss = "Profit"
467    else:
468        profitOrLoss = "Loss"
469    percent = round(diff / cP * 100, 2)
470
471    problem = f"{profitOrLoss} percent when $CP = {cP}$ and $SP = {sP}$ is: "
472    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):
475def quotient_of_power_same_base(max_base=50, max_power=10):
476    """Quotient of Powers with Same Base
477
478    | Ex. Problem | Ex. Solution |
479    | --- | --- |
480    | The Quotient of $5^{6}$ and $5^{8} = 5^{6-8} = 5^{-2}$ | $0.04$ |
481    """
482    base = random.randint(1, max_base)
483    power1 = random.randint(1, max_power)
484    power2 = random.randint(1, max_power)
485    step = power1 - power2
486    solution = base ** step
487
488    problem = f"The Quotient of ${base}^{{{power1}}}$ and ${base}^{{{power2}}} = " \
489        f"{base}^{{{power1}-{power2}}} = {base}^{{{step}}}$"
490    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):
493def quotient_of_power_same_power(max_base=50, max_power=10):
494    """Quotient of Powers with Same Power
495
496    | Ex. Problem | Ex. Solution |
497    | --- | --- |
498    | The quotient of $19^{8}$ and $10^{8} = (19/10)^8 = 1.9^{8}$ | $169.84$ |
499    """
500    base1 = random.randint(1, max_base)
501    base2 = random.randint(1, max_base)
502    power = random.randint(1, max_power)
503    step = base1 / base2
504    solution = round(step ** power, 2)
505
506    problem = f"The quotient of ${base1}^{{{power}}}$ and ${base2}^{{{power}}} = " \
507        f"({base1}/{base2})^{power} = {step}^{{{power}}}$"
508    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):
511def set_operation(min_size=3, max_size=7):
512    """Union, Intersection, Difference of Two Sets
513
514    | Ex. Problem | Ex. Solution |
515    | --- | --- |
516    | 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}$. |
517    """
518    number_variables_a = random.randint(min_size, max_size)
519    number_variables_b = random.randint(min_size, max_size)
520    a = []
521    b = []
522    for _ in range(number_variables_a):
523        a.append(random.randint(1, 10))
524    for _ in range(number_variables_b):
525        b.append(random.randint(1, 10))
526    a = set(a)
527    b = set(b)
528
529    problem = f"Given the two sets $a={a}$, $b={b}$. " + \
530        "Find the Union, intersection, $a-b$, $b-a$, and symmetric difference"
531    solution = f"Union is ${a.union(b)}$. Intersection is ${a.intersection(b)}$" + \
532        f", $a-b$ is ${a.difference(b)}$, $b-a$ is ${b.difference(a)}$." + \
533        f" Symmetric difference is ${a.symmetric_difference(b)}$."
534    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):
537def signum_function(min=-999, max=999):
538    """Signum Function
539
540    | Ex. Problem | Ex. Solution |
541    | --- | --- |
542    | Signum of $-229$ is = | $-1$ |
543    """
544    a = random.randint(min, max)
545    b = 0
546    if (a > 0):
547        b = 1
548    if (a < 0):
549        b = -1
550
551    problem = f"Signum of ${a}$ is ="
552    solution = f'${b}$'
553    return problem, solution

Signum Function

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

Comparing Surds

Ex. Problem Ex. Solution
Fill in the blanks $42^{\frac{1}{2}}$ _ $45^{\frac{1}{5}}$ $>$