Open utterances-bot opened 10 months ago
import random import math
def is_prime(n, k=5): if n <= 1: return False if n <= 3: return True
def miller_rabin(n, d):
a = random.randint(2, n - 2)
x = pow(a, d, n)
if x == 1 or x == n - 1:
return True
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
return True
return False
r, d = 0, n - 1
while d % 2 == 0:
r += 1
d //= 2
for _ in range(k):
if not miller_rabin(n, d):
return False
return True
def generate_prime(bits): while True: p = random.getrandbits(bits) if p % 2 == 0: p += 1 if is_prime(p): return p
def mod_inverse(a, m): g, x, y = extended_gcd(a, m) if g != 1: raise ValueError("DNE: Modular inverse does not exist") return x % m
def extended_gcd(a, b): if a == 0: return (b, 0, 1) else: g, x, y = extended_gcd(b % a, a) return (g, y - (b // a) * x, x)
def rsa_keygen(bits=2048): p = generate_prime(bits) q = generate_prime(bits) n = p q phi = (p - 1) (q - 1) e = 65537 d = mod_inverse(e, phi) return (n, e), (n, d)
def rsa_encrypt(public_key, plaintext): n, e = public_key cipher = pow(plaintext, e, n) return cipher
def rsa_decrypt(private_key, ciphertext): n, d = private_key plaintext = pow(ciphertext, d, n) return plaintext
if name == "main": public_key, private_key = rsa_keygen(bits=2048) plaintext = input("Enter plaintext: ") plaintext = int.from_bytes(plaintext.encode(), byteorder='big')
ciphertext = rsa_encrypt(public_key, plaintext)
decrypted = rsa_decrypt(private_key, ciphertext)
decrypted_text = decrypted.to_bytes((decrypted.bit_length() + 7) // 8, byteorder='big').decode()
print(f"Public Key (n, e): "+str(public_key))
print(f"Private Key (n, d): "+str(private_key))
print(f"Encrypted: "+str(ciphertext))
print(f"Decrypted: "+str(decrypted_text))
Please ignore the code above.
def procedural_abstraction(x): e = 2.718 return e**x
def sum(num1, num2): return num1 + num2
if name == "main": print(procedural_abstraction(2)) print(sum(5, 7))
First: def exponenchal(exponent, simTime): count = "" for x in range(0, simTime + 1): count += f"Term {x}; {exponent x}:\n\t" for y in range(1, (exponent x) + 1): count += str(y%10) if y%10 == 0: count += f" {y}\'s " count += "\n" return count nums = exponenchal(2, 14) print(nums)
Second:
array1 = [0, 2, 4, 6, 8] array2 = [1, 3, 5, 7, 9]
def summingMachine(num1, num2): if(isinstance(num1, list) == False and isinstance(num2, list) == False): return sum([num1, num2]) elif(isinstance(num1, list) == True and isinstance(num2, list) == True): return sum(num1) + sum(num2) else: return "Error" print(summingMachine(array1, array2)) print(summingMachine(5, 8))
def procedural_abstraction(length, width, height): volume = length width height return volume print(procedural_abstraction(5,2,3))
def summing_machine(first_number, second_number): sum = first_number + second_number return sum print(summing_machine(7, 5))
def procedural_abstraction(num):
print(num*2)
procedural_abstraction(5)
def summing_machine(num1, num2):
print(num1+num2)
summing_machine(5, 6)
def procedural_abstraction(parameter):
result = parameter * 2
return result
def summing_machine(first_number, second_number):
sum_result = first_number + second_number
return sum_result
input_value = 5 abstraction_result = procedural_abstraction(input_value) print("Result of procedural_abstraction:", abstraction_result)
num1 = 7 num2 = 5 sum_result = summing_machine(num1, num2) print(f"The sum of {num1} and {num2} is:", sum_result)
1. def calculate_rectangle_area(length, width): area = length * width return area
length = 6 width = 4 area_of_rectangle = calculate_rectangle_area(length, width) print("The area of the rectangle is:", area_of_rectangle) 2. def summing_machine(first_number, second_number): sum_result = first_number + second_number return sum_result
result = summing_machine(7, 5) print("The sum of 7 and 5 is:", result)
def procedural_abstraction(task): if task == "greet": return "Hello, world!" elif task == "goodbye": return "Goodbye, world!" else: return "Unknown task"
def summing_machine(first_number, second_number): return first_number + second_number
result = summing_machine(7, 5) print(result)
def procedural_abstraction(number1, number2): result = number1 * number2 return result
x = 5 y = 7 product = procedural_abstraction(x, y) print("The product of {} and {} is: {}".format(x, y, product))
def summing_machine(first_number, second_number): result = first_number + second_number return result
sum_result = summing_machine(7, 5)
print("The sum of 7 and 5 is:", sum_result)
def summing_machine(first_number, second_number):
total = first_number + second_number
return total
result = summing_machine(7, 5)
print(f"The sum of 7 and 5 is: {result}")
This fills the requirement for both homework hacks at the same time
# demonstrating procedural abstraction to find the sum of (3 * 5)+(6*2)
def multip(a,b):
return a*b
def solveEq(a,b,c,d):
print(f"({a}*{b})+({c}*{d})={multip(a,b)+multip(c,d)}")
def summing_machine(a,b):
return a+b
print(f"the sum of 5+7={summing_machine(5,7)}")
class Main {
public static int summing(int a,int b)
{
return a+b;
}
public static void main(String[] args) {
System.out.println("the sum of 5+7 is " + summing(5,7));
}
}
HW1: def procedural_abstraction(a, b): result = a + b return result
x = 7 y = 5 result = procedural_abstraction(x, y) print(f"The sum of {x} and {y} is {result}")
HW2: def summing_machine(first_number, second_number): sum_result = first_number + second_number return sum_result
num1 = 7 num2 = 5 total = summing_machine(num1, num2) print(f"The sum of {num1} and {num2} is {total}")
def procedural_abstraction(number1, number2): result = number1 * number2 return result
x = 5 y = 7 product = procedural_abstraction(x, y) print("The product of {} and {} is: {}".format(x, y, product))
def summing_machine(first_number, second_number): result = first_number + second_number return result
sum_result = summing_machine(5, 7)
print("The sum of 5 and 7 is:", sum_result)
sorry i forgot to submit:
https://tejm123.github.io/student2//5.a/c4.1/2023/10/30/proceduresHW_IPYNB_2_.html
Q1: def procedural_abstraction(value): return value ** 2
result = procedural_abstraction(4) print("Result:", result)
Q2: def summing_machine(first_number, second_number): return first_number + second_number
result = summing_machine(7, 5) print("Sum:", result)
1)
def procedural_abstraction(number):
result = number ** 2
return result
input_number = 5 square_result = procedural_abstraction(input_number)
print(f"The square of {input_number} is {square_result}")
2)
def summing_machine(first_number, second_number):
result = first_number + second_number
return result
result_sum = summing_machine(7, 5)
print(f"The sum of 7 and 5 is {result_sum}")
1)
def procedural_abstraction(number):
result = number ** 2
return result
input_number = 5 square_result = procedural_abstraction(input_number)
print(f"The square of {input_number} is {square_result}")
2)
def summing_machine(first_number, second_number):
result = first_number + second_number
return result
result_sum = summing_machine(7, 5)
print(f"The sum of 7 and 5 is {result_sum}")
P1 Procedures | Compci Blogs
Lesson for developing procedures in Python
https://nighthawkcoders.github.io/teacher/2023/10/22/P1_Procedures_IPYNB2.html