From a3db6680819a97207a0bf86137807cc9593b5408 Mon Sep 17 00:00:00 2001 From: boyreau Date: Sat, 14 Nov 2026 14:46:20 +0100 Subject: [PATCH] =?UTF-8?q?modular=20arithmetic=20course=20-=20challenge?= =?UTF-8?q?=206=20-=20r=C3=A9sidus=20quadratique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenge.py | 3 +++ modular_arithmetic.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/challenge.py b/challenge.py index 50be589..0e8f751 100644 --- a/challenge.py +++ b/challenge.py @@ -11,6 +11,7 @@ from intro import ( from modular_arithmetic import ( gcd, extended_gcd, + quadratic_residue, ) from public_key_cryptography import ( modular_exponentiation, @@ -22,6 +23,7 @@ from public_key_cryptography import ( prime_factorization, reverse_single_prime_RSA_cipher, ) +import functools if __name__ == "__main__": # fmt: off @@ -58,4 +60,5 @@ if __name__ == "__main__": print(gcd(66528, 52920)) print(extended_gcd(26513, 32321)) print(f"11 % 6 = {11 % 6}; 8146798528947 % 17 = {8146798528947 % 17}") + print(min([(quadratic_residue(29, x) or 30) for x in [14, 6, 11]])) # Bancal mais on est mod29 donc 30 est forcément pas le min. # fmt: on diff --git a/modular_arithmetic.py b/modular_arithmetic.py index 5cf5f2f..e23b9de 100644 --- a/modular_arithmetic.py +++ b/modular_arithmetic.py @@ -26,3 +26,18 @@ def extended_gcd(a: int, b: int) -> (int, int, int): a = b b = r return a, u[-2], v[-2] + + +# Challenge 6 - quadratic residues +def quadratic_residue(N: int, x: int): + """Find the quadratic residue of x mod N. + Args: + N: a prime integer + x: the integer for which we look for a quadratic residue. + Returns: + Either none or the square root of x mod N. + """ + for i in range(N): + if pow(i, 2, N) == x: + return i + return None