Iterating through multiple arguments in a function?
Question:
How would I go about iterating through this function so that it tries all possible combinations where a, b, c, d are a range of numbers where:
a = 20 to 40, b = 80 to 100, c = 100 to 120, d = 120 to 140
def euler(a,b,c,d):
my_dict = {'A1':[],'A2':[],'A3':[],'A4':[],'Number': []}
y = a**5 + b**5 + c**5 + d**5
for n in range(140,161):
if n**5 == y:
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
else:
pass
Essentially I want to iterate through all combinations to find a match between a b c and d.
Any thoughts? Thanks in advance!
Answers:
Update: Here’s a slightly optimized version by precomputing the 5th power of each number and removing unreachable values of n
. (Original version down below)
from itertools import product
def euler(a, b, c, d):
y = A5[a] + B5[b] + C5[c] + D5[d]
if y in N:
n = N.index(y) + 140
my_dict = {'A1': [], 'A2': [], 'A3': [], 'A4': [], 'Number': []}
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
# generate lists of numbers to iterate
A = list(range(20, 41))
B = list(range(80, 101))
C = list(range(100, 121))
D = list(range(120, 141))
# precompute 5th powers of a, b, c, d
A5 = {a: a**5 for a in A}
B5 = {b: b**5 for b in B}
C5 = {c: c**5 for c in C}
D5 = {d: d**5 for d in D}
# precompute 5th powers of n, removing unreachable values
N = [n**5 for n in range(140, 161) if n**5 <= 40**5 + 100**5 + 120**5 + 140**5]
for a, b, c, d in product(A, B, C, D):
if res:= euler(a, b, c, d):
print(res)
Original: Here’s a simple implementation (thanks to @JonSG, it was product
, not combinations
!)
from itertools import product
def euler(a,b,c,d):
my_dict = {'A1':[],'A2':[],'A3':[],'A4':[],'Number': []}
y = a**5 + b**5 + c**5 + d**5
for n in range(140,161):
if n**5 == y:
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
else:
pass
A = range(20, 41)
B = range(80, 101)
C = range(100, 121)
D = range(120, 141)
for a, b, c, d in product(A, B, C, D):
res = euler(a, b, c, d)
if res:
print(res)
The operation you want is the cartesian product. itertools.product
is a function you can use
for a, b, c in itertools.product(range(20,40), range(80, 100), range(100, 120)
(however, the number of combinations get impractical very fast, and maybe you need another approach)
How would I go about iterating through this function so that it tries all possible combinations where a, b, c, d are a range of numbers where:
a = 20 to 40, b = 80 to 100, c = 100 to 120, d = 120 to 140
def euler(a,b,c,d):
my_dict = {'A1':[],'A2':[],'A3':[],'A4':[],'Number': []}
y = a**5 + b**5 + c**5 + d**5
for n in range(140,161):
if n**5 == y:
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
else:
pass
Essentially I want to iterate through all combinations to find a match between a b c and d.
Any thoughts? Thanks in advance!
Update: Here’s a slightly optimized version by precomputing the 5th power of each number and removing unreachable values of n
. (Original version down below)
from itertools import product
def euler(a, b, c, d):
y = A5[a] + B5[b] + C5[c] + D5[d]
if y in N:
n = N.index(y) + 140
my_dict = {'A1': [], 'A2': [], 'A3': [], 'A4': [], 'Number': []}
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
# generate lists of numbers to iterate
A = list(range(20, 41))
B = list(range(80, 101))
C = list(range(100, 121))
D = list(range(120, 141))
# precompute 5th powers of a, b, c, d
A5 = {a: a**5 for a in A}
B5 = {b: b**5 for b in B}
C5 = {c: c**5 for c in C}
D5 = {d: d**5 for d in D}
# precompute 5th powers of n, removing unreachable values
N = [n**5 for n in range(140, 161) if n**5 <= 40**5 + 100**5 + 120**5 + 140**5]
for a, b, c, d in product(A, B, C, D):
if res:= euler(a, b, c, d):
print(res)
Original: Here’s a simple implementation (thanks to @JonSG, it was product
, not combinations
!)
from itertools import product
def euler(a,b,c,d):
my_dict = {'A1':[],'A2':[],'A3':[],'A4':[],'Number': []}
y = a**5 + b**5 + c**5 + d**5
for n in range(140,161):
if n**5 == y:
my_dict['A1'].append(a)
my_dict['A2'].append(b)
my_dict['A3'].append(c)
my_dict['A4'].append(d)
my_dict["Number"].append(n)
return my_dict
else:
pass
A = range(20, 41)
B = range(80, 101)
C = range(100, 121)
D = range(120, 141)
for a, b, c, d in product(A, B, C, D):
res = euler(a, b, c, d)
if res:
print(res)
The operation you want is the cartesian product. itertools.product
is a function you can use
for a, b, c in itertools.product(range(20,40), range(80, 100), range(100, 120)
(however, the number of combinations get impractical very fast, and maybe you need another approach)