RosettaCodeData/Task/Arithmetic-geometric-mean/DuckDB/arithmetic-geometric-mean-2.duckdb
2025-08-11 18:05:26 -07:00

14 lines
352 B
Text

create or replace function agm(a0, g0) as (
with recursive
rec as (
select a0::DOUBLE as a, g0::DOUBLE as g, a - g as diff
union all
select a, g, (a - g) as diff
from ( select ((a + g)/2) as a, sqrt(a * g) as g, diff
from rec )
where diff > 1e-15 )
select (a + g)/2
from rec
where diff <= 1e-15
limit 1
);