```5bit数: 二进制中包含5个1的数，例如11111,1011011,100011101...

(1)求出a,b 之间的最小5bit数
(2)求出a,b 之间所有5bit数的和

```

1.直接法

```create or replace function fn_10to2(p_num number) return varchar2
is
l_str varchar2(32767):=null;
n1 number;
n2 number;
begin
n1 := p_num;
loop
n2 := mod(n1, 2);
n1 := trunc(n1 / 2);
l_str := to_char(n2) || l_str;
exit when n1 = 0;
end loop;
return l_str;
end fn_10to2;
/

var a number
var b number
exec :a:=10000
exec :b:=200000

SQL> with t as (select level n,fn_10to2(level)v from dual connect by level <=:b)
2  select min(n),sum(n) from t where regexp_count(v,'1')=5 and n between :a and :b;

MIN(N)               SUM(N)
-------------------- --------------------
10000            543234109

```

2.间接法

```SQL> with t as(select power(2,level-1)a from dual connect by level<=ceil(log(2,:b)))
2  ,r as(select a.a+b.a+c.a+d.a+e.a v
3  from t a,t b,t c,t d,t e
4  where a.a<b.a and b.a<c.a and c.a<d.a and d.a<e.a)
5  select min(v),sum(v) from r where v>=:a and v<=:b;

MIN(V)               SUM(V)
-------------------- --------------------
10000            543234109

SQL> exec :a:=0

PL/SQL 过程已成功完成。

SQL> exec :b:=1000000000

PL/SQL 过程已成功完成。

SQL> /

MIN(V)               SUM(V)
-------------------- --------------------
31       25476202472250

```