diff --git a/docs/mathgenerator/_gen_list.html b/docs/mathgenerator/_gen_list.html index 829750c..5fe8783 100644 --- a/docs/mathgenerator/_gen_list.html +++ b/docs/mathgenerator/_gen_list.html @@ -216,7 +216,9 @@ 138 ("resistivity", "physics"), 139 ("fringe_spacing", "physics"), 140 ("lba_to_chs", "computer_science"), -141] +141 ("floating_point_binary_to_decimal", "computer_science"), +142 ("electric_field_strength_two_points", "physics"), +143] @@ -225,7 +227,7 @@
1import random 2import math 3 - 4def binary_addition(max_sum=256, max_addend=128): - 5 r"""Binary Addition + 4def floating_point_binary_to_decimal(mantissa_length=8, exponent_length=4): + 5 r"""Floating Point Binary to Decimal 6 7 | Ex. Problem | Ex. Solution | 8 | --- | --- | - 9 | In binary, calculate: $101 + 110110 = $ | $111011$ | - 10 """ - 11 if max_addend > max_sum: - 12 max_addend = max_sum - 13 a = random.randint(0, max_addend) - 14 b = random.randint(0, min((max_sum - a), max_addend)) - 15 c = a + b - 16 - 17 problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ " - 18 solution = f'${bin(c).replace('0b', "")}$' - 19 return problem, solution - 20 - 21def bcd_to_decimal(max_number=10000): - 22 r"""Binary Coded Decimal to Integer - 23 - 24 | Ex. Problem | Ex. Solution | - 25 | --- | --- | - 26 | Integer of Binary Coded Decimal $4 =$ | $17801$ | - 27 """ - 28 n = random.randint(1000, max_number) - 29 binstring = '' - 30 while True: - 31 q, r = divmod(n, 10) - 32 nibble = bin(r).replace('0b', "") - 33 while len(nibble) < 4: - 34 nibble = '0' + nibble - 35 binstring = nibble + binstring - 36 if q == 0: - 37 break - 38 else: - 39 n = q - 40 - 41 problem = f"Integer of Binary Coded Decimal ${n} =$ " - 42 solution = f'${int(binstring, 2)}$' - 43 return problem, solution + 9 | There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long | $011100000010 = 3.5$ | + 10 + 11 """ + 12 # TODO Could do with some work ensuring the number is not too small or too big. + 13 mantissa = '' + 14 for x in range(mantissa_length): + 15 mantissa += str(random.randint(0,1)) + 16 exponent = '' + 17 for x in range(exponent_length): + 18 exponent += str(random.randint(0,1)) + 19 decimal_mantissa = int(mantissa, 2) + 20 if (decimal_mantissa & (1 << (mantissa_length - 1))) != 0: + 21 decimal_mantissa = decimal_mantissa - (1 << mantissa_length) + 22 decimal_exponent = int(exponent, 2) + 23 if (decimal_exponent & (1 << (exponent_length - 1))) != 0: + 24 decimal_exponent = decimal_exponent - (1 << exponent_length) + 25 answer = decimal_mantissa * 2**(decimal_exponent - (mantissa_length - 1)) + 26 + 27 problem = f"There is a floating point binary number ${mantissa}{exponent}$ where the signed mantissa is {mantissa_length} bits long and the signed exponent is {exponent_length} bits long" + 28 solution = f'${mantissa}{exponent} = {answer}$' + 29 + 30 return problem, solution + 31 + 32def binary_addition(max_sum=256, max_addend=128): + 33 r"""Binary Addition + 34 + 35 | Ex. Problem | Ex. Solution | + 36 | --- | --- | + 37 | In binary, calculate: $101 + 110110 = $ | $111011$ | + 38 """ + 39 if max_addend > max_sum: + 40 max_addend = max_sum + 41 a = random.randint(0, max_addend) + 42 b = random.randint(0, min((max_sum - a), max_addend)) + 43 c = a + b 44 - 45 - 46def binary_2s_complement(maxDigits=10): - 47 r"""Binary 2's Complement + 45 problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ " + 46 solution = f'${bin(c).replace('0b', "")}$' + 47 return problem, solution 48 - 49 | Ex. Problem | Ex. Solution | - 50 | --- | --- | - 51 | 2's complement of $1011 = $ | $101$ | - 52 """ - 53 digits = random.randint(1, maxDigits) - 54 question = ''.join([str(random.randint(0, 1)) - 55 for i in range(digits)]).lstrip('0') - 56 - 57 answer = [] - 58 for i in question: - 59 answer.append(str(int(not bool(int(i))))) - 60 - 61 carry = True - 62 j = len(answer) - 1 - 63 while j >= 0: - 64 if answer[j] == '0': - 65 answer[j] = '1' - 66 carry = False - 67 break - 68 answer[j] = '0' - 69 j -= 1 - 70 - 71 if j == 0 and carry is True: - 72 answer.insert(0, '1') + 49def bcd_to_decimal(max_number=10000): + 50 r"""Binary Coded Decimal to Integer + 51 + 52 | Ex. Problem | Ex. Solution | + 53 | --- | --- | + 54 | Integer of Binary Coded Decimal $4 =$ | $17801$ | + 55 """ + 56 n = random.randint(1000, max_number) + 57 binstring = '' + 58 while True: + 59 q, r = divmod(n, 10) + 60 nibble = bin(r).replace('0b', "") + 61 while len(nibble) < 4: + 62 nibble = '0' + nibble + 63 binstring = nibble + binstring + 64 if q == 0: + 65 break + 66 else: + 67 n = q + 68 + 69 problem = f"Integer of Binary Coded Decimal ${n} =$ " + 70 solution = f'${int(binstring, 2)}$' + 71 return problem, solution + 72 73 - 74 problem = f"2's complement of ${question} = $" - 75 solution = ''.join(answer).lstrip('0') - 76 return problem, f'${solution}$' - 77 - 78 - 79def binary_complement_1s(maxDigits=10): - 80 r"""Binary Complement 1s - 81 - 82 | Ex. Problem | Ex. Solution | - 83 | --- | --- | - 84 | $1111001 = $ | $0000110$ | - 85 """ - 86 question = ''.join([ - 87 str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits)) - 88 ]) - 89 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) - 90 - 91 problem = f'${question} = $' - 92 return problem, f'${answer}$' - 93 - 94 - 95def binary_to_decimal(max_dig=10): - 96 r"""Binary to Decimal - 97 - 98 | Ex. Problem | Ex. Solution | - 99 | --- | --- | -100 | $000110$ | $6$ | -101 """ -102 problem = ''.join( -103 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) -104 solution = f'${int(problem, 2)}$' -105 return f'${problem}$', solution + 74def binary_2s_complement(maxDigits=10): + 75 r"""Binary 2's Complement + 76 + 77 | Ex. Problem | Ex. Solution | + 78 | --- | --- | + 79 | 2's complement of $1011 = $ | $101$ | + 80 """ + 81 digits = random.randint(1, maxDigits) + 82 question = ''.join([str(random.randint(0, 1)) + 83 for i in range(digits)]).lstrip('0') + 84 + 85 answer = [] + 86 for i in question: + 87 answer.append(str(int(not bool(int(i))))) + 88 + 89 carry = True + 90 j = len(answer) - 1 + 91 while j >= 0: + 92 if answer[j] == '0': + 93 answer[j] = '1' + 94 carry = False + 95 break + 96 answer[j] = '0' + 97 j -= 1 + 98 + 99 if j == 0 and carry is True: +100 answer.insert(0, '1') +101 +102 problem = f"2's complement of ${question} = $" +103 solution = ''.join(answer).lstrip('0') +104 return problem, f'${solution}$' +105 106 -107 -108def binary_to_hex(max_dig=10): -109 r"""Binary to Hexidecimal -110 -111 | Ex. Problem | Ex. Solution | -112 | --- | --- | -113 | $010101$ | $0x15$ | -114 """ -115 problem = ''.join( -116 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) -117 solution = f'${hex(int(problem, 2))}$' -118 return f'${problem}$', solution -119 -120 -121def decimal_to_bcd(max_number=10000): -122 r"""Decimal to Binary Coded Decimal -123 -124 | Ex. Problem | Ex. Solution | -125 | --- | --- | -126 | BCD of Decimal Number $6575 = $ | $191015$ | -127 """ -128 n = random.randint(1000, max_number) -129 x = n -130 # binstring = '' -131 bcdstring = '' -132 while x > 0: -133 nibble = x % 16 -134 bcdstring = str(nibble) + bcdstring -135 x >>= 4 -136 -137 problem = f"BCD of Decimal Number ${n} = $" -138 return problem, f'${bcdstring}$' -139 -140 -141def decimal_to_binary(max_dec=99): -142 r"""Decimal to Binary -143 -144 | Ex. Problem | Ex. Solution | -145 | --- | --- | -146 | Binary of $4 = $ | $100$ | -147 """ -148 a = random.randint(1, max_dec) -149 b = bin(a).replace("0b", "") -150 -151 problem = f'Binary of ${a} = $' -152 solution = f'${b}$' -153 return problem, solution -154 -155 -156def decimal_to_hexadeci(max_dec=1000): -157 r"""Decimal to Hexadecimal -158 -159 | Ex. Problem | Ex. Solution | -160 | --- | --- | -161 | Hexadecimal of $410 = $ | $0x19a$ | -162 """ -163 a = random.randint(0, max_dec) -164 b = hex(a) -165 -166 problem = f"Hexadecimal of ${a} = $" -167 solution = f"${b}$" -168 return problem, solution -169 -170 -171def decimal_to_octal(max_decimal=4096): -172 r"""Decimal to Octal -173 -174 | Ex. Problem | Ex. Solution | -175 | --- | --- | -176 | The decimal number $3698$ in octal is: | $0o7162$ | -177 """ -178 x = random.randint(0, max_decimal) -179 -180 problem = f"The decimal number ${x}$ in octal is: " -181 solution = f'${oct(x)}$' +107def binary_complement_1s(maxDigits=10): +108 r"""Binary Complement 1s +109 +110 | Ex. Problem | Ex. Solution | +111 | --- | --- | +112 | $1111001 = $ | $0000110$ | +113 """ +114 question = ''.join([ +115 str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits)) +116 ]) +117 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) +118 +119 problem = f'${question} = $' +120 return problem, f'${answer}$' +121 +122 +123def binary_to_decimal(max_dig=10): +124 r"""Binary to Decimal +125 +126 | Ex. Problem | Ex. Solution | +127 | --- | --- | +128 | $000110$ | $6$ | +129 """ +130 problem = ''.join( +131 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) +132 solution = f'${int(problem, 2)}$' +133 return f'${problem}$', solution +134 +135 +136def binary_to_hex(max_dig=10): +137 r"""Binary to Hexidecimal +138 +139 | Ex. Problem | Ex. Solution | +140 | --- | --- | +141 | $010101$ | $0x15$ | +142 """ +143 problem = ''.join( +144 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) +145 solution = f'${hex(int(problem, 2))}$' +146 return f'${problem}$', solution +147 +148 +149def decimal_to_bcd(max_number=10000): +150 r"""Decimal to Binary Coded Decimal +151 +152 | Ex. Problem | Ex. Solution | +153 | --- | --- | +154 | BCD of Decimal Number $6575 = $ | $191015$ | +155 """ +156 n = random.randint(1000, max_number) +157 x = n +158 # binstring = '' +159 bcdstring = '' +160 while x > 0: +161 nibble = x % 16 +162 bcdstring = str(nibble) + bcdstring +163 x >>= 4 +164 +165 problem = f"BCD of Decimal Number ${n} = $" +166 return problem, f'${bcdstring}$' +167 +168 +169def decimal_to_binary(max_dec=99): +170 r"""Decimal to Binary +171 +172 | Ex. Problem | Ex. Solution | +173 | --- | --- | +174 | Binary of $4 = $ | $100$ | +175 """ +176 a = random.randint(1, max_dec) +177 b = bin(a).replace("0b", "") +178 +179 problem = f'Binary of ${a} = $' +180 solution = f'${b}$' +181 return problem, solution 182 -183 return problem, solution -184 -185 -186def fibonacci_series(min_no=1): -187 r"""Fibonacci Series -188 -189 | Ex. Problem | Ex. Solution | -190 | --- | --- | -191 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | -192 """ -193 n = random.randint(min_no, 20) -194 -195 def createFibList(n): -196 list = [] -197 for i in range(n): -198 if i < 2: -199 list.append(i) -200 else: -201 val = list[i - 1] + list[i - 2] -202 list.append(val) -203 return list -204 -205 fibList = createFibList(n) -206 -207 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" -208 solution = ', '.join(map(str, fibList)) -209 return problem, f'${solution}$' +183 +184def decimal_to_hexadeci(max_dec=1000): +185 r"""Decimal to Hexadecimal +186 +187 | Ex. Problem | Ex. Solution | +188 | --- | --- | +189 | Hexadecimal of $410 = $ | $0x19a$ | +190 """ +191 a = random.randint(0, max_dec) +192 b = hex(a) +193 +194 problem = f"Hexadecimal of ${a} = $" +195 solution = f"${b}$" +196 return problem, solution +197 +198 +199def decimal_to_octal(max_decimal=4096): +200 r"""Decimal to Octal +201 +202 | Ex. Problem | Ex. Solution | +203 | --- | --- | +204 | The decimal number $3698$ in octal is: | $0o7162$ | +205 """ +206 x = random.randint(0, max_decimal) +207 +208 problem = f"The decimal number ${x}$ in octal is: " +209 solution = f'${oct(x)}$' 210 -211 -212def modulo_division(max_res=99, max_modulo=99): -213 r"""Modulo Division -214 -215 | Ex. Problem | Ex. Solution | -216 | --- | --- | -217 | $43$ % $33 = $ | $10$ | -218 """ -219 a = random.randint(0, max_modulo) -220 b = random.randint(0, min(max_res, max_modulo)) -221 c = a % b if b != 0 else 0 +211 return problem, solution +212 +213 +214def fibonacci_series(min_no=1): +215 r"""Fibonacci Series +216 +217 | Ex. Problem | Ex. Solution | +218 | --- | --- | +219 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | +220 """ +221 n = random.randint(min_no, 20) 222 -223 problem = f'${a}$ % ${b} = $' -224 solution = f'${c}$' -225 return problem, solution -226 -227 -228def nth_fibonacci_number(max_n=100): -229 r"""nth Fibonacci number -230 -231 | Ex. Problem | Ex. Solution | -232 | --- | --- | -233 | What is the 85th Fibonacci number? | $259695496911123328$ | -234 """ -235 gratio = (1 + math.sqrt(5)) / 2 -236 n = random.randint(1, max_n) -237 -238 problem = f"What is the {n}th Fibonacci number?" -239 solution = int( -240 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) -241 -242 return problem, f'${solution}$' -243 -244 -245def nth_tribonacci_number(min_length=1, max_length=80): -246 r"""nth Tribonacci number -247 -248 | Ex. Problem | Ex. Solution | -249 | --- | --- | -250 | What is the 14th Tribonacci number? | $504$ | -251 """ -252 -253 tribDict = {0: 0, 1: 0, 2: 1} +223 def createFibList(n): +224 list = [] +225 for i in range(n): +226 if i < 2: +227 list.append(i) +228 else: +229 val = list[i - 1] + list[i - 2] +230 list.append(val) +231 return list +232 +233 fibList = createFibList(n) +234 +235 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" +236 solution = ', '.join(map(str, fibList)) +237 return problem, f'${solution}$' +238 +239 +240def modulo_division(max_res=99, max_modulo=99): +241 r"""Modulo Division +242 +243 | Ex. Problem | Ex. Solution | +244 | --- | --- | +245 | $43$ % $33 = $ | $10$ | +246 """ +247 a = random.randint(0, max_modulo) +248 b = random.randint(0, min(max_res, max_modulo)) +249 c = a % b if b != 0 else 0 +250 +251 problem = f'${a}$ % ${b} = $' +252 solution = f'${c}$' +253 return problem, solution 254 -255 def recTrib(i): -256 if i not in tribDict: -257 tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3) -258 return tribDict[i] -259 -260 n = random.randint(min_length, max_length) -261 problem = f"What is the {n}th Tribonacci number?" -262 solution = recTrib(n - 1) -263 return problem, f'${solution}$' -264 +255 +256def nth_fibonacci_number(max_n=100): +257 r"""nth Fibonacci number +258 +259 | Ex. Problem | Ex. Solution | +260 | --- | --- | +261 | What is the 85th Fibonacci number? | $259695496911123328$ | +262 """ +263 gratio = (1 + math.sqrt(5)) / 2 +264 n = random.randint(1, max_n) 265 -266def tribonacci_series(min_length=1, max_length=80): -267 r"""Fibonacci Series -268 -269 | Ex. Problem | Ex. Solution | -270 | --- | --- | -271 | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ | -272 """ -273 -274 tribDict = {0: 0, 1: 0, 2: 1} +266 problem = f"What is the {n}th Fibonacci number?" +267 solution = int( +268 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) +269 +270 return problem, f'${solution}$' +271 +272 +273def nth_tribonacci_number(min_length=1, max_length=80): +274 r"""nth Tribonacci number 275 -276 def createTribSeries(i): -277 tribSeries = [] -278 for idx in range(i): -279 if idx not in tribDict: -280 tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3] -281 tribSeries.append(tribDict[idx]) -282 return tribSeries -283 -284 n = random.randint(min_length, max_length) -285 tribSeries = createTribSeries(n) -286 problem = "The Tribonacci Series of the first ${n}$ numbers is ?" -287 solution = ', '.join(map(str, tribSeries)) -288 return problem, f'${solution}$' -289 -290def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2): -291 r""" Calculating the Cylinder, Head and Sector numbers for a given LBA -292 | Ex. Problem | Ex. Solution | -293 | --- | --- | -294 | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ | -295 """ -296 lba = random.randint(0, max_lba) -297 cylinder = math.floor(lba/(sectors_per_track*number_of_heads)); -298 t = cylinder*(sectors_per_track*number_of_heads); -299 t = lba-t; -300 head = math.floor(t/(sectors_per_track)); -301 sector = t - (head * lba) + 1; -302 -303 problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?" -304 solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$" -305 return problem, solution +276 | Ex. Problem | Ex. Solution | +277 | --- | --- | +278 | What is the 14th Tribonacci number? | $504$ | +279 """ +280 +281 tribDict = {0: 0, 1: 0, 2: 1} +282 +283 def recTrib(i): +284 if i not in tribDict: +285 tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3) +286 return tribDict[i] +287 +288 n = random.randint(min_length, max_length) +289 problem = f"What is the {n}th Tribonacci number?" +290 solution = recTrib(n - 1) +291 return problem, f'${solution}$' +292 +293 +294def tribonacci_series(min_length=1, max_length=80): +295 r"""Fibonacci Series +296 +297 | Ex. Problem | Ex. Solution | +298 | --- | --- | +299 | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ | +300 """ +301 +302 tribDict = {0: 0, 1: 0, 2: 1} +303 +304 def createTribSeries(i): +305 tribSeries = [] +306 for idx in range(i): +307 if idx not in tribDict: +308 tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3] +309 tribSeries.append(tribDict[idx]) +310 return tribSeries +311 +312 n = random.randint(min_length, max_length) +313 tribSeries = createTribSeries(n) +314 problem = "The Tribonacci Series of the first ${n}$ numbers is ?" +315 solution = ', '.join(map(str, tribSeries)) +316 return problem, f'${solution}$' +317 +318def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2): +319 r""" Calculating the Cylinder, Head and Sector numbers for a given LBA +320 | Ex. Problem | Ex. Solution | +321 | --- | --- | +322 | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ | +323 """ +324 lba = random.randint(0, max_lba) +325 cylinder = math.floor(lba/(sectors_per_track*number_of_heads)); +326 t = cylinder*(sectors_per_track*number_of_heads); +327 t = lba-t; +328 head = math.floor(t/(sectors_per_track)); +329 sector = t - (head * lba) + 1; +330 +331 problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?" +332 solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$" +333 return problem, solution
5def floating_point_binary_to_decimal(mantissa_length=8, exponent_length=4): + 6 r"""Floating Point Binary to Decimal + 7 + 8 | Ex. Problem | Ex. Solution | + 9 | --- | --- | +10 | There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long | $011100000010 = 3.5$ | +11 +12 """ +13 # TODO Could do with some work ensuring the number is not too small or too big. +14 mantissa = '' +15 for x in range(mantissa_length): +16 mantissa += str(random.randint(0,1)) +17 exponent = '' +18 for x in range(exponent_length): +19 exponent += str(random.randint(0,1)) +20 decimal_mantissa = int(mantissa, 2) +21 if (decimal_mantissa & (1 << (mantissa_length - 1))) != 0: +22 decimal_mantissa = decimal_mantissa - (1 << mantissa_length) +23 decimal_exponent = int(exponent, 2) +24 if (decimal_exponent & (1 << (exponent_length - 1))) != 0: +25 decimal_exponent = decimal_exponent - (1 << exponent_length) +26 answer = decimal_mantissa * 2**(decimal_exponent - (mantissa_length - 1)) +27 +28 problem = f"There is a floating point binary number ${mantissa}{exponent}$ where the signed mantissa is {mantissa_length} bits long and the signed exponent is {exponent_length} bits long" +29 solution = f'${mantissa}{exponent} = {answer}$' +30 +31 return problem, solution +
Floating Point Binary to Decimal
+ +| Ex. Problem | +Ex. Solution | +
|---|---|
| There is a floating point binary number $011100000010$ where the signed mantissa is 8 bits long and the signed exponent is 4 bits long | +$011100000010 = 3.5$ | +
5def binary_addition(max_sum=256, max_addend=128): - 6 r"""Binary Addition - 7 - 8 | Ex. Problem | Ex. Solution | - 9 | --- | --- | -10 | In binary, calculate: $101 + 110110 = $ | $111011$ | -11 """ -12 if max_addend > max_sum: -13 max_addend = max_sum -14 a = random.randint(0, max_addend) -15 b = random.randint(0, min((max_sum - a), max_addend)) -16 c = a + b -17 -18 problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ " -19 solution = f'${bin(c).replace('0b', "")}$' -20 return problem, solution +@@ -491,29 +583,29 @@33def binary_addition(max_sum=256, max_addend=128): +34 r"""Binary Addition +35 +36 | Ex. Problem | Ex. Solution | +37 | --- | --- | +38 | In binary, calculate: $101 + 110110 = $ | $111011$ | +39 """ +40 if max_addend > max_sum: +41 max_addend = max_sum +42 a = random.randint(0, max_addend) +43 b = random.randint(0, min((max_sum - a), max_addend)) +44 c = a + b +45 +46 problem = f"In base 2, ${bin(a).replace('0b', "")} + {bin(b).replace('0b', "")} =$ " +47 solution = f'${bin(c).replace('0b', "")}$' +48 return problem, solution
22def bcd_to_decimal(max_number=10000): -23 r"""Binary Coded Decimal to Integer -24 -25 | Ex. Problem | Ex. Solution | -26 | --- | --- | -27 | Integer of Binary Coded Decimal $4 =$ | $17801$ | -28 """ -29 n = random.randint(1000, max_number) -30 binstring = '' -31 while True: -32 q, r = divmod(n, 10) -33 nibble = bin(r).replace('0b', "") -34 while len(nibble) < 4: -35 nibble = '0' + nibble -36 binstring = nibble + binstring -37 if q == 0: -38 break -39 else: -40 n = q -41 -42 problem = f"Integer of Binary Coded Decimal ${n} =$ " -43 solution = f'${int(binstring, 2)}$' -44 return problem, solution +@@ -548,37 +640,37 @@50def bcd_to_decimal(max_number=10000): +51 r"""Binary Coded Decimal to Integer +52 +53 | Ex. Problem | Ex. Solution | +54 | --- | --- | +55 | Integer of Binary Coded Decimal $4 =$ | $17801$ | +56 """ +57 n = random.randint(1000, max_number) +58 binstring = '' +59 while True: +60 q, r = divmod(n, 10) +61 nibble = bin(r).replace('0b', "") +62 while len(nibble) < 4: +63 nibble = '0' + nibble +64 binstring = nibble + binstring +65 if q == 0: +66 break +67 else: +68 n = q +69 +70 problem = f"Integer of Binary Coded Decimal ${n} =$ " +71 solution = f'${int(binstring, 2)}$' +72 return problem, solution
47def binary_2s_complement(maxDigits=10): -48 r"""Binary 2's Complement -49 -50 | Ex. Problem | Ex. Solution | -51 | --- | --- | -52 | 2's complement of $1011 = $ | $101$ | -53 """ -54 digits = random.randint(1, maxDigits) -55 question = ''.join([str(random.randint(0, 1)) -56 for i in range(digits)]).lstrip('0') -57 -58 answer = [] -59 for i in question: -60 answer.append(str(int(not bool(int(i))))) -61 -62 carry = True -63 j = len(answer) - 1 -64 while j >= 0: -65 if answer[j] == '0': -66 answer[j] = '1' -67 carry = False -68 break -69 answer[j] = '0' -70 j -= 1 -71 -72 if j == 0 and carry is True: -73 answer.insert(0, '1') -74 -75 problem = f"2's complement of ${question} = $" -76 solution = ''.join(answer).lstrip('0') -77 return problem, f'${solution}$' +@@ -613,20 +705,20 @@75def binary_2s_complement(maxDigits=10): + 76 r"""Binary 2's Complement + 77 + 78 | Ex. Problem | Ex. Solution | + 79 | --- | --- | + 80 | 2's complement of $1011 = $ | $101$ | + 81 """ + 82 digits = random.randint(1, maxDigits) + 83 question = ''.join([str(random.randint(0, 1)) + 84 for i in range(digits)]).lstrip('0') + 85 + 86 answer = [] + 87 for i in question: + 88 answer.append(str(int(not bool(int(i))))) + 89 + 90 carry = True + 91 j = len(answer) - 1 + 92 while j >= 0: + 93 if answer[j] == '0': + 94 answer[j] = '1' + 95 carry = False + 96 break + 97 answer[j] = '0' + 98 j -= 1 + 99 +100 if j == 0 and carry is True: +101 answer.insert(0, '1') +102 +103 problem = f"2's complement of ${question} = $" +104 solution = ''.join(answer).lstrip('0') +105 return problem, f'${solution}$'
80def binary_complement_1s(maxDigits=10): -81 r"""Binary Complement 1s -82 -83 | Ex. Problem | Ex. Solution | -84 | --- | --- | -85 | $1111001 = $ | $0000110$ | -86 """ -87 question = ''.join([ -88 str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits)) -89 ]) -90 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) -91 -92 problem = f'${question} = $' -93 return problem, f'${answer}$' +@@ -661,17 +753,17 @@108def binary_complement_1s(maxDigits=10): +109 r"""Binary Complement 1s +110 +111 | Ex. Problem | Ex. Solution | +112 | --- | --- | +113 | $1111001 = $ | $0000110$ | +114 """ +115 question = ''.join([ +116 str(random.randint(0, 1)) for _ in range(random.randint(1, maxDigits)) +117 ]) +118 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) +119 +120 problem = f'${question} = $' +121 return problem, f'${answer}$'
96def binary_to_decimal(max_dig=10): - 97 r"""Binary to Decimal - 98 - 99 | Ex. Problem | Ex. Solution | -100 | --- | --- | -101 | $000110$ | $6$ | -102 """ -103 problem = ''.join( -104 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) -105 solution = f'${int(problem, 2)}$' -106 return f'${problem}$', solution +@@ -706,17 +798,17 @@124def binary_to_decimal(max_dig=10): +125 r"""Binary to Decimal +126 +127 | Ex. Problem | Ex. Solution | +128 | --- | --- | +129 | $000110$ | $6$ | +130 """ +131 problem = ''.join( +132 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) +133 solution = f'${int(problem, 2)}$' +134 return f'${problem}$', solution
109def binary_to_hex(max_dig=10): -110 r"""Binary to Hexidecimal -111 -112 | Ex. Problem | Ex. Solution | -113 | --- | --- | -114 | $010101$ | $0x15$ | -115 """ -116 problem = ''.join( -117 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) -118 solution = f'${hex(int(problem, 2))}$' -119 return f'${problem}$', solution +@@ -751,24 +843,24 @@137def binary_to_hex(max_dig=10): +138 r"""Binary to Hexidecimal +139 +140 | Ex. Problem | Ex. Solution | +141 | --- | --- | +142 | $010101$ | $0x15$ | +143 """ +144 problem = ''.join( +145 [str(random.randint(0, 1)) for _ in range(random.randint(1, max_dig))]) +146 solution = f'${hex(int(problem, 2))}$' +147 return f'${problem}$', solution
122def decimal_to_bcd(max_number=10000): -123 r"""Decimal to Binary Coded Decimal -124 -125 | Ex. Problem | Ex. Solution | -126 | --- | --- | -127 | BCD of Decimal Number $6575 = $ | $191015$ | -128 """ -129 n = random.randint(1000, max_number) -130 x = n -131 # binstring = '' -132 bcdstring = '' -133 while x > 0: -134 nibble = x % 16 -135 bcdstring = str(nibble) + bcdstring -136 x >>= 4 -137 -138 problem = f"BCD of Decimal Number ${n} = $" -139 return problem, f'${bcdstring}$' +@@ -803,19 +895,19 @@150def decimal_to_bcd(max_number=10000): +151 r"""Decimal to Binary Coded Decimal +152 +153 | Ex. Problem | Ex. Solution | +154 | --- | --- | +155 | BCD of Decimal Number $6575 = $ | $191015$ | +156 """ +157 n = random.randint(1000, max_number) +158 x = n +159 # binstring = '' +160 bcdstring = '' +161 while x > 0: +162 nibble = x % 16 +163 bcdstring = str(nibble) + bcdstring +164 x >>= 4 +165 +166 problem = f"BCD of Decimal Number ${n} = $" +167 return problem, f'${bcdstring}$'
142def decimal_to_binary(max_dec=99): -143 r"""Decimal to Binary -144 -145 | Ex. Problem | Ex. Solution | -146 | --- | --- | -147 | Binary of $4 = $ | $100$ | -148 """ -149 a = random.randint(1, max_dec) -150 b = bin(a).replace("0b", "") -151 -152 problem = f'Binary of ${a} = $' -153 solution = f'${b}$' -154 return problem, solution +@@ -850,19 +942,19 @@170def decimal_to_binary(max_dec=99): +171 r"""Decimal to Binary +172 +173 | Ex. Problem | Ex. Solution | +174 | --- | --- | +175 | Binary of $4 = $ | $100$ | +176 """ +177 a = random.randint(1, max_dec) +178 b = bin(a).replace("0b", "") +179 +180 problem = f'Binary of ${a} = $' +181 solution = f'${b}$' +182 return problem, solution
157def decimal_to_hexadeci(max_dec=1000): -158 r"""Decimal to Hexadecimal -159 -160 | Ex. Problem | Ex. Solution | -161 | --- | --- | -162 | Hexadecimal of $410 = $ | $0x19a$ | -163 """ -164 a = random.randint(0, max_dec) -165 b = hex(a) -166 -167 problem = f"Hexadecimal of ${a} = $" -168 solution = f"${b}$" -169 return problem, solution +@@ -897,19 +989,19 @@185def decimal_to_hexadeci(max_dec=1000): +186 r"""Decimal to Hexadecimal +187 +188 | Ex. Problem | Ex. Solution | +189 | --- | --- | +190 | Hexadecimal of $410 = $ | $0x19a$ | +191 """ +192 a = random.randint(0, max_dec) +193 b = hex(a) +194 +195 problem = f"Hexadecimal of ${a} = $" +196 solution = f"${b}$" +197 return problem, solution
172def decimal_to_octal(max_decimal=4096): -173 r"""Decimal to Octal -174 -175 | Ex. Problem | Ex. Solution | -176 | --- | --- | -177 | The decimal number $3698$ in octal is: | $0o7162$ | -178 """ -179 x = random.randint(0, max_decimal) -180 -181 problem = f"The decimal number ${x}$ in octal is: " -182 solution = f'${oct(x)}$' -183 -184 return problem, solution +@@ -944,30 +1036,30 @@200def decimal_to_octal(max_decimal=4096): +201 r"""Decimal to Octal +202 +203 | Ex. Problem | Ex. Solution | +204 | --- | --- | +205 | The decimal number $3698$ in octal is: | $0o7162$ | +206 """ +207 x = random.randint(0, max_decimal) +208 +209 problem = f"The decimal number ${x}$ in octal is: " +210 solution = f'${oct(x)}$' +211 +212 return problem, solution
187def fibonacci_series(min_no=1): -188 r"""Fibonacci Series -189 -190 | Ex. Problem | Ex. Solution | -191 | --- | --- | -192 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | -193 """ -194 n = random.randint(min_no, 20) -195 -196 def createFibList(n): -197 list = [] -198 for i in range(n): -199 if i < 2: -200 list.append(i) -201 else: -202 val = list[i - 1] + list[i - 2] -203 list.append(val) -204 return list -205 -206 fibList = createFibList(n) -207 -208 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" -209 solution = ', '.join(map(str, fibList)) -210 return problem, f'${solution}$' +@@ -1002,20 +1094,20 @@215def fibonacci_series(min_no=1): +216 r"""Fibonacci Series +217 +218 | Ex. Problem | Ex. Solution | +219 | --- | --- | +220 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | +221 """ +222 n = random.randint(min_no, 20) +223 +224 def createFibList(n): +225 list = [] +226 for i in range(n): +227 if i < 2: +228 list.append(i) +229 else: +230 val = list[i - 1] + list[i - 2] +231 list.append(val) +232 return list +233 +234 fibList = createFibList(n) +235 +236 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" +237 solution = ', '.join(map(str, fibList)) +238 return problem, f'${solution}$'
213def modulo_division(max_res=99, max_modulo=99): -214 r"""Modulo Division -215 -216 | Ex. Problem | Ex. Solution | -217 | --- | --- | -218 | $43$ % $33 = $ | $10$ | -219 """ -220 a = random.randint(0, max_modulo) -221 b = random.randint(0, min(max_res, max_modulo)) -222 c = a % b if b != 0 else 0 -223 -224 problem = f'${a}$ % ${b} = $' -225 solution = f'${c}$' -226 return problem, solution +@@ -1050,21 +1142,21 @@241def modulo_division(max_res=99, max_modulo=99): +242 r"""Modulo Division +243 +244 | Ex. Problem | Ex. Solution | +245 | --- | --- | +246 | $43$ % $33 = $ | $10$ | +247 """ +248 a = random.randint(0, max_modulo) +249 b = random.randint(0, min(max_res, max_modulo)) +250 c = a % b if b != 0 else 0 +251 +252 problem = f'${a}$ % ${b} = $' +253 solution = f'${c}$' +254 return problem, solution
229def nth_fibonacci_number(max_n=100): -230 r"""nth Fibonacci number -231 -232 | Ex. Problem | Ex. Solution | -233 | --- | --- | -234 | What is the 85th Fibonacci number? | $259695496911123328$ | -235 """ -236 gratio = (1 + math.sqrt(5)) / 2 -237 n = random.randint(1, max_n) -238 -239 problem = f"What is the {n}th Fibonacci number?" -240 solution = int( -241 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) -242 -243 return problem, f'${solution}$' +@@ -1099,25 +1191,25 @@257def nth_fibonacci_number(max_n=100): +258 r"""nth Fibonacci number +259 +260 | Ex. Problem | Ex. Solution | +261 | --- | --- | +262 | What is the 85th Fibonacci number? | $259695496911123328$ | +263 """ +264 gratio = (1 + math.sqrt(5)) / 2 +265 n = random.randint(1, max_n) +266 +267 problem = f"What is the {n}th Fibonacci number?" +268 solution = int( +269 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) +270 +271 return problem, f'${solution}$'
246def nth_tribonacci_number(min_length=1, max_length=80): -247 r"""nth Tribonacci number -248 -249 | Ex. Problem | Ex. Solution | -250 | --- | --- | -251 | What is the 14th Tribonacci number? | $504$ | -252 """ -253 -254 tribDict = {0: 0, 1: 0, 2: 1} -255 -256 def recTrib(i): -257 if i not in tribDict: -258 tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3) -259 return tribDict[i] -260 -261 n = random.randint(min_length, max_length) -262 problem = f"What is the {n}th Tribonacci number?" -263 solution = recTrib(n - 1) -264 return problem, f'${solution}$' +@@ -1152,29 +1244,29 @@274def nth_tribonacci_number(min_length=1, max_length=80): +275 r"""nth Tribonacci number +276 +277 | Ex. Problem | Ex. Solution | +278 | --- | --- | +279 | What is the 14th Tribonacci number? | $504$ | +280 """ +281 +282 tribDict = {0: 0, 1: 0, 2: 1} +283 +284 def recTrib(i): +285 if i not in tribDict: +286 tribDict[i] = recTrib(i - 1) + recTrib(i - 2) + recTrib(i - 3) +287 return tribDict[i] +288 +289 n = random.randint(min_length, max_length) +290 problem = f"What is the {n}th Tribonacci number?" +291 solution = recTrib(n - 1) +292 return problem, f'${solution}$'
267def tribonacci_series(min_length=1, max_length=80): -268 r"""Fibonacci Series -269 -270 | Ex. Problem | Ex. Solution | -271 | --- | --- | -272 | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ | -273 """ -274 -275 tribDict = {0: 0, 1: 0, 2: 1} -276 -277 def createTribSeries(i): -278 tribSeries = [] -279 for idx in range(i): -280 if idx not in tribDict: -281 tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3] -282 tribSeries.append(tribDict[idx]) -283 return tribSeries -284 -285 n = random.randint(min_length, max_length) -286 tribSeries = createTribSeries(n) -287 problem = "The Tribonacci Series of the first ${n}$ numbers is ?" -288 solution = ', '.join(map(str, tribSeries)) -289 return problem, f'${solution}$' +@@ -1209,22 +1301,22 @@295def tribonacci_series(min_length=1, max_length=80): +296 r"""Fibonacci Series +297 +298 | Ex. Problem | Ex. Solution | +299 | --- | --- | +300 | The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ | +301 """ +302 +303 tribDict = {0: 0, 1: 0, 2: 1} +304 +305 def createTribSeries(i): +306 tribSeries = [] +307 for idx in range(i): +308 if idx not in tribDict: +309 tribDict[idx] = tribDict[idx - 1] + tribDict[idx - 2] + tribDict[idx - 3] +310 tribSeries.append(tribDict[idx]) +311 return tribSeries +312 +313 n = random.randint(min_length, max_length) +314 tribSeries = createTribSeries(n) +315 problem = "The Tribonacci Series of the first ${n}$ numbers is ?" +316 solution = ', '.join(map(str, tribSeries)) +317 return problem, f'${solution}$'
291def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2): -292 r""" Calculating the Cylinder, Head and Sector numbers for a given LBA -293 | Ex. Problem | Ex. Solution | -294 | --- | --- | -295 | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ | -296 """ -297 lba = random.randint(0, max_lba) -298 cylinder = math.floor(lba/(sectors_per_track*number_of_heads)); -299 t = cylinder*(sectors_per_track*number_of_heads); -300 t = lba-t; -301 head = math.floor(t/(sectors_per_track)); -302 sector = t - (head * lba) + 1; -303 -304 problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?" -305 solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$" -306 return problem, solution +diff --git a/docs/mathgenerator/physics.html b/docs/mathgenerator/physics.html index 30d100c..cd52315 100644 --- a/docs/mathgenerator/physics.html +++ b/docs/mathgenerator/physics.html @@ -61,9 +61,15 @@319def lba_to_chs(max_lba=2880, sectors_per_track=18, number_of_heads=2): +320 r""" Calculating the Cylinder, Head and Sector numbers for a given LBA +321 | Ex. Problem | Ex. Solution | +322 | --- | --- | +323 | If the LBA of a block is $2243$ on a device with $18$ sectors per track and $2$ heads, what are the cylinder, head and sector numbers? | Cylinder: $62$, Head: $0$, Sector: $12$ | +324 """ +325 lba = random.randint(0, max_lba) +326 cylinder = math.floor(lba/(sectors_per_track*number_of_heads)); +327 t = cylinder*(sectors_per_track*number_of_heads); +328 t = lba-t; +329 head = math.floor(t/(sectors_per_track)); +330 sector = t - (head * lba) + 1; +331 +332 problem = f"If the LBA of a block is ${lba}$ on a disk with ${sectors_per_track}$ sectors per track and ${number_of_heads}$ heads, what are the cylinder, head and sector numbers?" +333 solution = f"Cylinder: ${cylinder}$, Head: ${head}$, Sector: ${sector}$" +334 return problem, solution- resistivity
+- + electric_field_strength_two_points +
- fringe_spacing
+- + diffraction_grating_wavelength +
@@ -85,88 +91,147 @@ -+ +1import random - 2import math - 3 - 4# Generic - 5def kinetic_energy(max_mass=1000, max_vel=100): - 6 r"""Kinetic Energy calculation using Ek = 0.5 * m * v^2 - 7 - 8 | Ex. Problem | Ex. Solution | - 9 | --- | --- | -10 | What is the kinetic energy of an object of mass $5 kg$ and velocity $10 m/s$ | $250 J$ | -11 """ -12 velocity = round(random.uniform(1, max_vel),2) -13 mass = round(random.uniform(1, max_mass),2) -14 kinetic_energy = round((0.5 * mass * velocity**2), 2) -15 -16 -17 problem = f"What is the kinetic energy of an object of mass ${mass} kg$ and velocity ${velocity} m/s$?" -18 solution = f'${kinetic_energy} J$' -19 return problem, solution -20 -21 -22# Electricity -23def potential_dividers(max_vin=50, max_resistance=500): -24 r"""Potential Divider question using Vout = (Vin * R2) / (R2 + R1) -25 -26 | Ex. Problem | Ex. Solution | -27 | --- | --- | -28 | In a Potential Divider, if resistors R1 and R2 have resistances of $100 \Omega$ and $50 \Omega$ respectively, and the cell has $12 V$ What is the output potential difference across R2? | $4 V$ | -29 """ -30 ''' -31 This is what a potential divider circuit looks like: -32 ------ -33 | R1 -34 Vi = |----o -35 | R2 Vout -36 |____|____o -37 ''' -38 vin = random.randint(0, max_vin) # Voltage input of cell -39 r1 = random.randint(0, max_resistance) # Resistance of R1 -40 r2 = random.randint(0, max_resistance) # Resistance of R2 -41 vout = round((vin * r2) / (r1 + r2),2) # Voltage output across R2 -42 -43 problem = f"In a Potential Divider, if resistors R1 and R2 have resistances of ${r1} \\Omega$ and ${r2} \\Omega$ respectively, and the cell has ${vin} V$ What is the output potential difference across R2?" -44 solution = f"${vout} V$" -45 return problem, solution -46 -47def resistivity(max_diameter_mm=5, max_length_cm=100, max_resistance=0.1): -48 r"""Calculate the Resistivity using the equation R = (pL)/A, where R = Resistance, L = length of wire, p = resistivity and A = cross sectional area of wire -49 -50 | Ex. Problem | Ex. Solution | -51 | --- | --- | -52 | A wire has resistance $30 m\Omega$ when it is $83.64 cm$ long with a diameter of $4.67 mm$. Calculate the resistivity of the wire | $6.14e-07 \Omega m$ | -53 """ -54 # This question requires a lot of unit conversions and calculating the area of a circle from diameter -55 diameter_mm = round(random.uniform(0, max_diameter_mm),2) # Random diameter in mm -56 cross_sectional_area = math.pi * (diameter_mm / 2000)**2 # Calculate the cross sectional area using pi r² -57 length_cm = round(random.uniform(0, max_length_cm),2) # Random wire length in cm -58 resistance = round(random.uniform(0, max_resistance),2) # Random reistance in ohms -59 -60 resistivity = (resistance * cross_sectional_area) / (length_cm / 100) -61 -62 problem = f"A wire has resistance ${resistance*1000} m\\Omega$ when it is ${length_cm} cm$ long with a diameter of ${diameter_mm} mm$. Calculate the resistivity of the wire" -63 solution = f"${resistivity:.2e} \\Omega m$" -64 -65 return problem, solution -66 -67# Waves -68def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100): -69 r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s -70 | Ex. Problem | Ex. Solution | -71 | --- | --- | -72 | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ | -73 """ -74 wavelength_nm = random.randint(380,750) # Random wavelength between violet and red (nm) -75 screen_distance = random.randint(0, max_screen_distance) # Random distance between screen and slits (m) -76 slit_spacing_mm = random.randint(0, max_slit_spacing_mm) # Random slit spacing (mm) -77 -78 fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5) -79 -80 problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes." -81 solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$" -82 return problem, solution +@@ -329,6 +394,78 @@1import random + 2import math + 3 + 4# Mechanics + 5def kinetic_energy(max_mass=1000, max_vel=100): + 6 r"""Kinetic Energy calculation using Ek = 0.5 * m * v^2 + 7 + 8 | Ex. Problem | Ex. Solution | + 9 | --- | --- | + 10 | What is the kinetic energy of an object of mass $5 kg$ and velocity $10 m/s$ | $250 J$ | + 11 """ + 12 velocity = round(random.uniform(1, max_vel),2) + 13 mass = round(random.uniform(1, max_mass),2) + 14 kinetic_energy = round((0.5 * mass * velocity**2), 2) + 15 + 16 + 17 problem = f"What is the kinetic energy of an object of mass ${mass} kg$ and velocity ${velocity} m/s$?" + 18 solution = f'${kinetic_energy} J$' + 19 return problem, solution + 20 + 21 + 22# Electricity & Electric Fields + 23def potential_dividers(max_vin=50, max_resistance=500): + 24 r"""Potential Divider question using Vout = (Vin * R2) / (R2 + R1) + 25 + 26 | Ex. Problem | Ex. Solution | + 27 | --- | --- | + 28 | In a Potential Divider, if resistors R1 and R2 have resistances of $100 \Omega$ and $50 \Omega$ respectively, and the cell has $12 V$ What is the output potential difference across R2? | $4 V$ | + 29 """ + 30 ''' + 31 This is what a potential divider circuit looks like: + 32 ------ + 33 | R1 + 34 Vi = |----o + 35 | R2 Vout + 36 |____|____o + 37 ''' + 38 vin = random.randint(0, max_vin) # Voltage input of cell + 39 r1 = random.randint(0, max_resistance) # Resistance of R1 + 40 r2 = random.randint(0, max_resistance) # Resistance of R2 + 41 vout = round((vin * r2) / (r1 + r2),2) # Voltage output across R2 + 42 + 43 problem = f"In a Potential Divider, if resistors R1 and R2 have resistances of ${r1} \\Omega$ and ${r2} \\Omega$ respectively, and the cell has ${vin} V$ What is the output potential difference across R2?" + 44 solution = f"${vout} V$" + 45 return problem, solution + 46 + 47def resistivity(max_diameter_mm=5, max_length_cm=100, max_resistance=0.1): + 48 r"""Calculate the Resistivity using the equation R = (pL)/A, where R = Resistance, L = length of wire, p = resistivity and A = cross sectional area of wire + 49 + 50 | Ex. Problem | Ex. Solution | + 51 | --- | --- | + 52 | A wire has resistance $30 m\Omega$ when it is $83.64 cm$ long with a diameter of $4.67 mm$. Calculate the resistivity of the wire | $6.14e-07 \Omega m$ | + 53 """ + 54 # This question requires a lot of unit conversions and calculating the area of a circle from diameter + 55 diameter_mm = round(random.uniform(0, max_diameter_mm),2) # Random diameter in mm + 56 cross_sectional_area = math.pi * (diameter_mm / 2000)**2 # Calculate the cross sectional area using pi r² + 57 length_cm = round(random.uniform(0, max_length_cm),2) # Random wire length in cm + 58 resistance = round(random.uniform(0, max_resistance),2) # Random reistance in ohms + 59 + 60 resistivity = (resistance * cross_sectional_area) / (length_cm / 100) + 61 + 62 problem = f"A wire has resistance ${resistance*1000} m\\Omega$ when it is ${length_cm} cm$ long with a diameter of ${diameter_mm} mm$. Calculate the resistivity of the wire" + 63 solution = f"${resistivity:.2e} \\Omega m$" + 64 + 65 return problem, solution + 66 + 67def electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000): + 68 r"""Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r² + 69 + 70 | Ex. Problem | Ex. Solution | + 71 | --- | --- | + 72 | Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? | $-751417824 NC^{-1}$ (to the right) | + 73 + 74 """ + 75 a_charge = random.randint(-max_charge_uC,max_charge_uC) + 76 b_charge = random.randint(-max_charge_uC,max_charge_uC) + 77 arrangement = [['P'],['A',a_charge],['B',b_charge]] # Arrangement of charge A, B and the point of focus + 78 random.shuffle(arrangement) + 79 seperations = [random.randint(0,max_seperation_cm), random.randint(0,max_seperation_cm)] + 80 total_efs = 0 + 81 # Work out how far A and B are from P (vector) + 82 if arrangement[0][0] == 'P': + 83 arrangement[1].append(seperations[0]) + 84 arrangement[2].append(seperations[0]+seperations[1]) + 85 elif arrangement[1][0] == 'P': + 86 arrangement[0].append(-seperations[0]) + 87 arrangement[2].append(seperations[1]) + 88 else: + 89 arrangement[0].append(-(seperations[0]+seperations[1])) + 90 arrangement[1].append(-seperations[1]) + 91 + 92 # Work out the EFS at point P caused by A and B seperatley, then sum them together in `total_efs` + 93 for point in arrangement: + 94 if point[0] == 'P': + 95 continue + 96 else: + 97 efs = ((8.99*10**9)*(point[1]*10**-6))/((point[2]/100)**2) # efs = kQ/r² + 98 if point[2] > 0: efs = -efs + 99 point.append(efs) +100 total_efs += efs +101 +102 problem = f"Charges A and B and point P are arranged like this:\n{arrangement[0][0]} <-- ${seperations[0]}$ cm --> {arrangement[1][0]} <-- ${seperations[1]}$ cm --> {arrangement[2][0]}\nWhere A and B have charges of ${a_charge}$ µC and ${b_charge}$ µC\nWhat is the electric field strength at point P?" +103 solution = f"${round(total_efs)} NC^{-1}$ (to the right)" +104 return problem, solution +105 +106 +107# Waves +108def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100): +109 r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s +110 | Ex. Problem | Ex. Solution | +111 | --- | --- | +112 | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ | +113 """ +114 wavelength_nm = random.randint(380,750) # Random wavelength between violet and red (nm) +115 screen_distance = random.randint(0, max_screen_distance) # Random distance between screen and slits (m) +116 slit_spacing_mm = random.randint(0, max_slit_spacing_mm) # Random slit spacing (mm) +117 +118 fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5) +119 +120 problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes." +121 solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$" +122 return problem, solution +123 +124def diffraction_grating_wavelength(min_slits_per_mm=100, max_slits_per_mm=500, max_order_number=5): +125 r"""Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ +126 +127 | Ex. Problem | Ex. Solution | +128 | --- | --- | +129 | A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light | $\lambda = 6.487856913364529e-07m = 649nm | +130 +131 """ +132 slits_per_mm = random.randint(min_slits_per_mm, max_slits_per_mm) +133 slit_spacing = 1/(slits_per_mm * 1000) +134 order_number = random.randint(1, max_order_number) +135 angle_of_order = round(random.uniform(0.2, (math.pi/2)-0.2),2) +136 wavelength = ((slit_spacing * math.sin(angle_of_order)) / order_number) +137 +138 problem = f"A laser is shone through a diffraction grating which has ${slits_per_mm}$ lines per mm, the fringe of order number ${order_number}$ is at an angle of ${angle_of_order}$ rad. Calculate the wavelength of the light" +139 solution = f"$\\lambda = {wavelength}m = {round(wavelength / 10**-9)}nm$" +140 +141 return problem, solution+ + + + def + electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000): + + + ++ ++ + +68def electric_field_strength_two_points(max_seperation_cm=100, max_charge_uC=1000): + 69 r"""Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r² + 70 + 71 | Ex. Problem | Ex. Solution | + 72 | --- | --- | + 73 | Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? | $-751417824 NC^{-1}$ (to the right) | + 74 + 75 """ + 76 a_charge = random.randint(-max_charge_uC,max_charge_uC) + 77 b_charge = random.randint(-max_charge_uC,max_charge_uC) + 78 arrangement = [['P'],['A',a_charge],['B',b_charge]] # Arrangement of charge A, B and the point of focus + 79 random.shuffle(arrangement) + 80 seperations = [random.randint(0,max_seperation_cm), random.randint(0,max_seperation_cm)] + 81 total_efs = 0 + 82 # Work out how far A and B are from P (vector) + 83 if arrangement[0][0] == 'P': + 84 arrangement[1].append(seperations[0]) + 85 arrangement[2].append(seperations[0]+seperations[1]) + 86 elif arrangement[1][0] == 'P': + 87 arrangement[0].append(-seperations[0]) + 88 arrangement[2].append(seperations[1]) + 89 else: + 90 arrangement[0].append(-(seperations[0]+seperations[1])) + 91 arrangement[1].append(-seperations[1]) + 92 + 93 # Work out the EFS at point P caused by A and B seperatley, then sum them together in `total_efs` + 94 for point in arrangement: + 95 if point[0] == 'P': + 96 continue + 97 else: + 98 efs = ((8.99*10**9)*(point[1]*10**-6))/((point[2]/100)**2) # efs = kQ/r² + 99 if point[2] > 0: efs = -efs +100 point.append(efs) +101 total_efs += efs +102 +103 problem = f"Charges A and B and point P are arranged like this:\n{arrangement[0][0]} <-- ${seperations[0]}$ cm --> {arrangement[1][0]} <-- ${seperations[1]}$ cm --> {arrangement[2][0]}\nWhere A and B have charges of ${a_charge}$ µC and ${b_charge}$ µC\nWhat is the electric field strength at point P?" +104 solution = f"${round(total_efs)} NC^{-1}$ (to the right)" +105 return problem, solution ++ +Calculate the total electric field strength at point P with given points A and B, using the equation kQ/r²
+ ++ +
++ + + +Ex. Problem +Ex. Solution ++ + +Charges A and B and point P are arranged like this: B <-- 7 cm --> P <-- 79 cm --> A, Where A and B have charges of -56 µC and -410 µC, What is the electric field strength at point P? +$-751417824 NC^{-1}$ (to the right) +@@ -341,21 +478,21 @@
69def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100): -70 r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s -71 | Ex. Problem | Ex. Solution | -72 | --- | --- | -73 | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ | -74 """ -75 wavelength_nm = random.randint(380,750) # Random wavelength between violet and red (nm) -76 screen_distance = random.randint(0, max_screen_distance) # Random distance between screen and slits (m) -77 slit_spacing_mm = random.randint(0, max_slit_spacing_mm) # Random slit spacing (mm) -78 -79 fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5) -80 -81 problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes." -82 solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$" -83 return problem, solution +@@ -378,6 +515,58 @@109def fringe_spacing(max_screen_distance=30, max_slit_spacing_mm=100): +110 r"""Calculate the fringe spacing in a double slit experiment with w=(λD)/s +111 | Ex. Problem | Ex. Solution | +112 | --- | --- | +113 | A laser with a wavelength of $450nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is $12m$ from the slits and the slits are $0.30mm$ apart. Calculate the spacing between the bright fringes. | Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of $0.018m$ | +114 """ +115 wavelength_nm = random.randint(380,750) # Random wavelength between violet and red (nm) +116 screen_distance = random.randint(0, max_screen_distance) # Random distance between screen and slits (m) +117 slit_spacing_mm = random.randint(0, max_slit_spacing_mm) # Random slit spacing (mm) +118 +119 fringe_spacing = round((((wavelength_nm * 10**-9) * screen_distance) / (slit_spacing_mm * 10**-3)),5) +120 +121 problem = f"A laser with a wavelength of ${wavelength_nm}nm$ is shone through a double slit system to produce an interference pattern on a screen. The screen is ${screen_distance}m$ from the slits and the slits are ${slit_spacing_mm}mm$ apart. Calculate the spacing between the bright fringes." +122 solution = f"Using the equation $\\frac{{\\lambda D}}{{s}}$, we get a fringe spacing of ${fringe_spacing}m$" +123 return problem, solution
125def diffraction_grating_wavelength(min_slits_per_mm=100, max_slits_per_mm=500, max_order_number=5): +126 r"""Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ +127 +128 | Ex. Problem | Ex. Solution | +129 | --- | --- | +130 | A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light | $\lambda = 6.487856913364529e-07m = 649nm | +131 +132 """ +133 slits_per_mm = random.randint(min_slits_per_mm, max_slits_per_mm) +134 slit_spacing = 1/(slits_per_mm * 1000) +135 order_number = random.randint(1, max_order_number) +136 angle_of_order = round(random.uniform(0.2, (math.pi/2)-0.2),2) +137 wavelength = ((slit_spacing * math.sin(angle_of_order)) / order_number) +138 +139 problem = f"A laser is shone through a diffraction grating which has ${slits_per_mm}$ lines per mm, the fringe of order number ${order_number}$ is at an angle of ${angle_of_order}$ rad. Calculate the wavelength of the light" +140 solution = f"$\\lambda = {wavelength}m = {round(wavelength / 10**-9)}nm$" +141 +142 return problem, solution +
Calculate the wavelength when given the number of slits per mm, order number and angle of order using the equation nλ = dsinθ
+ +| Ex. Problem | +Ex. Solution | +
|---|---|
| A laser is shone through a diffraction grating which has $293$ lines per mm, the fringe of order number $2$ is at an angle of $0.39$ rad. Calculate the wavelength of the light | +$\lambda = 6.487856913364529e-07m = 649nm | +