Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@igorvanloo
igorvanloo / p30.py
Created July 22, 2021 14:37
Problem 30
# These are the fifth power digit sum numbers except the last one is missing
#[4150, 4151, 54748, 92727, 93084]
def sum_digits_mod(x):
totalsum = 0
while x != 0:
totalsum += (x % 10)**5
x = x // 10
return totalsum
@igorvanloo
igorvanloo / p23.py
Created July 21, 2021 15:46
Problem 23
def Divisors(x):
divisors = []
for i in range(1,int(math.sqrt(x))+1):
if x % i == 0:
divisors.append(i)
if i != int(x/i):
divisors.append(int(x/i))
divisors.remove(x)
return sum(set(divisors))
@igorvanloo
igorvanloo / pythagoreantriple.py
Last active July 23, 2021 06:46
Pythagorean Triple Function
def ppt(limit):
triples = []
for m in range(2,int(math.sqrt(limit))+1):
for n in range(1,m):
if (m+n) % 2 == 1 and math.gcd(m,n) == 1:
a = m**2 + n**2
b = m**2 - n**2
c = 2*m*n
p = max(a,b,c)
@igorvanloo
igorvanloo / partitionfunc.py
Last active July 22, 2021 15:28
Partition Function
def Partition(goal, alist):
ways = [0] + [1] * (goal)
for options in alist:
for i in range(len(ways) - options):
ways[i + options] += ways[i]
return ways[-1] #-1 here if you don't want to include 4 + 0 as a partition of 4 for example
@igorvanloo
igorvanloo / continuedfracfunc.py
Created July 21, 2021 13:49
Continued Fraction Function
def continued_fraction(x):
m0 = 0
d0 = 1
a0 = math.floor(math.sqrt(x)) #These are the starting values
temp_list = [a0]
while True:
mn = int(d0*a0 - m0)
dn = int((x - mn**2)/d0)
an = int(math.floor((math.sqrt(x) + mn) / dn)) #new values
temp_list.append(an)
@igorvanloo
igorvanloo / divisorfunc.py
Last active July 22, 2021 15:29
Divisor Function
def Divisors(x):
divisors = []
for i in range(1, int(math.sqrt(x)) + 1):
if x % i == 0:
divisors.append(i)
divisors.append(int(x/i))
divisors.remove(x) #Remove this line if you want to include x as a divisor of x
return (divisors)
@igorvanloo
igorvanloo / digitsum.py
Created July 21, 2021 13:37
Digit sum Function
#x % 10 will be the first digit, then we x // 10 to remove it and repeat
def sum_digits(x):
totalsum = 0
while x != 0:
totalsum += x % 10
x = x // 10
return totalsum
@igorvanloo
igorvanloo / basechanger.py
Created July 21, 2021 13:36
Base Changing Function
def numberToBase(n, b):
if n == 0:
return [0]
digits = []
while n != 0:
digits.append(int(n % b))
n //= b
return digits[::-1]
@igorvanloo
igorvanloo / fibgen.py
Created July 21, 2021 13:35
Fibonacci Generator
def fibonnaci(n): #Finds the nth fibonnaci number
v1, v2, v3 = 1, 1, 0 # initialise a matrix [[1,1],[1,0]]
for rec in bin(n)[3:]: # perform fast exponentiation of the matrix (quickly raise it to the nth power)
calc = v2*v2
v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
if rec=='1':
v1, v2, v3 = v1+v2, v1, v2
return v2
@igorvanloo
igorvanloo / isprime.py
Created July 21, 2021 13:33
Is prime Function
def is_prime(x):
if x <= 1:
return False
elif x <= 3:
return True
elif x % 2 == 0:
return False
else:
for i in range(3, int(math.sqrt(x)) + 1, 2):
if x % i == 0: