Introduction+correct algorithm for ideal

Signed-off-by: Maxim Limonov <matemaks@ya.ru>