Skip to content

Instantly share code, notes, and snippets.

View igorvanloo's full-sized avatar

Igor igorvanloo

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / p31.py
Created July 23, 2021 03:23
Problem 31
def compute():
totalways = 1
for a in range(0,3):
for b in range(0,(2-a)*2 + 1):
for c in range(0,math.ceil((2-a-b*0.5)*10)+1):
for d in range(0,math.ceil((2-a-b*0.5-c*0.2)*20)+1):
for e in range(0,math.ceil((2-a-b*0.5-c*0.2-d*0.1)*40)+1):
for f in range(0,math.ceil((2-a-b*0.5-c*0.2-d*0.1-e*0.05)*100)+1):
for g in range(0,math.ceil((2-a-b*0.5-c*0.2-d*0.1-e*0.05-f*0.02)*200)+1):
if a*100 + b*50 + c*20 + d*10 + e*5 + f*2 + g*1 == 200:
@igorvanloo
igorvanloo / p32.py
Created July 23, 2021 03:44
Problem 32
def compute():
testlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
values = set()
for x in range(9,99):
for y in range(99,999):
answer = x*y
if answer < 10000:
testingvalue = sorted(str(x) + str(y) + str(answer))
if testingvalue == testlist:
values.add(answer)
@igorvanloo
igorvanloo / p33.py
Created July 23, 2021 05:46
Problem 33
def compute():
values = []
numer = 1
denom = 1
for x in range(10,100):
for y in range(x+1, 100):
if x%10 != 0 and y%10 != 0:
value1 = str(x)
value2 = str(y)
for a in value1: