create or replace function ranges(x, y, z, one, three, seven) as ( range(-three, ((1 + 3) ^ 3)::BIGINT, three) || range( -seven, 1 + seven, x) || range( 555, 1 + 550 - y) || range( 22,-1 -28, -three) || range(1927 , 1 + 1939 ) || range(x , y, z) || range((11 ^ x)::BIGINT, 1 + ( 11 ^ x)::BIGINT + one) ); create or replace function task(x,y,z, one, three, seven) as table ( with recursive lst as (select ranges(x,y,z, one, three, seven) as lst), cte as ( select 1 as ix, -- index into ranges() 0 as j, 0 as sum, 1 as prod -- start with a product of unity union all select ix + 1 as ix, lst.lst[ix] as j, (sum + (@j)) as sum, if ((@prod) < (2 ^ 27)::BIGINT and j != 0, prod * j, prod) as prod from cte, lst where ix <= 1 + length(lst) ) select last(sum) as sum, last(prod) as prod from cte ); from task(5, -5, -2, 1, 3, 7);