Skip to content

Commit 8d378eb

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
Pull from TheAlgorithms/Python
2 parents 3c61849 + b4cbf5d commit 8d378eb

File tree

58 files changed

+2079
-73
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2079
-73
lines changed
File renamed without changes.
File renamed without changes.

ArithmeticAnalysis/LUdecomposition.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
import numpy
3+
4+
def LUDecompose (table): #table that contains our data
5+
#table has to be a square array so we need to check first
6+
rows,columns=numpy.shape(table)
7+
L=numpy.zeros((rows,columns))
8+
U=numpy.zeros((rows,columns))
9+
if rows!=columns:
10+
return
11+
for i in range (columns):
12+
for j in range(i-1):
13+
sum=0
14+
for k in range (j-1):
15+
sum+=L[i][k]*U[k][j]
16+
L[i][j]=(table[i][j]-sum)/U[j][j]
17+
L[i][i]=1
18+
for j in range(i-1,columns):
19+
sum1=0
20+
for k in range(i-1):
21+
sum1+=L[i][k]*U[k][j]
22+
U[i][j]=table[i][j]-sum1
23+
return L,U
24+
25+
26+
27+
28+
29+
30+
31+
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
32+
L,U = LUDecompose(matrix)
33+
print(L)
34+
print(U)
File renamed without changes.

File_Transfer_Protocol/ftp_client_server.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
s.bind((host, port)) # Bind to the port
99
s.listen(5) # Now wait for client connection.
1010

11-
print 'Server listening....'
11+
print('Server listening....')
1212

1313
while True:
1414
conn, addr = s.accept() # Establish connection with client.
15-
print 'Got connection from', addr
15+
print('Got connection from', addr)
1616
data = conn.recv(1024)
1717
print('Server received', repr(data))
1818

19-
filename='mytext.txt'
20-
f = open(filename,'rb')
21-
l = f.read(1024)
22-
while (l):
23-
conn.send(l)
24-
print('Sent ',repr(l))
25-
l = f.read(1024)
19+
filename = 'mytext.txt'
20+
f = open(filename, 'rb')
21+
in_data = f.read(1024)
22+
while (in_data):
23+
conn.send(in_data)
24+
print('Sent ', repr(in_data))
25+
in_data = f.read(1024)
2626
f.close()
2727

2828
print('Done sending')
@@ -42,7 +42,7 @@
4242
s.send("Hello server!")
4343

4444
with open('received_file', 'wb') as f:
45-
print 'file opened'
45+
print('file opened')
4646
while True:
4747
print('receiving data...')
4848
data = s.recv(1024)
@@ -55,4 +55,4 @@
5555
f.close()
5656
print('Successfully get the file')
5757
s.close()
58-
print('connection closed')
58+
print('connection closed')

Maths/SimpsonRule.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
'''
3+
Numerical integration or quadrature for a smooth function f with known values at x_i
4+
5+
This method is the classical approch of suming 'Equally Spaced Abscissas'
6+
7+
method 2:
8+
"Simpson Rule"
9+
10+
'''
11+
from __future__ import print_function
12+
13+
14+
def method_2(boundary, steps):
15+
# "Simpson Rule"
16+
# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
17+
h = (boundary[1] - boundary[0]) / steps
18+
a = boundary[0]
19+
b = boundary[1]
20+
x_i = makePoints(a,b,h)
21+
y = 0.0
22+
y += (h/3.0)*f(a)
23+
cnt = 2
24+
for i in x_i:
25+
y += (h/3)*(4-2*(cnt%2))*f(i)
26+
cnt += 1
27+
y += (h/3.0)*f(b)
28+
return y
29+
30+
def makePoints(a,b,h):
31+
x = a + h
32+
while x < (b-h):
33+
yield x
34+
x = x + h
35+
36+
def f(x): #enter your function here
37+
y = (x-0)*(x-0)
38+
return y
39+
40+
def main():
41+
a = 0.0 #Lower bound of integration
42+
b = 1.0 #Upper bound of integration
43+
steps = 10.0 #define number of steps or resolution
44+
boundary = [a, b] #define boundary of integration
45+
y = method_2(boundary, steps)
46+
print('y = {0}'.format(y))
47+
48+
if __name__ == '__main__':
49+
main()

Maths/TrapezoidalRule.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Numerical integration or quadrature for a smooth function f with known values at x_i
3+
4+
This method is the classical approch of suming 'Equally Spaced Abscissas'
5+
6+
method 1:
7+
"extended trapezoidal rule"
8+
9+
'''
10+
from __future__ import print_function
11+
12+
def method_1(boundary, steps):
13+
# "extended trapezoidal rule"
14+
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
15+
h = (boundary[1] - boundary[0]) / steps
16+
a = boundary[0]
17+
b = boundary[1]
18+
x_i = makePoints(a,b,h)
19+
y = 0.0
20+
y += (h/2.0)*f(a)
21+
for i in x_i:
22+
#print(i)
23+
y += h*f(i)
24+
y += (h/2.0)*f(b)
25+
return y
26+
27+
def makePoints(a,b,h):
28+
x = a + h
29+
while x < (b-h):
30+
yield x
31+
x = x + h
32+
33+
def f(x): #enter your function here
34+
y = (x-0)*(x-0)
35+
return y
36+
37+
def main():
38+
a = 0.0 #Lower bound of integration
39+
b = 1.0 #Upper bound of integration
40+
steps = 10.0 #define number of steps or resolution
41+
boundary = [a, b] #define boundary of integration
42+
y = method_1(boundary, steps)
43+
print('y = {0}'.format(y))
44+
45+
if __name__ == '__main__':
46+
main()

Project Euler/Problem 01/sol4.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def mulitples(limit):
2+
xmulti = []
3+
zmulti = []
4+
z = 3
5+
x = 5
6+
temp = 1
7+
while True:
8+
result = z * temp
9+
if (result < limit):
10+
zmulti.append(result)
11+
temp += 1
12+
continue
13+
else:
14+
temp = 1
15+
break
16+
while True:
17+
result = x * temp
18+
if (result < limit):
19+
xmulti.append(result)
20+
temp += 1
21+
continue
22+
else:
23+
temp = 1
24+
break
25+
return (sum(zmulti) + sum(xmulti))
26+
27+
28+
29+
30+
31+
32+
print (mulitples(100))

Project Euler/Problem 02/sol2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def fib(n):
2+
ls = []
3+
a,b = 0,1
4+
n += 1
5+
for i in range(n):
6+
if (b % 2 == 0):
7+
ls.append(b)
8+
else:
9+
pass
10+
a,b = b, a+b
11+
print (sum(ls))
12+
return None
13+
fib(10)

Project Euler/Problem 02/sol3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Problem:
3+
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
4+
0,1,1,2,3,5,8,13,21,34,55,89,..
5+
Every third term from 0 is even So using this I have written a simple code
6+
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
7+
e.g. for n=10, we have {2,8}, sum is 10.
8+
'''
9+
"""Python 3"""
10+
n = int(input())
11+
a=0
12+
b=2
13+
count=0
14+
while 4*b+a<n:
15+
c=4*b+a
16+
a=b
17+
b=c
18+
count=count+a
19+
print(count+b)
20+

Project Euler/Problem 05/sol2.py

Lines changed: 20 additions & 0 deletions
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 08/sol1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
def main():
3+
LargestProduct = -sys.maxsize-1
4+
number=input().strip()
5+
for i in range(len(number)-13):
6+
product=1
7+
for j in range(13):
8+
product *= int(number[i+j])
9+
if product > LargestProduct:
10+
LargestProduct = product
11+
print(LargestProduct)
12+
13+
14+
if __name__ == '__main__':
15+
main()

Project Euler/Problem 10/sol1.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import print_function
2+
from math import sqrt
3+
4+
try:
5+
xrange #Python 2
6+
except NameError:
7+
xrange = range #Python 3
8+
9+
def is_prime(n):
10+
for i in xrange(2, int(sqrt(n))+1):
11+
if n%i == 0:
12+
return False
13+
14+
return True
15+
16+
def sum_of_primes(n):
17+
if n > 2:
18+
sumOfPrimes = 2
19+
else:
20+
return 0
21+
22+
for i in xrange(3, n, 2):
23+
if is_prime(i):
24+
sumOfPrimes += i
25+
26+
return sumOfPrimes
27+
28+
if __name__ == '__main__':
29+
import sys
30+
31+
if len(sys.argv) == 1:
32+
print(sum_of_primes(2000000))
33+
else:
34+
try:
35+
n = int(sys.argv[1])
36+
print(sum_of_primes(n))
37+
except ValueError:
38+
print('Invalid entry - please enter a number.')

Project Euler/Problem 11/grid.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
2+
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
3+
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
4+
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
5+
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
6+
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
7+
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
8+
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
9+
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
10+
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
11+
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
12+
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
13+
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
14+
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
15+
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
16+
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
17+
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
18+
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
19+
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
20+
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

0 commit comments

Comments
 (0)