Talk:Weird numbers: Difference between revisions

m
→‎A faster and less ambitious algorithm ?: (Tests to show advantage of search thru descending numbers)
(→‎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:
def anySum(n, xs):
'''First subset of xs found to sum to n.
(Probably more efficient where xs is sorted in descending
descending order of magnitude)'''
def go(n, xs):
if xs:
Line 45:
 
 
# Search for sum through descending numbers (more efficient)
print(anySum(7196, [1range(100, 10, -1, 1, 1, 3])))
# -> [1100, 1, 1, 1, 396]
 
# 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]))
# -> []</lang>
9,655

edits