Skip to content

Commit a2e615e

Browse files
authored
Merge pull request TheAlgorithms#277 from girijamanojkumarreddy/master
Added a Solution using Euclidean Algo
2 parents 5f1ab6b + f538ea5 commit a2e615e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Project Euler/Problem 05/sol2.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/python3
2+
'''
3+
Problem:
4+
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
5+
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?
6+
'''
7+
8+
""" Euclidean GCD Algorithm """
9+
def gcd(x,y):
10+
return x if y==0 else gcd(y,x%y)
11+
12+
""" Using the property lcm*gcd of two numbers = product of them """
13+
def lcm(x,y):
14+
return (x*y)//gcd(x,y)
15+
16+
n = int(input())
17+
g=1
18+
for i in range(1,n+1):
19+
g=lcm(g,i)
20+
print(g)

Project Euler/Problem 9/sol2.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""A Pythagorean triplet is a set of three natural numbers, for which,
2+
a^2+b^2=c^2
3+
Given N, Check if there exists any Pythagorean triplet for which a+b+c=N
4+
Find maximum possible value of product of a,b,c among all such Pythagorean triplets, If there is no such Pythagorean triplet print -1."""
5+
#!/bin/python3
6+
import sys
7+
8+
product=-1
9+
d=0
10+
N = int(input())
11+
for a in range(1,N//3):
12+
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """
13+
b=(N*N-2*a*N)//(2*N-2*a)
14+
c=N-a-b
15+
if c*c==(a*a+b*b):
16+
d=(a*b*c)
17+
if d>=product:
18+
product=d
19+
print(product)

0 commit comments

Comments
 (0)