Skip to content

Instantly share code, notes, and snippets.

View anivarth's full-sized avatar
🎯
Focusing

Anivarth anivarth

🎯
Focusing
View GitHub Profile
@anivarth
anivarth / pairwise_algorithm.py
Created October 25, 2016 12:14
pairwise summation is a better performing algorithm and gives result as precise as possible
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]
@anivarth
anivarth / kahan_algorithm.py
Last active October 22, 2016 13:29
Kahan's Algorithm is used to improve the accuracy of the summation
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
@anivarth
anivarth / binary_gcd_nr.py
Created October 21, 2016 14:31
Binary GCD algorithm implementation without using recursions
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
@anivarth
anivarth / fractions_gcd.py
Created October 21, 2016 14:00
A single line import code to import the gcd function from the fractions module
# import the function
from fractions import gcd
# use it directly
print gcd(500, 5) #5
@anivarth
anivarth / binary_gcd.py
Last active October 21, 2016 11:55
Binary GCD algorithm used to calculate
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:
@anivarth
anivarth / heap.py
Created October 19, 2016 08:11
Implementation of Heap's Algorithm using python
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)
@anivarth
anivarth / heap_string.py
Last active October 19, 2016 08:00
Implementation of heap algorithm in python to find permutations of a given string
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
@anivarth
anivarth / pep61.py
Created October 17, 2016 06:07
Project Euler Problem 61 Solution With Python
# 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)]
@anivarth
anivarth / pep60.py
Last active June 12, 2021 20:53
Project Euler Problem 60 Solution With Python
# 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
@anivarth
anivarth / pep59.py
Created June 26, 2016 04:40
Project Euler Problem 59 Solution With Python
# http://radiusofcircle.blogspot.com
# time module
import time
# time at the start of program execution
start = time.time()
def check_english(ascii1, ascii2):