RosettaCodeData/Task/Metallic-ratios/DuckDB/metallic-ratios-3.duckdb
2025-08-11 18:05:26 -07:00

28 lines
695 B
Text

# ix serves both as a rowid and as the count of the number of
# iterations required to achieve the corresponding ratio
create or replace function metallic_ratio_to_quiescence(b) as (
with recursive cte as (
select
-1::HUGEINT as ix,
1::HUGEINT as previ,
1::HUGEINT as i,
1::HUGEINT as j,
1::DOUBLE as prevratio,
1::DOUBLE as ratio
union all
select
ix+1,
i as previ,
j as i,
i + b*j as j,
ratio as prevratio,
(i::DOUBLE / previ) as ratio
from cte
where (ix < 4 or prevratio != ratio)
)
select last((ix, ratio))
from cte
);
select b, name, metallic_ratio_to_quiescence(b)
from metals;