mathgenerator.geometry

  1import random
  2import math
  3from math import cos, sin, pi
  4
  5
  6def angle_btw_vectors(max_elt_amt=20):
  7    r"""Angle between 2 vectors
  8
  9    | Ex. Problem | Ex. Solution |
 10    | --- | --- |
 11    | angle between the vectors $[363.84, 195.54, 997.08, 39.26, 60.14, 722.7, 888.57, 713.15, 436.22, 712.23, 349.23, 595.91, 191.8, 824.58, 861.56, 122.73, 815.14, 700.68, 506.5]$ and $[760.85, 934.67, 513.37, 796.93, 809.97, 423.54, 162.69, 758.96, 133.42, 478.14, 771.84, 824.88, 483.07, 134.41, 954.41, 893.42, 191.01, 453.97, 648.59]$ is: | $0.81$ radians |
 12    """
 13    s = 0
 14    v1 = [
 15        round(random.uniform(0, 1000), 2)
 16        for i in range(random.randint(2, max_elt_amt))
 17    ]
 18    v2 = [round(random.uniform(0, 1000), 2) for i in v1]
 19    for i in range(len(v1)):
 20        s += v1[i] * v2[i]
 21
 22    mags = math.sqrt(sum([i**2
 23                          for i in v1])) * math.sqrt(sum([i**2 for i in v2]))
 24    solution = ''
 25    ans = 0
 26    try:
 27        ans = round(math.acos(s / mags), 2)
 28        solution = f"${ans}$ radians"
 29    except ValueError:
 30        print('angleBtwVectorsFunc has some issues with math module, line 16')
 31        solution = 'NaN'
 32        ans = 'NaN'
 33    # would return the answer in radians
 34    problem = f"angle between the vectors ${v1}$ and ${v2}$ is:"
 35    return problem, solution
 36
 37
 38def angle_regular_polygon(min_val=3, max_val=20):
 39    r"""Angle of a Regular Polygon
 40
 41    | Ex. Problem | Ex. Solution |
 42    | --- | --- |
 43    | Find the angle of a regular polygon with $8$ sides | $135.0$ |
 44    """
 45    sideNum = random.randint(min_val, max_val)
 46    problem = f"Find the angle of a regular polygon with ${sideNum}$ sides"
 47
 48    exteriorAngle = round((360 / sideNum), 2)
 49    solution = f'${180 - exteriorAngle}$'
 50
 51    return problem, solution
 52
 53
 54def arc_length(max_radius=49, max_angle=359):
 55    r"""Arc length of Angle
 56
 57    | Ex. Problem | Ex. Solution |
 58    | --- | --- |
 59    | Given radius, $44$ and angle, $184$. Find the arc length of the angle. | Arc length of the angle $= 141.30186$ |
 60    """
 61    radius = random.randint(1, max_radius)
 62    angle = random.randint(1, max_angle)
 63    angle_arc_length = float((angle / 360) * 2 * math.pi * radius)
 64    formatted_float = "{:.5f}".format(angle_arc_length)
 65
 66    problem = f"Given radius, ${radius}$ and angle, ${angle}$. Find the arc length of the angle."
 67    solution = f"Arc length of the angle $= {formatted_float}$"
 68    return problem, solution
 69
 70
 71def area_of_circle(max_radius=100):
 72    r"""Area of Circle
 73
 74    | Ex. Problem | Ex. Solution |
 75    | --- | --- |
 76    | Area of circle with radius $29=$ | $2642.08$ |
 77    """
 78    r = random.randint(0, max_radius)
 79    area = round(pi * r * r, 2)
 80
 81    problem = f'Area of circle with radius ${r}=$'
 82    return problem, f'${area}$'
 83
 84
 85def area_of_circle_given_center_and_point(max_coordinate=10, max_radius=10):
 86    r"""Area of Circle given center and a point on circle
 87
 88    | Ex. Problem | Ex. Solution |
 89    | --- | --- |
 90    | Area of circle with center $(7,-6)$ and passing through $(1.0, -6.0)$ is | $113.1$ |
 91    """
 92    r = random.randint(0, max_radius)
 93    center_x = random.randint(-max_coordinate, max_coordinate)
 94    center_y = random.randint(-max_coordinate, max_coordinate)
 95
 96    angle = random.choice([0, pi // 6, pi // 2, pi, pi + pi // 6, 3 * pi // 2])
 97
 98    point_x = center_x + round(r * cos(angle), 2)
 99    point_y = center_y + round(r * sin(angle), 2)
100
101    area = round(pi * r * r, 2)
102
103    problem = f"Area of circle with center $({center_x},{center_y})$ and passing through $({point_x}, {point_y})$ is"
104    return problem, f'${area}$'
105
106
107def area_of_trapezoid(max_len=20):
108    r"""Area of Trapezoid
109
110    | Ex. Problem | Ex. Solution |
111    | --- | --- |
112    | Area of trapezoid with height $12$ and base lengths: $3, 7, = $ | $60$ |
113    """
114    a = random.randint(1, max_len)
115    b = random.randint(1, max_len)
116    h = random.randint(1, max_len)
117
118    area = (a + b) * h / 2
119
120    problem = f"Area of trapezoid with height ${h}$ and base lengths: ${a}, {b}, = $"
121    solution = f'${round(area, 2)}$'
122    return problem, solution
123
124
125def area_of_triangle(max_a=20, max_b=20):
126    r"""Area of Triangle
127
128    | Ex. Problem | Ex. Solution |
129    | --- | --- |
130    | Area of triangle with side lengths: $8, 1, 8 = $ | $3.99$ |
131    """
132    a = random.randint(1, max_a)
133    b = random.randint(1, max_b)
134    c = random.randint(abs(b - a) + 1, abs(a + b) - 1)
135
136    s = (a + b + c) / 2
137    area = (s * (s - a) * (s - b) * (s - c))**0.5
138
139    problem = f"Area of triangle with side lengths: ${a}, {b}, {c} = $"
140    solution = f'${round(area, 2)}$'
141    return problem, solution
142
143
144# Handles degrees in quadrant one
145def basic_trigonometry(angles=[0, 30, 45, 60, 90],
146                       functions=["sin", "cos", "tan"]):
147    r"""Trigonometric Values
148
149    | Ex. Problem | Ex. Solution |
150    | --- | --- |
151    | $\sin(30) = $ | $\frac{1}{2}$ |
152    """
153    angle = random.choice(angles)
154    function = random.choice(functions)
155
156    problem = rf"$\{function}({angle}) = $"
157
158    expression = 'math.' + function + '(math.radians(angle))'
159    result_fraction_map = {
160        0.0: "0",
161        0.5: r"\frac{1}{2}",
162        0.71: r"\frac{1}{\sqrt{2}}",
163        0.87: r"\frac{\sqrt{3}}{2}",
164        1.0: "1",
165        0.58: r"\frac{1}{\sqrt{3}}",
166        1.73: r"\sqrt{3}",
167    }
168
169    solution = result_fraction_map[round(
170        eval(expression),
171        2)] if round(eval(expression),
172                     2) <= 99999 else r"\infty"  # for handling the ∞ condition
173
174    return problem, f'${solution}$'
175
176
177def circumference(max_radius=100):
178    r"""Circumference of Circle
179
180    | Ex. Problem | Ex. Solution |
181    | --- | --- |
182    | Circumference of circle with radius $56 = $ | $351.86$ |
183    """
184    r = random.randint(0, max_radius)
185    circumference = round(2 * math.pi * r, 2)
186
187    problem = f"Circumference of circle with radius ${r} = $"
188    return problem, f'${circumference}$'
189
190
191def complementary_and_supplementary_angle(max_supp=180, max_comp=90):
192    r"""Complementary and Supplementary Angle
193
194    | Ex. Problem | Ex. Solution |
195    | --- | --- |
196    | The complementary angle of $15 =$ | $75$ |
197    """
198    angleType = random.choice(["supplementary", "complementary"])
199
200    if angleType == "supplementary":
201        angle = random.randint(1, max_supp)
202        angleAns = 180 - angle
203    else:
204        angle = random.randint(1, max_comp)
205        angleAns = 90 - angle
206
207    problem = f"The {angleType} angle of ${angle} =$"
208    solution = f'${angleAns}$'
209    return problem, solution
210
211
212def curved_surface_area_cylinder(max_radius=49, max_height=99):
213    r"""Curved surface area of a cylinder
214
215    | Ex. Problem | Ex. Solution |
216    | --- | --- |
217    | What is the curved surface area of a cylinder of radius, $44$ and height, $92$? | $25434.33$ |
218    """
219    r = random.randint(1, max_radius)
220    h = random.randint(1, max_height)
221    csa = float(2 * math.pi * r * h)
222    formatted_float = round(csa, 2)  # "{:.5f}".format(csa)
223
224    problem = f"What is the curved surface area of a cylinder of radius, ${r}$ and height, ${h}$?"
225    solution = f"${formatted_float}$"
226    return problem, solution
227
228
229def degree_to_rad(max_deg=360):
230    r"""Degrees to Radians
231
232    | Ex. Problem | Ex. Solution |
233    | --- | --- |
234    | Angle $113$ degrees in radians is: | $1.97$ |
235    """
236    a = random.randint(0, max_deg)
237    b = (math.pi * a) / 180
238    b = round(b, 2)
239
240    problem = f"Angle ${a}$ degrees in radians is: "
241    solution = f'${b}$'
242    return problem, solution
243
244
245def equation_of_line_from_two_points(max_coordinate=20, min_coordinate=-20):
246    r"""Equation of line from two points
247
248    | Ex. Problem | Ex. Solution |
249    | --- | --- |
250    | What is the equation of the line between points $(13,9)$ and $(6,-19)$ in slope-intercept form? | $y = 4x -43$ |
251    """
252    x1 = random.randint(min_coordinate, max_coordinate)
253    x2 = random.randint(min_coordinate, max_coordinate)
254
255    y1 = random.randint(min_coordinate, max_coordinate)
256    y2 = random.randint(min_coordinate, max_coordinate)
257
258    coeff_y = (x2 - x1)
259    coeff_x = (y2 - y1)
260    constant = y2 * coeff_y - x2 * coeff_x
261
262    gcd = math.gcd(abs(coeff_x), abs(coeff_y))
263
264    if gcd != 1:
265        if coeff_y > 0:
266            coeff_y //= gcd
267        if coeff_x > 0:
268            coeff_x //= gcd
269        if constant > 0:
270            constant //= gcd
271        if coeff_y < 0:
272            coeff_y = -(-coeff_y // gcd)
273        if coeff_x < 0:
274            coeff_x = -(-coeff_x // gcd)
275        if constant < 0:
276            constant = -(-constant // gcd)
277    if coeff_y < 0:
278        coeff_y = -(coeff_y)
279        coeff_x = -(coeff_x)
280        constant = -(constant)
281    if coeff_x in [1, -1]:
282        if coeff_x == 1:
283            coeff_x = ''
284        else:
285            coeff_x = '-'
286    if coeff_y in [1, -1]:
287        if coeff_y == 1:
288            coeff_y = ''
289        else:
290            coeff_y = '-'
291
292    problem = f"What is the equation of the line between points $({x1},{y1})$ and $({x2},{y2})$ in slope-intercept form?"
293    if coeff_x == 0:
294        solution = str(coeff_y) + "y = " + str(constant)
295    elif coeff_y == 0:
296        solution = str(coeff_x) + "x = " + str(-constant)
297    else:
298        if constant > 0:
299            solution = str(coeff_y) + "y = " + str(coeff_x) + \
300                "x + " + str(constant)
301        else:
302            solution = str(coeff_y) + "y = " + \
303                str(coeff_x) + "x " + str(constant)
304    return problem, f'${solution}$'
305
306
307def fourth_angle_of_quadrilateral(max_angle=180):
308    r"""Fourth Angle of Quadrilateral
309
310    | Ex. Problem | Ex. Solution |
311    | --- | --- |
312    | Fourth angle of quadrilateral with angles $162 , 43, 78 =$ | $77$ |
313    """
314    angle1 = random.randint(1, max_angle)
315    angle2 = random.randint(1, 240 - angle1)
316    angle3 = random.randint(1, 340 - (angle1 + angle2))
317
318    sum_ = angle1 + angle2 + angle3
319    angle4 = 360 - sum_
320
321    problem = f"Fourth angle of quadrilateral with angles ${angle1} , {angle2}, {angle3} =$"
322    solution = f'${angle4}$'
323    return problem, solution
324
325
326def perimeter_of_polygons(max_sides=12, max_length=120):
327    r"""Perimeter of Polygons
328
329    | Ex. Problem | Ex. Solution |
330    | --- | --- |
331    | The perimeter of a $4$ sided polygon with lengths of $30, 105, 78, 106$cm is: | $319$ |
332    """
333    size_of_sides = random.randint(3, max_sides)
334    sides = [random.randint(1, max_length) for _ in range(size_of_sides)]
335
336    problem = f"The perimeter of a ${size_of_sides}$ sided polygon with lengths of ${', '.join(map(str, sides))}$cm is: "
337    solution = sum(sides)
338
339    return problem, f'${solution}$'
340
341
342def pythagorean_theorem(max_length=20):
343    """Pythagorean Theorem
344
345    | Ex. Problem | Ex. Solution |
346    | --- | --- |
347    | What is the hypotenuse of a right triangle given the other two sides have lengths $9$ and $10$? | $13.45$ |
348    """
349    a = random.randint(1, max_length)
350    b = random.randint(1, max_length)
351    c = round((a**2 + b**2)**0.5, 2)
352
353    problem = f"What is the hypotenuse of a right triangle given the other two sides have lengths ${a}$ and ${b}$?"
354    solution = f"${c}$"
355    return problem, solution
356
357
358def radian_to_deg(max_rad=6.28):
359    """Radians to Degrees"""
360    a = random.randint(0, int(max_rad * 100)) / 100
361    b = round((180 * a) / math.pi, 2)
362
363    problem = f"Angle ${a}$ radians in degrees is: "
364    solution = f'${b}$'
365    return problem, solution
366
367
368def sector_area(max_radius=49, max_angle=359):
369    """Area of a Sector
370
371    | Ex. Problem | Ex. Solution |
372    | --- | --- |
373    | What is the area of a sector with radius $42$ and angle $83$ degrees? | $1277.69$ |
374    """
375    r = random.randint(1, max_radius)
376    a = random.randint(1, max_angle)
377    secArea = float((a / 360) * math.pi * r * r)
378    formatted_float = round(secArea, 2)
379
380    problem = f"What is the area of a sector with radius ${r}$ and angle ${a}$ degrees?"
381    solution = f"${formatted_float}$"
382    return problem, solution
383
384
385def sum_of_polygon_angles(max_sides=12):
386    """Sum of Angles of Polygon
387
388    | Ex. Problem | Ex. Solution |
389    | --- | --- |
390    | What is the sum of interior angles of a polygon with $8$ sides? | $1080$ |
391    """
392    side_count = random.randint(3, max_sides)
393    sum = (side_count - 2) * 180
394
395    problem = f"What is the sum of interior angles of a polygon with ${side_count}$ sides?"
396    return problem, f'${sum}$'
397
398
399def surface_area_cone(max_radius=20, max_height=50, unit='m'):
400    """Surface area of a cone
401
402    | Ex. Problem | Ex. Solution |
403    | --- | --- |
404    | Surface area of cone with height $= 26m$ and radius $= 6m$ is | $616 m^2$ |
405    """
406    a = random.randint(1, max_height)
407    b = random.randint(1, max_radius)
408
409    slopingHeight = math.sqrt(a**2 + b**2)
410    ans = int(math.pi * b * slopingHeight + math.pi * b * b)
411
412    problem = f"Surface area of cone with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
413    solution = f"${ans} {unit}^2$"
414    return problem, solution
415
416
417def surface_area_cube(max_side=20, unit='m'):
418    """Surface area of a cube
419
420    | Ex. Problem | Ex. Solution |
421    | --- | --- |
422    | Surface area of cube with side $= 6m$ is | $216 m^2$ |
423    """
424    a = random.randint(1, max_side)
425    ans = 6 * (a**2)
426
427    problem = f"Surface area of cube with side $= {a}{unit}$ is"
428    solution = f"${ans} {unit}^2$"
429    return problem, solution
430
431
432def surface_area_cuboid(max_side=20, unit='m'):
433    """Surface area of a cuboid
434
435    | Ex. Problem | Ex. Solution |
436    | --- | --- |
437    | Surface area of cuboid with sides of lengths: $11m, 20m, 8m$ is | $936 m^2$ |
438    """
439    a = random.randint(1, max_side)
440    b = random.randint(1, max_side)
441    c = random.randint(1, max_side)
442    ans = 2 * (a * b + b * c + c * a)
443
444    problem = f"Surface area of cuboid with sides of lengths: ${a}{unit}, {b}{unit}, {c}{unit}$ is"
445    solution = f"${ans} {unit}^2$"
446    return problem, solution
447
448
449def surface_area_cylinder(max_radius=20, max_height=50, unit='m'):
450    """Surface area of a cylinder
451
452    | Ex. Problem | Ex. Solution |
453    | --- | --- |
454    | Surface area of cylinder with height $= 26m$ and radius $= 15m$ is | $3864 m^2$ |
455    """
456    a = random.randint(1, max_height)
457    b = random.randint(1, max_radius)
458    ans = int(2 * math.pi * a * b + 2 * math.pi * b * b)
459
460    problem = f"Surface area of cylinder with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
461    solution = f"${ans} {unit}^2$"
462    return problem, solution
463
464
465def surface_area_pyramid(unit='m'):
466    """Surface area of a pyramid
467
468    | Ex. Problem | Ex. Solution |
469    | --- | --- |
470    | Surface area of pyramid with base length $= 30m$, base width $= 40m$, and height $= 25m$ is | $2400 m^2$ |
471    """
472    # List of Pythagorean triplets
473    _PYTHAGOREAN = [(3, 4, 5), (6, 8, 10), (9, 12, 15), (12, 16, 20),
474                    (15, 20, 25), (5, 12, 13), (10, 24, 26), (7, 24, 25)]
475
476    # Generate first triplet
477    height, half_width, _ = random.sample(
478        random.choice(_PYTHAGOREAN), 3)
479    
480    # Calculate the first triangle height
481    triangle_height_1 = math.sqrt((half_width)**2 + height**2)
482
483    # Generate second triplet
484    second_triplet = random.choice([i for i in _PYTHAGOREAN if height in i])
485    half_length, _ = random.sample(
486        tuple(i for i in second_triplet if i != height), 2)
487    
488    # Calculate the second triangle height
489    triangle_height_2 = math.sqrt((half_length)**2 + height**2)
490    
491    # Calculate triangles area
492    triangle_1 = half_length * triangle_height_1
493    triangle_2 = half_width * triangle_height_2
494
495    # Calculate base area
496    base = 4 * half_width * half_length
497    ans = base + 2 * triangle_1 + 2 * triangle_2
498    
499    problem = f"Surface area of pyramid with base length $= {2*half_length}{unit}$, base width $= {2*half_width}{unit}$, and height $= {height}{unit}$ is"
500    solution = f"${ans} {unit}^2$"
501    return problem, solution
502
503
504def surface_area_sphere(max_side=20, unit='m'):
505    """Surface area of a sphere
506
507    | Ex. Problem | Ex. Solution |
508    | --- | --- |
509    | Surface area of a sphere with radius $= 8m$ is | $804.25 m^2$ |
510    """
511    r = random.randint(1, max_side)
512    ans = round(4 * math.pi * r * r, 2)
513
514    problem = f"Surface area of a sphere with radius $= {r}{unit}$ is"
515    solution = f"${ans} {unit}^2$"
516    return problem, solution
517
518
519def third_angle_of_triangle(max_angle=89):
520    """Third Angle of Triangle
521
522    | Ex. Problem | Ex. Solution |
523    | --- | --- |
524    | Third angle of triangle with angles $10$ and $22 =$ | $148$ |
525    """
526    angle1 = random.randint(1, max_angle)
527    angle2 = random.randint(1, max_angle)
528    angle3 = 180 - (angle1 + angle2)
529
530    problem = f"Third angle of triangle with angles ${angle1}$ and ${angle2} = $"
531    return problem, f'${angle3}$'
532
533
534def valid_triangle(max_side_length=50):
535    """Valid Triangle
536
537    | Ex. Problem | Ex. Solution |
538    | --- | --- |
539    | Does triangel with sides $10, 31$ and $14$ exist? | No |
540    """
541    sideA = random.randint(1, max_side_length)
542    sideB = random.randint(1, max_side_length)
543    sideC = random.randint(1, max_side_length)
544
545    sideSums = [sideA + sideB, sideB + sideC, sideC + sideA]
546    sides = [sideC, sideA, sideB]
547
548    exists = True & (sides[0] < sideSums[0]) & (sides[1] < sideSums[1]) & (
549        sides[2] < sideSums[2])
550
551    problem = f"Does triangle with sides ${sideA}, {sideB}$ and ${sideC}$ exist?"
552    solution = "Yes" if exists else "No"
553    return problem, f'${solution}$'
554
555
556def volume_cone(max_radius=20, max_height=50, unit='m'):
557    """Volume of a cone
558
559    | Ex. Problem | Ex. Solution |
560    | --- | --- |
561    | Volume of cone with height $= 44m$ and radius $= 11m$ is | $5575 m^3$ |
562    """
563    a = random.randint(1, max_height)
564    b = random.randint(1, max_radius)
565    ans = int(math.pi * b * b * a * (1 / 3))
566
567    problem = f"Volume of cone with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
568    solution = f"${ans} {unit}^3$"
569    return problem, solution
570
571
572def volume_cube(max_side=20, unit='m'):
573    """Volume of a cube
574
575    | Ex. Problem | Ex. Solution |
576    | --- | --- |
577    | Volume of a cube with a side length of $19m$ is | $6859 m^3$ |
578    """
579    a = random.randint(1, max_side)
580    ans = a**3
581
582    problem = f"Volume of cube with a side length of ${a}{unit}$ is"
583    solution = f"${ans} {unit}^3$"
584    return problem, solution
585
586
587def volume_cuboid(max_side=20, unit='m'):
588    """Volume of a cuboid
589
590    | Ex. Problem | Ex. Solution |
591    | --- | --- |
592    | Volume of cuboid with sides = $17m, 11m, 13m$ is | $2431 m^3$ |
593    """
594    a = random.randint(1, max_side)
595    b = random.randint(1, max_side)
596    c = random.randint(1, max_side)
597    ans = a * b * c
598
599    problem = f"Volume of cuboid with sides = ${a}{unit}, {b}{unit}, {c}{unit}$ is"
600    solution = f"${ans} {unit}^3$"
601    return problem, solution
602
603
604def volume_cylinder(max_radius=20, max_height=50, unit='m'):
605    """Volume of a cylinder
606
607    | Ex. Problem | Ex. Solution |
608    | --- | --- |
609    | Volume of cylinder with height $= 3m$ and radius $= 10m$ is | $942 m^3$ |
610    """
611    a = random.randint(1, max_height)
612    b = random.randint(1, max_radius)
613    ans = int(math.pi * b * b * a)
614
615    problem = f"Volume of cylinder with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
616    solution = f"${ans} {unit}^3$"
617    return problem, solution
618
619
620def volume_cone_frustum(max_r1=20, max_r2=20, max_height=50, unit='m'):
621    """Volume of the frustum of a cone
622
623    | Ex. Problem | Ex. Solution |
624    | --- | --- |
625    | Volume of frustum with height $= 30m$ and $r1 = 20m$ is and $r2 = 8m$ is | $19603.54 m^3$ |
626    """
627    h = random.randint(1, max_height)
628    r1 = random.randint(1, max_r1)
629    r2 = random.randint(1, max_r2)
630    ans = round(((math.pi * h) * (r1**2 + r2**2 + r1 * r2)) / 3, 2)
631
632    problem = f"Volume of frustum with height $= {h}{unit}$ and $r1 = {r1}{unit}$ is and $r2 = {r2}{unit}$ is "
633    solution = f"${ans} {unit}^3$"
634    return problem, solution
635
636
637def volume_hemisphere(max_radius=100):
638    """Volume of a hemisphere
639
640    | Ex. Problem | Ex. Solution |
641    | --- | --- |
642    | Volume of hemisphere with radius $32m =$ | $68629.14 m^3$ |
643    """
644    r = random.randint(1, max_radius)
645    ans = round((2 * math.pi / 3) * r**3, 2)
646
647    problem = f"Volume of hemisphere with radius ${r} m =$ "
648    solution = f"${ans} m^3$"
649    return problem, solution
650
651
652def volume_pyramid(max_length=20, max_width=20, max_height=50, unit='m'):
653    """Volume of a pyramid
654
655    | Ex. Problem | Ex. Solution |
656    | --- | --- |
657    | Volume of pyramid with base length $= 7 m$, base width $= 18 m$ and height $= 42 m$ is | $1764.0 m^3$ |
658    """
659    length = random.randint(1, max_length)
660    width = random.randint(1, max_width)
661    height = random.randint(1, max_height)
662
663    ans = round((length * width * height) / 3, 2)
664
665    problem = f"Volume of pyramid with base length $= {length} {unit}$, base width $= {width} {unit}$ and height $= {height} {unit}$ is"
666    solution = f"${ans} {unit}^3$"
667    return problem, solution
668
669
670def volume_sphere(max_radius=100):
671    """Volume of a sphere
672
673    | Ex. Problem | Ex. Solution |
674    | --- | --- |
675    | Volume of sphere with radius $30 m = $ | $113097.36 m^3$ |
676    """
677    r = random.randint(1, max_radius)
678    ans = round((4 * math.pi / 3) * r**3, 2)
679
680    problem = f"Volume of sphere with radius ${r} m = $"
681    solution = f"${ans} m^3$"
682    return problem, solution
def angle_btw_vectors(max_elt_amt=20):
 7def angle_btw_vectors(max_elt_amt=20):
 8    r"""Angle between 2 vectors
 9
10    | Ex. Problem | Ex. Solution |
11    | --- | --- |
12    | angle between the vectors $[363.84, 195.54, 997.08, 39.26, 60.14, 722.7, 888.57, 713.15, 436.22, 712.23, 349.23, 595.91, 191.8, 824.58, 861.56, 122.73, 815.14, 700.68, 506.5]$ and $[760.85, 934.67, 513.37, 796.93, 809.97, 423.54, 162.69, 758.96, 133.42, 478.14, 771.84, 824.88, 483.07, 134.41, 954.41, 893.42, 191.01, 453.97, 648.59]$ is: | $0.81$ radians |
13    """
14    s = 0
15    v1 = [
16        round(random.uniform(0, 1000), 2)
17        for i in range(random.randint(2, max_elt_amt))
18    ]
19    v2 = [round(random.uniform(0, 1000), 2) for i in v1]
20    for i in range(len(v1)):
21        s += v1[i] * v2[i]
22
23    mags = math.sqrt(sum([i**2
24                          for i in v1])) * math.sqrt(sum([i**2 for i in v2]))
25    solution = ''
26    ans = 0
27    try:
28        ans = round(math.acos(s / mags), 2)
29        solution = f"${ans}$ radians"
30    except ValueError:
31        print('angleBtwVectorsFunc has some issues with math module, line 16')
32        solution = 'NaN'
33        ans = 'NaN'
34    # would return the answer in radians
35    problem = f"angle between the vectors ${v1}$ and ${v2}$ is:"
36    return problem, solution

Angle between 2 vectors

Ex. Problem Ex. Solution
angle between the vectors $[363.84, 195.54, 997.08, 39.26, 60.14, 722.7, 888.57, 713.15, 436.22, 712.23, 349.23, 595.91, 191.8, 824.58, 861.56, 122.73, 815.14, 700.68, 506.5]$ and $[760.85, 934.67, 513.37, 796.93, 809.97, 423.54, 162.69, 758.96, 133.42, 478.14, 771.84, 824.88, 483.07, 134.41, 954.41, 893.42, 191.01, 453.97, 648.59]$ is: $0.81$ radians
def angle_regular_polygon(min_val=3, max_val=20):
39def angle_regular_polygon(min_val=3, max_val=20):
40    r"""Angle of a Regular Polygon
41
42    | Ex. Problem | Ex. Solution |
43    | --- | --- |
44    | Find the angle of a regular polygon with $8$ sides | $135.0$ |
45    """
46    sideNum = random.randint(min_val, max_val)
47    problem = f"Find the angle of a regular polygon with ${sideNum}$ sides"
48
49    exteriorAngle = round((360 / sideNum), 2)
50    solution = f'${180 - exteriorAngle}$'
51
52    return problem, solution

Angle of a Regular Polygon

Ex. Problem Ex. Solution
Find the angle of a regular polygon with $8$ sides $135.0$
def arc_length(max_radius=49, max_angle=359):
55def arc_length(max_radius=49, max_angle=359):
56    r"""Arc length of Angle
57
58    | Ex. Problem | Ex. Solution |
59    | --- | --- |
60    | Given radius, $44$ and angle, $184$. Find the arc length of the angle. | Arc length of the angle $= 141.30186$ |
61    """
62    radius = random.randint(1, max_radius)
63    angle = random.randint(1, max_angle)
64    angle_arc_length = float((angle / 360) * 2 * math.pi * radius)
65    formatted_float = "{:.5f}".format(angle_arc_length)
66
67    problem = f"Given radius, ${radius}$ and angle, ${angle}$. Find the arc length of the angle."
68    solution = f"Arc length of the angle $= {formatted_float}$"
69    return problem, solution

Arc length of Angle

Ex. Problem Ex. Solution
Given radius, $44$ and angle, $184$. Find the arc length of the angle. Arc length of the angle $= 141.30186$
def area_of_circle(max_radius=100):
72def area_of_circle(max_radius=100):
73    r"""Area of Circle
74
75    | Ex. Problem | Ex. Solution |
76    | --- | --- |
77    | Area of circle with radius $29=$ | $2642.08$ |
78    """
79    r = random.randint(0, max_radius)
80    area = round(pi * r * r, 2)
81
82    problem = f'Area of circle with radius ${r}=$'
83    return problem, f'${area}$'

Area of Circle

Ex. Problem Ex. Solution
Area of circle with radius $29=$ $2642.08$
def area_of_circle_given_center_and_point(max_coordinate=10, max_radius=10):
 86def area_of_circle_given_center_and_point(max_coordinate=10, max_radius=10):
 87    r"""Area of Circle given center and a point on circle
 88
 89    | Ex. Problem | Ex. Solution |
 90    | --- | --- |
 91    | Area of circle with center $(7,-6)$ and passing through $(1.0, -6.0)$ is | $113.1$ |
 92    """
 93    r = random.randint(0, max_radius)
 94    center_x = random.randint(-max_coordinate, max_coordinate)
 95    center_y = random.randint(-max_coordinate, max_coordinate)
 96
 97    angle = random.choice([0, pi // 6, pi // 2, pi, pi + pi // 6, 3 * pi // 2])
 98
 99    point_x = center_x + round(r * cos(angle), 2)
100    point_y = center_y + round(r * sin(angle), 2)
101
102    area = round(pi * r * r, 2)
103
104    problem = f"Area of circle with center $({center_x},{center_y})$ and passing through $({point_x}, {point_y})$ is"
105    return problem, f'${area}$'

Area of Circle given center and a point on circle

Ex. Problem Ex. Solution
Area of circle with center $(7,-6)$ and passing through $(1.0, -6.0)$ is $113.1$
def area_of_trapezoid(max_len=20):
108def area_of_trapezoid(max_len=20):
109    r"""Area of Trapezoid
110
111    | Ex. Problem | Ex. Solution |
112    | --- | --- |
113    | Area of trapezoid with height $12$ and base lengths: $3, 7, = $ | $60$ |
114    """
115    a = random.randint(1, max_len)
116    b = random.randint(1, max_len)
117    h = random.randint(1, max_len)
118
119    area = (a + b) * h / 2
120
121    problem = f"Area of trapezoid with height ${h}$ and base lengths: ${a}, {b}, = $"
122    solution = f'${round(area, 2)}$'
123    return problem, solution

Area of Trapezoid

Ex. Problem Ex. Solution
Area of trapezoid with height $12$ and base lengths: $3, 7, = $ $60$
def area_of_triangle(max_a=20, max_b=20):
126def area_of_triangle(max_a=20, max_b=20):
127    r"""Area of Triangle
128
129    | Ex. Problem | Ex. Solution |
130    | --- | --- |
131    | Area of triangle with side lengths: $8, 1, 8 = $ | $3.99$ |
132    """
133    a = random.randint(1, max_a)
134    b = random.randint(1, max_b)
135    c = random.randint(abs(b - a) + 1, abs(a + b) - 1)
136
137    s = (a + b + c) / 2
138    area = (s * (s - a) * (s - b) * (s - c))**0.5
139
140    problem = f"Area of triangle with side lengths: ${a}, {b}, {c} = $"
141    solution = f'${round(area, 2)}$'
142    return problem, solution

Area of Triangle

Ex. Problem Ex. Solution
Area of triangle with side lengths: $8, 1, 8 = $ $3.99$
def basic_trigonometry(angles=[0, 30, 45, 60, 90], functions=['sin', 'cos', 'tan']):
146def basic_trigonometry(angles=[0, 30, 45, 60, 90],
147                       functions=["sin", "cos", "tan"]):
148    r"""Trigonometric Values
149
150    | Ex. Problem | Ex. Solution |
151    | --- | --- |
152    | $\sin(30) = $ | $\frac{1}{2}$ |
153    """
154    angle = random.choice(angles)
155    function = random.choice(functions)
156
157    problem = rf"$\{function}({angle}) = $"
158
159    expression = 'math.' + function + '(math.radians(angle))'
160    result_fraction_map = {
161        0.0: "0",
162        0.5: r"\frac{1}{2}",
163        0.71: r"\frac{1}{\sqrt{2}}",
164        0.87: r"\frac{\sqrt{3}}{2}",
165        1.0: "1",
166        0.58: r"\frac{1}{\sqrt{3}}",
167        1.73: r"\sqrt{3}",
168    }
169
170    solution = result_fraction_map[round(
171        eval(expression),
172        2)] if round(eval(expression),
173                     2) <= 99999 else r"\infty"  # for handling the ∞ condition
174
175    return problem, f'${solution}$'

Trigonometric Values

Ex. Problem Ex. Solution
$\sin(30) = $ $\frac{1}{2}$
def circumference(max_radius=100):
178def circumference(max_radius=100):
179    r"""Circumference of Circle
180
181    | Ex. Problem | Ex. Solution |
182    | --- | --- |
183    | Circumference of circle with radius $56 = $ | $351.86$ |
184    """
185    r = random.randint(0, max_radius)
186    circumference = round(2 * math.pi * r, 2)
187
188    problem = f"Circumference of circle with radius ${r} = $"
189    return problem, f'${circumference}$'

Circumference of Circle

Ex. Problem Ex. Solution
Circumference of circle with radius $56 = $ $351.86$
def complementary_and_supplementary_angle(max_supp=180, max_comp=90):
192def complementary_and_supplementary_angle(max_supp=180, max_comp=90):
193    r"""Complementary and Supplementary Angle
194
195    | Ex. Problem | Ex. Solution |
196    | --- | --- |
197    | The complementary angle of $15 =$ | $75$ |
198    """
199    angleType = random.choice(["supplementary", "complementary"])
200
201    if angleType == "supplementary":
202        angle = random.randint(1, max_supp)
203        angleAns = 180 - angle
204    else:
205        angle = random.randint(1, max_comp)
206        angleAns = 90 - angle
207
208    problem = f"The {angleType} angle of ${angle} =$"
209    solution = f'${angleAns}$'
210    return problem, solution

Complementary and Supplementary Angle

Ex. Problem Ex. Solution
The complementary angle of $15 =$ $75$
def curved_surface_area_cylinder(max_radius=49, max_height=99):
213def curved_surface_area_cylinder(max_radius=49, max_height=99):
214    r"""Curved surface area of a cylinder
215
216    | Ex. Problem | Ex. Solution |
217    | --- | --- |
218    | What is the curved surface area of a cylinder of radius, $44$ and height, $92$? | $25434.33$ |
219    """
220    r = random.randint(1, max_radius)
221    h = random.randint(1, max_height)
222    csa = float(2 * math.pi * r * h)
223    formatted_float = round(csa, 2)  # "{:.5f}".format(csa)
224
225    problem = f"What is the curved surface area of a cylinder of radius, ${r}$ and height, ${h}$?"
226    solution = f"${formatted_float}$"
227    return problem, solution

Curved surface area of a cylinder

Ex. Problem Ex. Solution
What is the curved surface area of a cylinder of radius, $44$ and height, $92$? $25434.33$
def degree_to_rad(max_deg=360):
230def degree_to_rad(max_deg=360):
231    r"""Degrees to Radians
232
233    | Ex. Problem | Ex. Solution |
234    | --- | --- |
235    | Angle $113$ degrees in radians is: | $1.97$ |
236    """
237    a = random.randint(0, max_deg)
238    b = (math.pi * a) / 180
239    b = round(b, 2)
240
241    problem = f"Angle ${a}$ degrees in radians is: "
242    solution = f'${b}$'
243    return problem, solution

Degrees to Radians

Ex. Problem Ex. Solution
Angle $113$ degrees in radians is: $1.97$
def equation_of_line_from_two_points(max_coordinate=20, min_coordinate=-20):
246def equation_of_line_from_two_points(max_coordinate=20, min_coordinate=-20):
247    r"""Equation of line from two points
248
249    | Ex. Problem | Ex. Solution |
250    | --- | --- |
251    | What is the equation of the line between points $(13,9)$ and $(6,-19)$ in slope-intercept form? | $y = 4x -43$ |
252    """
253    x1 = random.randint(min_coordinate, max_coordinate)
254    x2 = random.randint(min_coordinate, max_coordinate)
255
256    y1 = random.randint(min_coordinate, max_coordinate)
257    y2 = random.randint(min_coordinate, max_coordinate)
258
259    coeff_y = (x2 - x1)
260    coeff_x = (y2 - y1)
261    constant = y2 * coeff_y - x2 * coeff_x
262
263    gcd = math.gcd(abs(coeff_x), abs(coeff_y))
264
265    if gcd != 1:
266        if coeff_y > 0:
267            coeff_y //= gcd
268        if coeff_x > 0:
269            coeff_x //= gcd
270        if constant > 0:
271            constant //= gcd
272        if coeff_y < 0:
273            coeff_y = -(-coeff_y // gcd)
274        if coeff_x < 0:
275            coeff_x = -(-coeff_x // gcd)
276        if constant < 0:
277            constant = -(-constant // gcd)
278    if coeff_y < 0:
279        coeff_y = -(coeff_y)
280        coeff_x = -(coeff_x)
281        constant = -(constant)
282    if coeff_x in [1, -1]:
283        if coeff_x == 1:
284            coeff_x = ''
285        else:
286            coeff_x = '-'
287    if coeff_y in [1, -1]:
288        if coeff_y == 1:
289            coeff_y = ''
290        else:
291            coeff_y = '-'
292
293    problem = f"What is the equation of the line between points $({x1},{y1})$ and $({x2},{y2})$ in slope-intercept form?"
294    if coeff_x == 0:
295        solution = str(coeff_y) + "y = " + str(constant)
296    elif coeff_y == 0:
297        solution = str(coeff_x) + "x = " + str(-constant)
298    else:
299        if constant > 0:
300            solution = str(coeff_y) + "y = " + str(coeff_x) + \
301                "x + " + str(constant)
302        else:
303            solution = str(coeff_y) + "y = " + \
304                str(coeff_x) + "x " + str(constant)
305    return problem, f'${solution}$'

Equation of line from two points

Ex. Problem Ex. Solution
What is the equation of the line between points $(13,9)$ and $(6,-19)$ in slope-intercept form? $y = 4x -43$
def fourth_angle_of_quadrilateral(max_angle=180):
308def fourth_angle_of_quadrilateral(max_angle=180):
309    r"""Fourth Angle of Quadrilateral
310
311    | Ex. Problem | Ex. Solution |
312    | --- | --- |
313    | Fourth angle of quadrilateral with angles $162 , 43, 78 =$ | $77$ |
314    """
315    angle1 = random.randint(1, max_angle)
316    angle2 = random.randint(1, 240 - angle1)
317    angle3 = random.randint(1, 340 - (angle1 + angle2))
318
319    sum_ = angle1 + angle2 + angle3
320    angle4 = 360 - sum_
321
322    problem = f"Fourth angle of quadrilateral with angles ${angle1} , {angle2}, {angle3} =$"
323    solution = f'${angle4}$'
324    return problem, solution

Fourth Angle of Quadrilateral

Ex. Problem Ex. Solution
Fourth angle of quadrilateral with angles $162 , 43, 78 =$ $77$
def perimeter_of_polygons(max_sides=12, max_length=120):
327def perimeter_of_polygons(max_sides=12, max_length=120):
328    r"""Perimeter of Polygons
329
330    | Ex. Problem | Ex. Solution |
331    | --- | --- |
332    | The perimeter of a $4$ sided polygon with lengths of $30, 105, 78, 106$cm is: | $319$ |
333    """
334    size_of_sides = random.randint(3, max_sides)
335    sides = [random.randint(1, max_length) for _ in range(size_of_sides)]
336
337    problem = f"The perimeter of a ${size_of_sides}$ sided polygon with lengths of ${', '.join(map(str, sides))}$cm is: "
338    solution = sum(sides)
339
340    return problem, f'${solution}$'

Perimeter of Polygons

Ex. Problem Ex. Solution
The perimeter of a $4$ sided polygon with lengths of $30, 105, 78, 106$cm is: $319$
def pythagorean_theorem(max_length=20):
343def pythagorean_theorem(max_length=20):
344    """Pythagorean Theorem
345
346    | Ex. Problem | Ex. Solution |
347    | --- | --- |
348    | What is the hypotenuse of a right triangle given the other two sides have lengths $9$ and $10$? | $13.45$ |
349    """
350    a = random.randint(1, max_length)
351    b = random.randint(1, max_length)
352    c = round((a**2 + b**2)**0.5, 2)
353
354    problem = f"What is the hypotenuse of a right triangle given the other two sides have lengths ${a}$ and ${b}$?"
355    solution = f"${c}$"
356    return problem, solution

Pythagorean Theorem

Ex. Problem Ex. Solution
What is the hypotenuse of a right triangle given the other two sides have lengths $9$ and $10$? $13.45$
def radian_to_deg(max_rad=6.28):
359def radian_to_deg(max_rad=6.28):
360    """Radians to Degrees"""
361    a = random.randint(0, int(max_rad * 100)) / 100
362    b = round((180 * a) / math.pi, 2)
363
364    problem = f"Angle ${a}$ radians in degrees is: "
365    solution = f'${b}$'
366    return problem, solution

Radians to Degrees

def sector_area(max_radius=49, max_angle=359):
369def sector_area(max_radius=49, max_angle=359):
370    """Area of a Sector
371
372    | Ex. Problem | Ex. Solution |
373    | --- | --- |
374    | What is the area of a sector with radius $42$ and angle $83$ degrees? | $1277.69$ |
375    """
376    r = random.randint(1, max_radius)
377    a = random.randint(1, max_angle)
378    secArea = float((a / 360) * math.pi * r * r)
379    formatted_float = round(secArea, 2)
380
381    problem = f"What is the area of a sector with radius ${r}$ and angle ${a}$ degrees?"
382    solution = f"${formatted_float}$"
383    return problem, solution

Area of a Sector

Ex. Problem Ex. Solution
What is the area of a sector with radius $42$ and angle $83$ degrees? $1277.69$
def sum_of_polygon_angles(max_sides=12):
386def sum_of_polygon_angles(max_sides=12):
387    """Sum of Angles of Polygon
388
389    | Ex. Problem | Ex. Solution |
390    | --- | --- |
391    | What is the sum of interior angles of a polygon with $8$ sides? | $1080$ |
392    """
393    side_count = random.randint(3, max_sides)
394    sum = (side_count - 2) * 180
395
396    problem = f"What is the sum of interior angles of a polygon with ${side_count}$ sides?"
397    return problem, f'${sum}$'

Sum of Angles of Polygon

Ex. Problem Ex. Solution
What is the sum of interior angles of a polygon with $8$ sides? $1080$
def surface_area_cone(max_radius=20, max_height=50, unit='m'):
400def surface_area_cone(max_radius=20, max_height=50, unit='m'):
401    """Surface area of a cone
402
403    | Ex. Problem | Ex. Solution |
404    | --- | --- |
405    | Surface area of cone with height $= 26m$ and radius $= 6m$ is | $616 m^2$ |
406    """
407    a = random.randint(1, max_height)
408    b = random.randint(1, max_radius)
409
410    slopingHeight = math.sqrt(a**2 + b**2)
411    ans = int(math.pi * b * slopingHeight + math.pi * b * b)
412
413    problem = f"Surface area of cone with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
414    solution = f"${ans} {unit}^2$"
415    return problem, solution

Surface area of a cone

Ex. Problem Ex. Solution
Surface area of cone with height $= 26m$ and radius $= 6m$ is $616 m^2$
def surface_area_cube(max_side=20, unit='m'):
418def surface_area_cube(max_side=20, unit='m'):
419    """Surface area of a cube
420
421    | Ex. Problem | Ex. Solution |
422    | --- | --- |
423    | Surface area of cube with side $= 6m$ is | $216 m^2$ |
424    """
425    a = random.randint(1, max_side)
426    ans = 6 * (a**2)
427
428    problem = f"Surface area of cube with side $= {a}{unit}$ is"
429    solution = f"${ans} {unit}^2$"
430    return problem, solution

Surface area of a cube

Ex. Problem Ex. Solution
Surface area of cube with side $= 6m$ is $216 m^2$
def surface_area_cuboid(max_side=20, unit='m'):
433def surface_area_cuboid(max_side=20, unit='m'):
434    """Surface area of a cuboid
435
436    | Ex. Problem | Ex. Solution |
437    | --- | --- |
438    | Surface area of cuboid with sides of lengths: $11m, 20m, 8m$ is | $936 m^2$ |
439    """
440    a = random.randint(1, max_side)
441    b = random.randint(1, max_side)
442    c = random.randint(1, max_side)
443    ans = 2 * (a * b + b * c + c * a)
444
445    problem = f"Surface area of cuboid with sides of lengths: ${a}{unit}, {b}{unit}, {c}{unit}$ is"
446    solution = f"${ans} {unit}^2$"
447    return problem, solution

Surface area of a cuboid

Ex. Problem Ex. Solution
Surface area of cuboid with sides of lengths: $11m, 20m, 8m$ is $936 m^2$
def surface_area_cylinder(max_radius=20, max_height=50, unit='m'):
450def surface_area_cylinder(max_radius=20, max_height=50, unit='m'):
451    """Surface area of a cylinder
452
453    | Ex. Problem | Ex. Solution |
454    | --- | --- |
455    | Surface area of cylinder with height $= 26m$ and radius $= 15m$ is | $3864 m^2$ |
456    """
457    a = random.randint(1, max_height)
458    b = random.randint(1, max_radius)
459    ans = int(2 * math.pi * a * b + 2 * math.pi * b * b)
460
461    problem = f"Surface area of cylinder with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
462    solution = f"${ans} {unit}^2$"
463    return problem, solution

Surface area of a cylinder

Ex. Problem Ex. Solution
Surface area of cylinder with height $= 26m$ and radius $= 15m$ is $3864 m^2$
def surface_area_pyramid(unit='m'):
466def surface_area_pyramid(unit='m'):
467    """Surface area of a pyramid
468
469    | Ex. Problem | Ex. Solution |
470    | --- | --- |
471    | Surface area of pyramid with base length $= 30m$, base width $= 40m$, and height $= 25m$ is | $2400 m^2$ |
472    """
473    # List of Pythagorean triplets
474    _PYTHAGOREAN = [(3, 4, 5), (6, 8, 10), (9, 12, 15), (12, 16, 20),
475                    (15, 20, 25), (5, 12, 13), (10, 24, 26), (7, 24, 25)]
476
477    # Generate first triplet
478    height, half_width, _ = random.sample(
479        random.choice(_PYTHAGOREAN), 3)
480    
481    # Calculate the first triangle height
482    triangle_height_1 = math.sqrt((half_width)**2 + height**2)
483
484    # Generate second triplet
485    second_triplet = random.choice([i for i in _PYTHAGOREAN if height in i])
486    half_length, _ = random.sample(
487        tuple(i for i in second_triplet if i != height), 2)
488    
489    # Calculate the second triangle height
490    triangle_height_2 = math.sqrt((half_length)**2 + height**2)
491    
492    # Calculate triangles area
493    triangle_1 = half_length * triangle_height_1
494    triangle_2 = half_width * triangle_height_2
495
496    # Calculate base area
497    base = 4 * half_width * half_length
498    ans = base + 2 * triangle_1 + 2 * triangle_2
499    
500    problem = f"Surface area of pyramid with base length $= {2*half_length}{unit}$, base width $= {2*half_width}{unit}$, and height $= {height}{unit}$ is"
501    solution = f"${ans} {unit}^2$"
502    return problem, solution

Surface area of a pyramid

Ex. Problem Ex. Solution
Surface area of pyramid with base length $= 30m$, base width $= 40m$, and height $= 25m$ is $2400 m^2$
def surface_area_sphere(max_side=20, unit='m'):
505def surface_area_sphere(max_side=20, unit='m'):
506    """Surface area of a sphere
507
508    | Ex. Problem | Ex. Solution |
509    | --- | --- |
510    | Surface area of a sphere with radius $= 8m$ is | $804.25 m^2$ |
511    """
512    r = random.randint(1, max_side)
513    ans = round(4 * math.pi * r * r, 2)
514
515    problem = f"Surface area of a sphere with radius $= {r}{unit}$ is"
516    solution = f"${ans} {unit}^2$"
517    return problem, solution

Surface area of a sphere

Ex. Problem Ex. Solution
Surface area of a sphere with radius $= 8m$ is $804.25 m^2$
def third_angle_of_triangle(max_angle=89):
520def third_angle_of_triangle(max_angle=89):
521    """Third Angle of Triangle
522
523    | Ex. Problem | Ex. Solution |
524    | --- | --- |
525    | Third angle of triangle with angles $10$ and $22 =$ | $148$ |
526    """
527    angle1 = random.randint(1, max_angle)
528    angle2 = random.randint(1, max_angle)
529    angle3 = 180 - (angle1 + angle2)
530
531    problem = f"Third angle of triangle with angles ${angle1}$ and ${angle2} = $"
532    return problem, f'${angle3}$'

Third Angle of Triangle

Ex. Problem Ex. Solution
Third angle of triangle with angles $10$ and $22 =$ $148$
def valid_triangle(max_side_length=50):
535def valid_triangle(max_side_length=50):
536    """Valid Triangle
537
538    | Ex. Problem | Ex. Solution |
539    | --- | --- |
540    | Does triangel with sides $10, 31$ and $14$ exist? | No |
541    """
542    sideA = random.randint(1, max_side_length)
543    sideB = random.randint(1, max_side_length)
544    sideC = random.randint(1, max_side_length)
545
546    sideSums = [sideA + sideB, sideB + sideC, sideC + sideA]
547    sides = [sideC, sideA, sideB]
548
549    exists = True & (sides[0] < sideSums[0]) & (sides[1] < sideSums[1]) & (
550        sides[2] < sideSums[2])
551
552    problem = f"Does triangle with sides ${sideA}, {sideB}$ and ${sideC}$ exist?"
553    solution = "Yes" if exists else "No"
554    return problem, f'${solution}$'

Valid Triangle

Ex. Problem Ex. Solution
Does triangel with sides $10, 31$ and $14$ exist? No
def volume_cone(max_radius=20, max_height=50, unit='m'):
557def volume_cone(max_radius=20, max_height=50, unit='m'):
558    """Volume of a cone
559
560    | Ex. Problem | Ex. Solution |
561    | --- | --- |
562    | Volume of cone with height $= 44m$ and radius $= 11m$ is | $5575 m^3$ |
563    """
564    a = random.randint(1, max_height)
565    b = random.randint(1, max_radius)
566    ans = int(math.pi * b * b * a * (1 / 3))
567
568    problem = f"Volume of cone with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
569    solution = f"${ans} {unit}^3$"
570    return problem, solution

Volume of a cone

Ex. Problem Ex. Solution
Volume of cone with height $= 44m$ and radius $= 11m$ is $5575 m^3$
def volume_cube(max_side=20, unit='m'):
573def volume_cube(max_side=20, unit='m'):
574    """Volume of a cube
575
576    | Ex. Problem | Ex. Solution |
577    | --- | --- |
578    | Volume of a cube with a side length of $19m$ is | $6859 m^3$ |
579    """
580    a = random.randint(1, max_side)
581    ans = a**3
582
583    problem = f"Volume of cube with a side length of ${a}{unit}$ is"
584    solution = f"${ans} {unit}^3$"
585    return problem, solution

Volume of a cube

Ex. Problem Ex. Solution
Volume of a cube with a side length of $19m$ is $6859 m^3$
def volume_cuboid(max_side=20, unit='m'):
588def volume_cuboid(max_side=20, unit='m'):
589    """Volume of a cuboid
590
591    | Ex. Problem | Ex. Solution |
592    | --- | --- |
593    | Volume of cuboid with sides = $17m, 11m, 13m$ is | $2431 m^3$ |
594    """
595    a = random.randint(1, max_side)
596    b = random.randint(1, max_side)
597    c = random.randint(1, max_side)
598    ans = a * b * c
599
600    problem = f"Volume of cuboid with sides = ${a}{unit}, {b}{unit}, {c}{unit}$ is"
601    solution = f"${ans} {unit}^3$"
602    return problem, solution

Volume of a cuboid

Ex. Problem Ex. Solution
Volume of cuboid with sides = $17m, 11m, 13m$ is $2431 m^3$
def volume_cylinder(max_radius=20, max_height=50, unit='m'):
605def volume_cylinder(max_radius=20, max_height=50, unit='m'):
606    """Volume of a cylinder
607
608    | Ex. Problem | Ex. Solution |
609    | --- | --- |
610    | Volume of cylinder with height $= 3m$ and radius $= 10m$ is | $942 m^3$ |
611    """
612    a = random.randint(1, max_height)
613    b = random.randint(1, max_radius)
614    ans = int(math.pi * b * b * a)
615
616    problem = f"Volume of cylinder with height $= {a}{unit}$ and radius $= {b}{unit}$ is"
617    solution = f"${ans} {unit}^3$"
618    return problem, solution

Volume of a cylinder

Ex. Problem Ex. Solution
Volume of cylinder with height $= 3m$ and radius $= 10m$ is $942 m^3$
def volume_cone_frustum(max_r1=20, max_r2=20, max_height=50, unit='m'):
621def volume_cone_frustum(max_r1=20, max_r2=20, max_height=50, unit='m'):
622    """Volume of the frustum of a cone
623
624    | Ex. Problem | Ex. Solution |
625    | --- | --- |
626    | Volume of frustum with height $= 30m$ and $r1 = 20m$ is and $r2 = 8m$ is | $19603.54 m^3$ |
627    """
628    h = random.randint(1, max_height)
629    r1 = random.randint(1, max_r1)
630    r2 = random.randint(1, max_r2)
631    ans = round(((math.pi * h) * (r1**2 + r2**2 + r1 * r2)) / 3, 2)
632
633    problem = f"Volume of frustum with height $= {h}{unit}$ and $r1 = {r1}{unit}$ is and $r2 = {r2}{unit}$ is "
634    solution = f"${ans} {unit}^3$"
635    return problem, solution

Volume of the frustum of a cone

Ex. Problem Ex. Solution
Volume of frustum with height $= 30m$ and $r1 = 20m$ is and $r2 = 8m$ is $19603.54 m^3$
def volume_hemisphere(max_radius=100):
638def volume_hemisphere(max_radius=100):
639    """Volume of a hemisphere
640
641    | Ex. Problem | Ex. Solution |
642    | --- | --- |
643    | Volume of hemisphere with radius $32m =$ | $68629.14 m^3$ |
644    """
645    r = random.randint(1, max_radius)
646    ans = round((2 * math.pi / 3) * r**3, 2)
647
648    problem = f"Volume of hemisphere with radius ${r} m =$ "
649    solution = f"${ans} m^3$"
650    return problem, solution

Volume of a hemisphere

Ex. Problem Ex. Solution
Volume of hemisphere with radius $32m =$ $68629.14 m^3$
def volume_pyramid(max_length=20, max_width=20, max_height=50, unit='m'):
653def volume_pyramid(max_length=20, max_width=20, max_height=50, unit='m'):
654    """Volume of a pyramid
655
656    | Ex. Problem | Ex. Solution |
657    | --- | --- |
658    | Volume of pyramid with base length $= 7 m$, base width $= 18 m$ and height $= 42 m$ is | $1764.0 m^3$ |
659    """
660    length = random.randint(1, max_length)
661    width = random.randint(1, max_width)
662    height = random.randint(1, max_height)
663
664    ans = round((length * width * height) / 3, 2)
665
666    problem = f"Volume of pyramid with base length $= {length} {unit}$, base width $= {width} {unit}$ and height $= {height} {unit}$ is"
667    solution = f"${ans} {unit}^3$"
668    return problem, solution

Volume of a pyramid

Ex. Problem Ex. Solution
Volume of pyramid with base length $= 7 m$, base width $= 18 m$ and height $= 42 m$ is $1764.0 m^3$
def volume_sphere(max_radius=100):
671def volume_sphere(max_radius=100):
672    """Volume of a sphere
673
674    | Ex. Problem | Ex. Solution |
675    | --- | --- |
676    | Volume of sphere with radius $30 m = $ | $113097.36 m^3$ |
677    """
678    r = random.randint(1, max_radius)
679    ans = round((4 * math.pi / 3) * r**3, 2)
680
681    problem = f"Volume of sphere with radius ${r} m = $"
682    solution = f"${ans} m^3$"
683    return problem, solution

Volume of a sphere

Ex. Problem Ex. Solution
Volume of sphere with radius $30 m = $ $113097.36 m^3$