Last active
August 12, 2016 04:23
-
-
Save anch0vy/b0afee1cec48eaf1c9a8a8c351eb8280 to your computer and use it in GitHub Desktop.
test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
loopCallCouter = 0 | |
def loop(min_, max_, n, sum): | |
global loopCallCouter | |
loopCallCouter += 1 | |
if max_ * n < sum: | |
yield () | |
elif n == 1: | |
yield (sum,) | |
else: | |
r1 = min(max_, (sum / n) + 1) | |
for n1 in range(min_, r1): | |
for else_ in loop(n1, max_, n - 1, sum - n1): | |
if else_ is (): | |
break | |
else_ += (n1,) | |
yield else_ | |
def babo(min_, max_, n, sum_): | |
ret = [] | |
for nums in itertools.combinations(range(min_, max_), n): | |
if sum(nums) == sum_: | |
ret.append(nums) | |
return ret | |
if __name__ == '__main__': | |
n = 0 | |
for x in loop(0, 74, 13, 60): | |
n += 1 | |
# print sorted(x) | |
print 'loopCallCounter:', loopCallCouter | |
print 'resultCount:', n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment