new algorithm of Java & Groovy