Reply To: Programming
The mystery of the silver bullet › Forums › The Intelligence Room › Programming › Reply To: Programming
X = 29031
Here’s a python solution; I challenged myself to write it all in one line. (I don’t usually write unreadable code like this.)
print(next(filter(lambda x: all(x % (4, 5, 7, 11, 17)[i] == (3, 1, 2, 2, 12)[i] for i in range(5)), range(5000, 50000))))
I’ll break it down step by step.
First, I generate a list of numbers from 5000-50000 with range(5000, 50000)
.
Next, we check each number in turn. I check the remainder division of the number with each divisor we care about, and check it against the expected result.
That’s this section: all(x % (4, 5, 7, 11, 17)[i] == (3, 1, 2, 2, 12)[i] for i in range(5))
. It uses list comprehension for efficiency.
We filter out each number from the list by using the in-built filter function: filter(lambda x: ...)
. Lambda simply creates an anonymous function for this purpose.
Finally, we print out the first (and only) number using print(next(...))
. It is important to use the next keyword rather than indexing (e.g. list[0]
), as we have created a filter object that isn’t subscriptable.
Manually checking the result:
29031 / 4 = 7257r3
29031 / 5 = 5806r1
29031 / 7 = 4147r2
29031 / 11 = 2639r2
29031 / 17 = 1707r12
Naturally, I also decided to write a one-liner for this:
print(list(divmod(29031, v) for v in (4, 5, 7, 11, 17)))