Simple algorithms in java and python