Practical numbers: Difference between revisions
Content added Content deleted
(→Python: Faster version: Whoops) |
(→Python: Faster version: Check sum of factors can reach x-1.) |
||
Line 243: | Line 243: | ||
the sets. 720, another Practical number needs just 0.01% of its half a billion sets to prove it is Practical. |
the sets. 720, another Practical number needs just 0.01% of its half a billion sets to prove it is Practical. |
||
The inner loop is sensitive to the order of factors passed to the powerset generator and experimentation shows that reverse sorting the factors saves the most computation. |
The inner loop is sensitive to the order of factors passed to the powerset generator and experimentation shows that reverse sorting the factors saves the most computation.<br> |
||
An extra check on the sum of all factors has a minor positive effect too. |
|||
<lang python>def is_practical5(x: int) -> bool: |
<lang python>def is_practical5(x: int) -> bool: |
||
Line 258: | Line 258: | ||
f = sorted(factors5(x), reverse=True) |
f = sorted(factors5(x), reverse=True) |
||
if sum(f) < x - 1: |
|||
return False # Never get x-1 |
|||
ps = powerset(f) |
ps = powerset(f) |
||