From 871e969b76920bafd5a19e96761d5d4598381a11 Mon Sep 17 00:00:00 2001 From: Kritika Singh Date: Tue, 24 Dec 2024 16:20:15 +0530 Subject: [PATCH] Add Euler's Totient Function and Visualization --- .../Mathematics/Euler Function/euler.py | 70 +++++++++++++++++++ .../Mathematics/Euler Function/test_euler.py | 35 ++++++++++ 2 files changed, 105 insertions(+) create mode 100644 pysnippets/Mathematics/Euler Function/euler.py create mode 100644 pysnippets/Mathematics/Euler Function/test_euler.py diff --git a/pysnippets/Mathematics/Euler Function/euler.py b/pysnippets/Mathematics/Euler Function/euler.py new file mode 100644 index 0000000..be3db07 --- /dev/null +++ b/pysnippets/Mathematics/Euler Function/euler.py @@ -0,0 +1,70 @@ +import matplotlib.pyplot as plt +from math import gcd + +def euler_totient_function(n): + """ + Calculate Euler's Totient Function φ(n), which is the count of integers + from 1 to n that are coprime with n. + + Parameters: + n (int): Input number + + Returns: + tuple: (Value of φ(n), List of coprime numbers) + """ + if n <= 0: + raise ValueError("Input must be a positive integer.") + + original_n = n # Save the original value of n for coprime calculation + result = n + coprimes = [] + + # Check divisors up to √n + p = 2 + while p * p <= n: + if n % p == 0: + while n % p == 0: + n //= p + result -= result // p + p += 1 + + if n > 1: + result -= result // n + + # Find coprimes using the original n + for i in range(1, original_n + 1): + if gcd(i, original_n) == 1: + coprimes.append(i) + + return result, coprimes + +def visualize_coprimes(n, coprimes): + """ + Visualize coprime numbers up to n using a bar chart. + + Parameters: + n (int): Input number + coprimes (list): List of coprime numbers + """ + all_numbers = list(range(1, n + 1)) + coprime_flags = [1 if num in coprimes else 0 for num in all_numbers] + + plt.figure(figsize=(10, 6)) + plt.bar(all_numbers, coprime_flags, color='skyblue', edgecolor='black', label='Coprime') + plt.title(f"Coprime Numbers Up to {n}", fontsize=16) + plt.xlabel("Numbers", fontsize=14) + plt.ylabel("Coprime (1 = Yes, 0 = No)", fontsize=14) + plt.xticks(range(1, n + 1, max(1, n // 10))) + plt.yticks([0, 1], labels=["Not Coprime", "Coprime"]) + plt.grid(axis='y', linestyle='--', alpha=0.6) + plt.legend() + plt.show() + +# Example usage +if __name__ == "__main__": + num = int(input("Enter a positive integer: ")) + result, coprimes = euler_totient_function(num) + print(f"φ({num}) = {result}") + print(f"Coprime numbers up to {num}: {coprimes}") + + visualize_coprimes(num, coprimes) diff --git a/pysnippets/Mathematics/Euler Function/test_euler.py b/pysnippets/Mathematics/Euler Function/test_euler.py new file mode 100644 index 0000000..b89024c --- /dev/null +++ b/pysnippets/Mathematics/Euler Function/test_euler.py @@ -0,0 +1,35 @@ +import unittest +from euler import euler_totient_function, visualize_coprimes + +class TestEulerFunction(unittest.TestCase): + + def test_phi_value(self): + # Test the Euler's Totient function value + self.assertEqual(euler_totient_function(1)[0], 1) + self.assertEqual(euler_totient_function(6)[0], 2) + self.assertEqual(euler_totient_function(9)[0], 6) + self.assertEqual(euler_totient_function(10)[0], 4) + + def test_coprimes_list(self): + # Test the list of coprimes + self.assertEqual(euler_totient_function(9)[1], [1, 2, 4, 5, 7, 8]) + self.assertEqual(euler_totient_function(10)[1], [1, 3, 7, 9]) + self.assertEqual(euler_totient_function(6)[1], [1, 5]) + self.assertEqual(euler_totient_function(1)[1], [1]) + + def test_invalid_input(self): + # Test invalid input + with self.assertRaises(ValueError): + euler_totient_function(0) + with self.assertRaises(ValueError): + euler_totient_function(-5) + + def test_visualize_coprimes(self): + # Test the visualize_coprimes function + try: + visualize_coprimes(10, [1, 3, 7, 9]) + except Exception as e: + self.fail(f"visualize_coprimes raised an exception: {e}") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file