ported from euler repository