From 463b20910b3d2dff65eed3f091c3a3b85122aaba Mon Sep 17 00:00:00 2001 From: JehanPatel Date: Sun, 1 Oct 2023 11:45:45 +0530 Subject: [PATCH 1/5] Added Project Euler - Problem 95 (Fixes #9166) --- .../problem_095/Project Euler - Problem 95.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 project_euler/problem_095/Project Euler - Problem 95.py diff --git a/project_euler/problem_095/Project Euler - Problem 95.py b/project_euler/problem_095/Project Euler - Problem 95.py new file mode 100644 index 000000000000..ac77887bbd2f --- /dev/null +++ b/project_euler/problem_095/Project Euler - Problem 95.py @@ -0,0 +1,45 @@ +# Project Euler - Problem 95 +# Amicable Chains + +# https://www.github.com/JehanPatel + +# Refer to https://github.com/TheAlgorithms/Python/issues/9166 for further discussion on Project Euler - Problem 95 + +# What is Amicable Chains? +# Amicable chains are a sequence of numbers in which each number is the sum of the proper divisors of the number that follows it. For example, consider the sequence 220, 284, 220, 284, ... Here, 220 is the sum of the proper divisors of 284, and 284 is the sum of the proper divisors of 220. + +# Problem Statement - https://user-images.githubusercontent.com/118645569/271806807-863546a0-c723-437a-9475-3017e3218d55.png + +import itertools + +def compute(): + LIMIT = 1000000 + + divisorsum = [0] * (LIMIT + 1) + for i in range(1, LIMIT + 1): + for j in range(i * 2, LIMIT + 1, i): + divisorsum[j] += i + + maxchainlen = 0 + ans = -1 + for i in range(LIMIT + 1): + visited = set() + cur = i + for count in itertools.count(1): + visited.add(cur) + next = divisorsum[cur] + if next == i: + if count > maxchainlen: + ans = i + maxchainlen = count + break + elif next > LIMIT or next in visited: + break + else: + cur = next + + return str(ans) + + +if __name__ == "__main__": + print(compute()) From df4fecaa58125016572e813b6d08a1a8e3dd4a5f Mon Sep 17 00:00:00 2001 From: JehanPatel Date: Sun, 1 Oct 2023 11:50:29 +0530 Subject: [PATCH 2/5] Added Project Euler - Problem 95 (Fixes #9166) --- .../{Project Euler - Problem 95.py => project_euler_095.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename project_euler/problem_095/{Project Euler - Problem 95.py => project_euler_095.py} (100%) diff --git a/project_euler/problem_095/Project Euler - Problem 95.py b/project_euler/problem_095/project_euler_095.py similarity index 100% rename from project_euler/problem_095/Project Euler - Problem 95.py rename to project_euler/problem_095/project_euler_095.py From 0b376df76e32d2273412a4446e0dc7e2bebdf470 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 06:24:06 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../problem_095/project_euler_095.py | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/project_euler/problem_095/project_euler_095.py b/project_euler/problem_095/project_euler_095.py index ac77887bbd2f..42527a026d69 100644 --- a/project_euler/problem_095/project_euler_095.py +++ b/project_euler/problem_095/project_euler_095.py @@ -12,34 +12,35 @@ import itertools + def compute(): - LIMIT = 1000000 - - divisorsum = [0] * (LIMIT + 1) - for i in range(1, LIMIT + 1): - for j in range(i * 2, LIMIT + 1, i): - divisorsum[j] += i - - maxchainlen = 0 - ans = -1 - for i in range(LIMIT + 1): - visited = set() - cur = i - for count in itertools.count(1): - visited.add(cur) - next = divisorsum[cur] - if next == i: - if count > maxchainlen: - ans = i - maxchainlen = count - break - elif next > LIMIT or next in visited: - break - else: - cur = next - - return str(ans) + LIMIT = 1000000 + + divisorsum = [0] * (LIMIT + 1) + for i in range(1, LIMIT + 1): + for j in range(i * 2, LIMIT + 1, i): + divisorsum[j] += i + + maxchainlen = 0 + ans = -1 + for i in range(LIMIT + 1): + visited = set() + cur = i + for count in itertools.count(1): + visited.add(cur) + next = divisorsum[cur] + if next == i: + if count > maxchainlen: + ans = i + maxchainlen = count + break + elif next > LIMIT or next in visited: + break + else: + cur = next + + return str(ans) if __name__ == "__main__": - print(compute()) + print(compute()) From ff1005fa54443b45a12dd35e4cd2c87b548d13e3 Mon Sep 17 00:00:00 2001 From: JehanPatel Date: Sun, 1 Oct 2023 12:10:38 +0530 Subject: [PATCH 4/5] added einstein_mass_energy_equivalence_module (fixes #9191) --- ...einstein_mass_energy_equivalence_module.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 physics/einstein_mass_energy_equivalence_module.py diff --git a/physics/einstein_mass_energy_equivalence_module.py b/physics/einstein_mass_energy_equivalence_module.py new file mode 100644 index 000000000000..bde4ab83e59a --- /dev/null +++ b/physics/einstein_mass_energy_equivalence_module.py @@ -0,0 +1,30 @@ +# Einstein's Energy-Mass Equivalence #9191 (https://github.com/TheAlgorithms/Python/issues/9191) + +# https://www.github.com/JehanPatel + +# What is Einstein's Energy-Mass Equivalence +# It expresses the fact that mass and energy are the same physical entity and can be changed into each other. +# Refer https://en.wikipedia.org/wiki/Mass%E2%80%93energy_equivalence for more information on the topic. + +# Module for Einstein's Energy-Mass Equivalence. + +def calculate_energy_equivalent(mass): + """ + Calculate the energy equivalent of a given mass. + + :param mass: Mass in kilograms + :type mass: float + :return: Energy equivalent in joules + :rtype: float + """ + # Constants + G = 6.67430e-11 # Gravitational constant + c = 3.0e8 # Speed of light + h = 6.62607004e-34 # Planck constant + + # Calculate energy equivalent + energy_equivalent = mass * c**2 + + return energy_equivalent + +# Happy Coding! \ No newline at end of file From c2780f43e1e5f75a64cebffa38efd6049fa8c7fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 06:41:59 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/einstein_mass_energy_equivalence_module.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/physics/einstein_mass_energy_equivalence_module.py b/physics/einstein_mass_energy_equivalence_module.py index bde4ab83e59a..ce06124bb2d3 100644 --- a/physics/einstein_mass_energy_equivalence_module.py +++ b/physics/einstein_mass_energy_equivalence_module.py @@ -8,6 +8,7 @@ # Module for Einstein's Energy-Mass Equivalence. + def calculate_energy_equivalent(mass): """ Calculate the energy equivalent of a given mass. @@ -18,13 +19,14 @@ def calculate_energy_equivalent(mass): :rtype: float """ # Constants - G = 6.67430e-11 # Gravitational constant - c = 3.0e8 # Speed of light - h = 6.62607004e-34 # Planck constant + G = 6.67430e-11 # Gravitational constant + c = 3.0e8 # Speed of light + h = 6.62607004e-34 # Planck constant # Calculate energy equivalent energy_equivalent = mass * c**2 return energy_equivalent -# Happy Coding! \ No newline at end of file + +# Happy Coding!