2

I have got a function:

def euler9():
    for b in range(1, 500):
        a = (500000 - 1000 * b) / (1000 - b)
        if a % 1 == 0:
            print(b * a * (1000 - a - b))

And I want to make it in one line like

x*x for x in range(1,1)

This is what I have done:

def euler9():
    print([b * a * (1000 - a - b) for b in range(1, 500) for a in (500000 - 1000 * b) / (1000 - b) if a % 1 == 0])

but I do not know what am I doing wrong. I have got an error: TypeError: 'float' object is not iterable

2
  • 1
    It looks like you're trying to see whether 1000 - b divides 500000 - 1000 * b. If so, you should check whether (500000 - 1000 * b) % (1000 - b) == 0, rather than performing a floating-point division and checking whether the result has a fractional part. Floating-point operations involve rounding error that you shouldn't subject yourself to for a purely-integer operation. Commented May 13, 2016 at 20:17
  • It looks way worse in a list comprehension and it will be less efficient since you have to calculate the same things twice. Commented May 13, 2016 at 20:19

3 Answers 3

1

for is for iteration (looping). When you say for b in range(1, 500) you are not setting b = range(1, 500), since that would make b a list. You are extracting each individual value and using them one at a time. You cannot extract values from a float.

Python has no syntax for simple assignment in list comprehensions, but you can work around that by putting the value inside a single-element list, thus making it iterable:

[b * a * (1000 - a - b) for b in range(1, 500) for a in [(500000 - 1000 * b) / (1000 - b)] if a % 1 == 0]

(You can put print(...) around the initial expression if you want but I assume you want to actually use the values)

But don't ever do this, it's hard to read and unnecessary.

Sign up to request clarification or add additional context in comments.

Comments

1

Here for a in (500000 - 1000 * b) / (1000 - b) you are trying to iterate over a float number which is the result of a devision. As a quick fix try this:

def euler9():
    print([b * ((500000 - 1000 * b) / (1000 - b)) * (1000 - ((500000 - 1000 * b) / (1000 - b)) - b) 
           for b in range(1, 500) if ((500000 - 1000 * b) / (1000 - b)) % 1 == 0])

But, as you see it gets a bit messy that way, and it is recommended to use loops instead of list comprehension when things get complicated.

1 Comment

but here you duplicate the same equalation
-1

Turn it into a generator by changing print to yield:

def euler9():
    for b in range(1, 500):
        a = (500000 - 1000 * b) / (1000 - b)
        if a % 1 == 0:
            yield (b * a * (1000 - a - b))

Then you can access it as a list comprehension:

print [x for x in euler9()]

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.