mathgenerator.computer_science
1import random 2import math 3 4 5def bcd_to_decimal(max_number=10000): 6 r"""Binary Coded Decimal to Integer 7 8 | Ex. Problem | Ex. Solution | 9 | --- | --- | 10 | Integer of Binary Coded Decimal $4 =$ | $17801$ | 11 """ 12 n = random.randint(1000, max_number) 13 binstring = '' 14 while True: 15 q, r = divmod(n, 10) 16 nibble = bin(r).replace('0b', "") 17 while len(nibble) < 4: 18 nibble = '0' + nibble 19 binstring = nibble + binstring 20 if q == 0: 21 break 22 else: 23 n = q 24 25 problem = f"Integer of Binary Coded Decimal ${n} =$ " 26 solution = f'${int(binstring, 2)}$' 27 return problem, solution 28 29 30def binary_2s_complement(maxDigits=10): 31 r"""Binary 2's Complement 32 33 | Ex. Problem | Ex. Solution | 34 | --- | --- | 35 | 2's complement of $1011 = $ | $101$ | 36 """ 37 digits = random.randint(1, maxDigits) 38 question = ''.join([str(random.randint(0, 1)) 39 for i in range(digits)]).lstrip('0') 40 41 answer = [] 42 for i in question: 43 answer.append(str(int(not bool(int(i))))) 44 45 carry = True 46 j = len(answer) - 1 47 while j >= 0: 48 if answer[j] == '0': 49 answer[j] = '1' 50 carry = False 51 break 52 answer[j] = '0' 53 j -= 1 54 55 if j == 0 and carry is True: 56 answer.insert(0, '1') 57 58 problem = f"2's complement of ${question} = $" 59 solution = ''.join(answer).lstrip('0') 60 return problem, f'${solution}$' 61 62 63def binary_complement_1s(maxDigits=10): 64 r"""Binary Complement 1s 65 66 | Ex. Problem | Ex. Solution | 67 | --- | --- | 68 | $1111001 = $ | $0000110$ | 69 """ 70 question = ''.join([str(random.randint(0, 1)) 71 for _ in range(random.randint(1, maxDigits))]) 72 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) 73 74 problem = f'${question} = $' 75 return problem, f'${answer}$' 76 77 78def binary_to_decimal(max_dig=10): 79 r"""Binary to Decimal 80 81 | Ex. Problem | Ex. Solution | 82 | --- | --- | 83 | $000110$ | $6$ | 84 """ 85 problem = ''.join([str(random.randint(0, 1)) 86 for _ in range(random.randint(1, max_dig))]) 87 solution = f'${int(problem, 2)}$' 88 return f'${problem}$', solution 89 90 91def binary_to_hex(max_dig=10): 92 r"""Binary to Hexidecimal 93 94 | Ex. Problem | Ex. Solution | 95 | --- | --- | 96 | $010101$ | $0x15$ | 97 """ 98 problem = ''.join([str(random.randint(0, 1)) 99 for _ in range(random.randint(1, max_dig))]) 100 solution = f'${hex(int(problem, 2))}$' 101 return f'${problem}$', solution 102 103 104def decimal_to_bcd(max_number=10000): 105 r"""Decimal to Binary Coded Decimal 106 107 | Ex. Problem | Ex. Solution | 108 | --- | --- | 109 | BCD of Decimal Number $6575 = $ | $191015$ | 110 """ 111 n = random.randint(1000, max_number) 112 x = n 113 # binstring = '' 114 bcdstring = '' 115 while x > 0: 116 nibble = x % 16 117 bcdstring = str(nibble) + bcdstring 118 x >>= 4 119 120 problem = f"BCD of Decimal Number ${n} = $" 121 return problem, f'${bcdstring}$' 122 123 124def decimal_to_binary(max_dec=99): 125 r"""Decimal to Binary 126 127 | Ex. Problem | Ex. Solution | 128 | --- | --- | 129 | Binary of $4 = $ | $100$ | 130 """ 131 a = random.randint(1, max_dec) 132 b = bin(a).replace("0b", "") 133 134 problem = f'Binary of ${a} = $' 135 solution = f'${b}$' 136 return problem, solution 137 138 139def decimal_to_hexadeci(max_dec=1000): 140 r"""Decimal to Hexadecimal 141 142 | Ex. Problem | Ex. Solution | 143 | --- | --- | 144 | Hexadecimal of $410 = $ | $0x19a$ | 145 """ 146 a = random.randint(0, max_dec) 147 b = hex(a) 148 149 problem = f"Hexadecimal of ${a} = $" 150 solution = f"${b}$" 151 return problem, solution 152 153 154def decimal_to_octal(max_decimal=4096): 155 r"""Decimal to Octal 156 157 | Ex. Problem | Ex. Solution | 158 | --- | --- | 159 | The decimal number $3698$ in octal is: | $0o7162$ | 160 """ 161 x = random.randint(0, max_decimal) 162 163 problem = f"The decimal number ${x}$ in octal is: " 164 solution = f'${oct(x)}$' 165 166 return problem, solution 167 168 169def fibonacci_series(min_no=1): 170 r"""Fibonacci Series 171 172 | Ex. Problem | Ex. Solution | 173 | --- | --- | 174 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | 175 """ 176 n = random.randint(min_no, 20) 177 178 def createFibList(n): 179 list = [] 180 for i in range(n): 181 if i < 2: 182 list.append(i) 183 else: 184 val = list[i - 1] + list[i - 2] 185 list.append(val) 186 return list 187 188 fibList = createFibList(n) 189 190 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" 191 solution = ', '.join(map(str, fibList)) 192 return problem, f'${solution}$' 193 194 195def modulo_division(max_res=99, max_modulo=99): 196 r"""Modulo Division 197 198 | Ex. Problem | Ex. Solution | 199 | --- | --- | 200 | $43$ % $33 = $ | $10$ | 201 """ 202 a = random.randint(0, max_modulo) 203 b = random.randint(0, min(max_res, max_modulo)) 204 c = a % b if b != 0 else 0 205 206 problem = f'${a}$ % ${b} = $' 207 solution = f'${c}$' 208 return problem, solution 209 210 211def nth_fibonacci_number(max_n=100): 212 r"""nth Fibonacci number 213 214 | Ex. Problem | Ex. Solution | 215 | --- | --- | 216 | What is the 85th Fibonacci number? | $259695496911123328$ | 217 """ 218 gratio = (1 + math.sqrt(5)) / 2 219 n = random.randint(1, max_n) 220 221 problem = f"What is the {n}th Fibonacci number?" 222 solution = int( 223 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) 224 225 return problem, f'${solution}$'
def
bcd_to_decimal(max_number=10000):
6def bcd_to_decimal(max_number=10000): 7 r"""Binary Coded Decimal to Integer 8 9 | Ex. Problem | Ex. Solution | 10 | --- | --- | 11 | Integer of Binary Coded Decimal $4 =$ | $17801$ | 12 """ 13 n = random.randint(1000, max_number) 14 binstring = '' 15 while True: 16 q, r = divmod(n, 10) 17 nibble = bin(r).replace('0b', "") 18 while len(nibble) < 4: 19 nibble = '0' + nibble 20 binstring = nibble + binstring 21 if q == 0: 22 break 23 else: 24 n = q 25 26 problem = f"Integer of Binary Coded Decimal ${n} =$ " 27 solution = f'${int(binstring, 2)}$' 28 return problem, solution
Binary Coded Decimal to Integer
| Ex. Problem | Ex. Solution |
|---|---|
| Integer of Binary Coded Decimal $4 =$ | $17801$ |
def
binary_2s_complement(maxDigits=10):
31def binary_2s_complement(maxDigits=10): 32 r"""Binary 2's Complement 33 34 | Ex. Problem | Ex. Solution | 35 | --- | --- | 36 | 2's complement of $1011 = $ | $101$ | 37 """ 38 digits = random.randint(1, maxDigits) 39 question = ''.join([str(random.randint(0, 1)) 40 for i in range(digits)]).lstrip('0') 41 42 answer = [] 43 for i in question: 44 answer.append(str(int(not bool(int(i))))) 45 46 carry = True 47 j = len(answer) - 1 48 while j >= 0: 49 if answer[j] == '0': 50 answer[j] = '1' 51 carry = False 52 break 53 answer[j] = '0' 54 j -= 1 55 56 if j == 0 and carry is True: 57 answer.insert(0, '1') 58 59 problem = f"2's complement of ${question} = $" 60 solution = ''.join(answer).lstrip('0') 61 return problem, f'${solution}$'
Binary 2's Complement
| Ex. Problem | Ex. Solution |
|---|---|
| 2's complement of $1011 = $ | $101$ |
def
binary_complement_1s(maxDigits=10):
64def binary_complement_1s(maxDigits=10): 65 r"""Binary Complement 1s 66 67 | Ex. Problem | Ex. Solution | 68 | --- | --- | 69 | $1111001 = $ | $0000110$ | 70 """ 71 question = ''.join([str(random.randint(0, 1)) 72 for _ in range(random.randint(1, maxDigits))]) 73 answer = ''.join(["0" if digit == "1" else "1" for digit in question]) 74 75 problem = f'${question} = $' 76 return problem, f'${answer}$'
Binary Complement 1s
| Ex. Problem | Ex. Solution |
|---|---|
| $1111001 = $ | $0000110$ |
def
binary_to_decimal(max_dig=10):
79def binary_to_decimal(max_dig=10): 80 r"""Binary to Decimal 81 82 | Ex. Problem | Ex. Solution | 83 | --- | --- | 84 | $000110$ | $6$ | 85 """ 86 problem = ''.join([str(random.randint(0, 1)) 87 for _ in range(random.randint(1, max_dig))]) 88 solution = f'${int(problem, 2)}$' 89 return f'${problem}$', solution
Binary to Decimal
| Ex. Problem | Ex. Solution |
|---|---|
| $000110$ | $6$ |
def
binary_to_hex(max_dig=10):
92def binary_to_hex(max_dig=10): 93 r"""Binary to Hexidecimal 94 95 | Ex. Problem | Ex. Solution | 96 | --- | --- | 97 | $010101$ | $0x15$ | 98 """ 99 problem = ''.join([str(random.randint(0, 1)) 100 for _ in range(random.randint(1, max_dig))]) 101 solution = f'${hex(int(problem, 2))}$' 102 return f'${problem}$', solution
Binary to Hexidecimal
| Ex. Problem | Ex. Solution |
|---|---|
| $010101$ | $0x15$ |
def
decimal_to_bcd(max_number=10000):
105def decimal_to_bcd(max_number=10000): 106 r"""Decimal to Binary Coded Decimal 107 108 | Ex. Problem | Ex. Solution | 109 | --- | --- | 110 | BCD of Decimal Number $6575 = $ | $191015$ | 111 """ 112 n = random.randint(1000, max_number) 113 x = n 114 # binstring = '' 115 bcdstring = '' 116 while x > 0: 117 nibble = x % 16 118 bcdstring = str(nibble) + bcdstring 119 x >>= 4 120 121 problem = f"BCD of Decimal Number ${n} = $" 122 return problem, f'${bcdstring}$'
Decimal to Binary Coded Decimal
| Ex. Problem | Ex. Solution |
|---|---|
| BCD of Decimal Number $6575 = $ | $191015$ |
def
decimal_to_binary(max_dec=99):
125def decimal_to_binary(max_dec=99): 126 r"""Decimal to Binary 127 128 | Ex. Problem | Ex. Solution | 129 | --- | --- | 130 | Binary of $4 = $ | $100$ | 131 """ 132 a = random.randint(1, max_dec) 133 b = bin(a).replace("0b", "") 134 135 problem = f'Binary of ${a} = $' 136 solution = f'${b}$' 137 return problem, solution
Decimal to Binary
| Ex. Problem | Ex. Solution |
|---|---|
| Binary of $4 = $ | $100$ |
def
decimal_to_hexadeci(max_dec=1000):
140def decimal_to_hexadeci(max_dec=1000): 141 r"""Decimal to Hexadecimal 142 143 | Ex. Problem | Ex. Solution | 144 | --- | --- | 145 | Hexadecimal of $410 = $ | $0x19a$ | 146 """ 147 a = random.randint(0, max_dec) 148 b = hex(a) 149 150 problem = f"Hexadecimal of ${a} = $" 151 solution = f"${b}$" 152 return problem, solution
Decimal to Hexadecimal
| Ex. Problem | Ex. Solution |
|---|---|
| Hexadecimal of $410 = $ | $0x19a$ |
def
decimal_to_octal(max_decimal=4096):
155def decimal_to_octal(max_decimal=4096): 156 r"""Decimal to Octal 157 158 | Ex. Problem | Ex. Solution | 159 | --- | --- | 160 | The decimal number $3698$ in octal is: | $0o7162$ | 161 """ 162 x = random.randint(0, max_decimal) 163 164 problem = f"The decimal number ${x}$ in octal is: " 165 solution = f'${oct(x)}$' 166 167 return problem, solution
Decimal to Octal
| Ex. Problem | Ex. Solution |
|---|---|
| The decimal number $3698$ in octal is: | $0o7162$ |
def
fibonacci_series(min_no=1):
170def fibonacci_series(min_no=1): 171 r"""Fibonacci Series 172 173 | Ex. Problem | Ex. Solution | 174 | --- | --- | 175 | The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ | 176 """ 177 n = random.randint(min_no, 20) 178 179 def createFibList(n): 180 list = [] 181 for i in range(n): 182 if i < 2: 183 list.append(i) 184 else: 185 val = list[i - 1] + list[i - 2] 186 list.append(val) 187 return list 188 189 fibList = createFibList(n) 190 191 problem = "The Fibonacci Series of the first ${n}$ numbers is ?" 192 solution = ', '.join(map(str, fibList)) 193 return problem, f'${solution}$'
Fibonacci Series
| Ex. Problem | Ex. Solution |
|---|---|
| The Fibonacci Series of the first ${n}$ numbers is ? | $0, 1, 1, 2, 3, 5, 8, 13, 21$ |
def
modulo_division(max_res=99, max_modulo=99):
196def modulo_division(max_res=99, max_modulo=99): 197 r"""Modulo Division 198 199 | Ex. Problem | Ex. Solution | 200 | --- | --- | 201 | $43$ % $33 = $ | $10$ | 202 """ 203 a = random.randint(0, max_modulo) 204 b = random.randint(0, min(max_res, max_modulo)) 205 c = a % b if b != 0 else 0 206 207 problem = f'${a}$ % ${b} = $' 208 solution = f'${c}$' 209 return problem, solution
Modulo Division
| Ex. Problem | Ex. Solution |
|---|---|
| $43$ % $33 = $ | $10$ |
def
nth_fibonacci_number(max_n=100):
212def nth_fibonacci_number(max_n=100): 213 r"""nth Fibonacci number 214 215 | Ex. Problem | Ex. Solution | 216 | --- | --- | 217 | What is the 85th Fibonacci number? | $259695496911123328$ | 218 """ 219 gratio = (1 + math.sqrt(5)) / 2 220 n = random.randint(1, max_n) 221 222 problem = f"What is the {n}th Fibonacci number?" 223 solution = int( 224 (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) 225 226 return problem, f'${solution}$'
nth Fibonacci number
| Ex. Problem | Ex. Solution |
|---|---|
| What is the 85th Fibonacci number? | $259695496911123328$ |