0

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!

3
  • 2
    Are you looking for itertools.combinations? Commented Jan 25, 2023 at 19:21
  • 3
    or itertools.product() as I don't really think .combinations() is what you are after given you have differing lists to combine. Commented Jan 25, 2023 at 19:22
  • 1
    @JonSG You're right, I was too quick to comment lol Commented Jan 25, 2023 at 19:34

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.