0

I'm experienced with Python a little, however, still doesn't understand how to use all() and any(). I'm trying to solve a problem;

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

My algorithm was first like this;

tp = (1,2,3,4,5,6,7,8,9,10,
      11,12,13,14,15,16,17,
      18,19,20) #I used tuple so I thought process may faster than list

for x in range(100,100000,2):
    for t in tp:
        if x%t==0:
            print(x)

However, before I run the script I realized that my algorithm is wrong because number may divisible by all of the numbers in the tuple. Then I remember all() function, I tried to change my codes like;

if all(x%t==0):
    print(x)

But I got TypeError: 'bool' object is not iterable error. Probably I have never used all() and any() before, just saw some examples and I didn't understand. Could anyone explain me clearly? Then I can solve this problem.

dively
  • 11
  • 2
  • have a look at this [SO link](http://stackoverflow.com/q/19389490/1082673) – lukik Jan 29 '15 at 20:13
  • It’s worth nothing that there are a few languages (R …) where your approach would directly work. However, Python does not auto-vectorise operations. This has got nothing to do with `all` (which is conceptually correct here), it’s just that you unfortunately cannot write `x % t == 0` when `x` is a list. You need to manually map or iterate over each element of the list. – Konrad Rudolph Jan 29 '15 at 20:19
  • 1
    This is not an answer to your question about `all()-any()`, but regarding the X of your [XY-problem](http://xyproblem.info/). What you need, is known as "[least common multiple](http://en.wikipedia.org/wiki/Least_common_multiple)", and calculated via a simple formula without having to search for a solution among million candidates (even with such a great optimization as a tuple instead of a list). – bereal Jan 29 '15 at 20:22
  • @bereal Yes thanks you're right the problem is about least common multiples basically – dively Jan 29 '15 at 20:28

3 Answers3

2

As all() function accept an iterable as its argument you can pass a generator within , also as you are in python 3 you can use range(1,20) that return a generator instead the tuple :

>>> for x in range(100,100000,2):
...     if all(x%t==0 for t in range(1,21)):
...        print (x)
... 
>>>
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Thanks for the answer but are you sure is this correct? I tried with this number `100000000` and there is no result. Number is so big so probably codes are wrong – dively Jan 29 '15 at 20:21
  • @dively you're welcome . its not incorrect you can try with `>>> for x in range(100,100000,2): ... if all(x%t==0 for t in range(1,11)): ... print (x) ... 2520 5040 7560 10080 12600 15120 17640 20160 22680 25200 27720 30240 32760 35280 37800 40320 42840 45360 47880 50400 52920 55440 57960 60480 63000 65520 68040 70560 73080 75600 78120 80640 83160 85680 88200 90720 93240 95760 98280 ` – Mazdak Jan 29 '15 at 20:22
  • You're right,codes are working. Well then probably the number that I'm looking is HUGE. Any suggestion for algorithm? T_T – dively Jan 29 '15 at 20:26
  • @dively mmm . this could be complecated , but as a more efficient way its better to use `any` tiil it find an number in specific range that `x%t !=0` it return false ! – Mazdak Jan 29 '15 at 20:32
  • Hah! I found the number, I realized that actually my tuple must be `(11,12,13,14,15,16,17,18,19,20)` because before these numbers, all numbers can be divisible. Like if a number divisible by 20 then it may divisible by 2,4,5,10 too. Number is `232792560`. Thanks for the answer again! – dively Jan 29 '15 at 20:35
1

all() and any() require an iterable parameter to be passed to them.

  • all() returns true if and only if all values in the iterable are truthy.
  • any() returns true if any one value in the iterable is truthy.

For your specific problem, you would likely be better off using filter instead, since that can support both an iterable and a function.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

any() and all() take lists, not equations. While the concepts come from mathematics, these functions operate on lists of values. Maybe you want:

for x in range(100,10000,2):
    if all([x%t==0 for t in tp]):
        print(x)
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82