On Aug 3, 7:38 am, YannLC <yannlaiglecha...@gmail.com> wrote:
> Here is my version,

Thanks Yann!

I'm going to write a version in Cython.  A guy on the OEIS list got
about a 100x speed up with shedskin.  Pypy was close to the shedskin
mark, and he never finished his Cython port (properly) to judge Cython
fairly.

> def is_zk(a):
>     if is_prime(a): return False
>     d = divisors(a)

No need to call is_prime(a), divisors would only return length two;
otherwise the sieving is done twice.  (Unless Sage does something
extra smart behind the scenes.)

>     s = sum(d)
>     if s & 1 or s < 2*a:
>         return False
>     target = s//2 - a
>     if target < 2 : return True
>     m = [0] + [1] * target
>     d = [i for i in d[1:] if i <= target]
>     for wi in d:
>         for w in xrange(target, wi - 1, -1):
>             m[w] = max(m[w], m[w - wi] + wi)
>         if m[target] == target:
>             return True
>     return False

Thanks, I will review this part, I detect you may have more clarity in
your dynamic programming style.  In my defense, I ported someone
else's code. :-)  I still like returning the number instead of
True. :^)

Cheers,
Don

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to