27 lines
822 B
Text
27 lines
822 B
Text
# Generate the hailstone sequence without an index
|
|
create or replace function hailstone(start) as table (
|
|
with recursive cte as (
|
|
select start as n
|
|
union all
|
|
select if( n % 2 = 0, n // 2, 3 * n + 1) as n
|
|
from cte
|
|
where n != 1
|
|
) from cte
|
|
);
|
|
|
|
# The length of the hailstone sequence of nn using count()
|
|
create or replace function hailstone_length(nn) as (
|
|
select count() as n from hailstone(nn)
|
|
);
|
|
|
|
# Return a tuple giving the seed and the hailstone sequence length
|
|
create or replace function hailstone_max_of_lengths(mx) as (
|
|
select max_by( (n,hailstone_length(n)), hailstone_length(n))
|
|
from range(1,mx+1) _(n)
|
|
);
|
|
|
|
# Table of lengths (e.g. for use with histogram())
|
|
create or replace function hailstone_lengths(mx) as table (
|
|
select hailstone_length(n) as length
|
|
from range(1,mx+1) _(n)
|
|
);
|