Talk:Weird numbers: Difference between revisions
Content added Content deleted
(→A faster and less ambitious algorithm ?: (changed an example, to show a sum of more than 2 parts)) |
m (→A faster and less ambitious algorithm ?: (Tests to show advantage of search thru descending numbers)) |
||
Line 25: | Line 25: | ||
def anySum(n, xs): |
def anySum(n, xs): |
||
'''First subset of xs found to sum to n. |
'''First subset of xs found to sum to n. |
||
(Probably more efficient where xs is sorted in |
(Probably more efficient where xs is sorted in |
||
order of magnitude)''' |
descending order of magnitude)''' |
||
def go(n, xs): |
def go(n, xs): |
||
if xs: |
if xs: |
||
Line 45: | Line 45: | ||
# Search for sum through descending numbers (more efficient) |
|||
print(anySum( |
print(anySum(196, range(100, 0, -1))) |
||
# -> [ |
# -> [100, 96] |
||
# Search for sum through ascending numbers (less efficient) |
|||
print(anySum(196, range(1, 101))) |
|||
# -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 25] |
|||
print(anySum(7, [6, 3])) |
print(anySum(7, [6, 3])) |
||
# -> []</lang> |
# -> []</lang> |