This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pairwise(x, N = 1): | |
""" | |
Pair wise summation algorithm | |
is a summaiton algorithm | |
used to get accurate summation | |
of the given lists""" | |
if len(x) == 0: | |
return 0 | |
if len(x) <= N: | |
s = x[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def KahanSum(l): | |
""" | |
This function will find the sum | |
of a given list without compromising | |
on the accuracy""" | |
summation = 0.0 | |
c = 0.0 | |
for i in l: | |
y = i - c | |
t = summation + y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gcd(u, v): | |
""" | |
A non recursive function | |
while takes two integers as input | |
and returns the GCD of the numbers | |
""" | |
# if u is 0, return v | |
if u == 0: return v | |
# if v is 0, return u | |
if v == 0: return u |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import the function | |
from fractions import gcd | |
# use it directly | |
print gcd(500, 5) #5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gcd(u, v): | |
""" This function will calcluate | |
GCD of given two numbers. | |
If the input is negative, both the | |
numbers are converted to positive | |
before the calculation | |
""" | |
if u == v: | |
return u | |
elif u == 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def heap(a): | |
""" | |
This program will take any iterable object | |
as input and will give a generator object | |
as output which can be used with for | |
loop to get all the permutations | |
""" | |
n = len(a) | |
c = [0]*n | |
A = list(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def heap_string(a): | |
""" | |
This function will find different permutations of | |
a given string. It was first proposed by B. R. Heap in 1963. | |
https://en.wikipedia.org/wiki/Heap%27s_algorithm | |
""" | |
n = len(a) | |
c = [0]*n | |
yield a | |
i = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://radiusofcircle.blogspot.com | |
# time module | |
import time | |
# time at the start of program execution | |
start = time.time() | |
# 4 digit triangular numbers | |
tria = [n*(n+1)/2 for n in xrange(45, 141)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://radiusofcircle.blogspot.com | |
# time, random, math modules | |
import time, random, math | |
# start of program | |
start = time.time() | |
# Sieve of Erotosthenes | |
# One of the best algorithm to generate prime numbers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://radiusofcircle.blogspot.com | |
# time module | |
import time | |
# time at the start of program execution | |
start = time.time() | |
def check_english(ascii1, ascii2): |
NewerOlder