20 lines
669 B
Text
20 lines
669 B
Text
# `seed` should be a positive integer or numeric string representing a positive integer;
|
|
# `mx` is the maximum number of rows
|
|
# Output: a table of the "middle-squares" as HUGEINT values
|
|
create or replace function middle_square(seed, mx) as table (
|
|
with recursive n as (select length(seed::VARCHAR) as n, (n // 2) as n2),
|
|
cte as (
|
|
select 0 as ix,
|
|
seed::VARCHAR as ms
|
|
from n
|
|
union all
|
|
select ix+1 as ix,
|
|
format('{:0>{}}', (ms::HUGEINT * ms::HUGEINT)::VARCHAR, 2*n)[ n2 + 1 : n2 + n ] as ms
|
|
from cte, n
|
|
where ix < mx and NOT ms ~ '0*'
|
|
)
|
|
select ms::HUGEINT
|
|
from cte offset 1
|
|
);
|
|
|
|
from middle_square(675248, 10) _(ms);
|