Página 1 de 1

[Dica] Select dos NÚMEROS PRIMOS

Enviado: Seg, 31 Out 2005 12:32 pm
por dr_gori
Olá pessoal... Pra quem não sabe, eu estudo Matemática aplicada a Informática na ULBRA. (ou seja, adoro cálculos!). Já fazia um tempo que eu queria montar um select que gera os números primos. Finalmente, hoje eu consegui!!! :-D

SQL DOS NÚMEROS PRIMOS

Selecionar tudo

select z.zz PRIMOS
from 
 (select rownum+1 zz
  from user_tables
  where rownum < 100
 ) z
where zz not in (
                select y.bb
                from (select rownum+1 aa from user_tables a where rownum < 100) x
                ,    (select rownum+1 bb from user_tables b where rownum < 100) y
                where bb<>aa
                  and mod(bb, aa)=0
                  and aa<=bb
                )
/
Aí vai ele:

Selecionar tudo

SQL> select z.zz PRIMOS
  2  from 
  3   (select rownum+1 zz
  4    from user_tables
  5    where rownum < 100
  6   ) z
  7  where zz not in (
  8                  select y.bb
  9                  from (select rownum+1 aa from user_tables a where rownum < 100) x
 10                  ,    (select rownum+1 bb from user_tables b where rownum < 100) y
 11                  where bb<>aa
 12                    and mod(bb, aa)=0
 13                    and aa<=bb
 14                  )
 15  /

   PRIMOS
---------
        2
        3
        5
        7
       11
       13
       17
       19
       23
       29
       31
       37
       41
       43
       47
       53
       59
       61
       67
       71
       73
       79
       83
       89
       97

25 rows selected.
SQL> 

Enviado: Ter, 01 Nov 2005 11:04 am
por anderson
ai dr_gori, parabéns, cada volta e meia você coloca umas dicas muito interessantes...
algumas são curiosidades mas outras são de grande valia para quem trabalha com oracle....

[]´s

Enviado: Ter, 01 Nov 2005 11:14 am
por dr_gori
Valeu Anderson!

Realmente, procuro colocar aqui coisas que podem beneficiar os parceiros do oracle e também porque eu não quero esquecer essas dicas! :-) (eu mesmo vivo consultando esse forum e acho dicas que eu mesmo dei que nem lembrava mais!) :-o

Obrigado a todos que colaboram também com o fórum, seja pra dar uma dica, ou mesmo comentar algo!!!!

[]´s

Enviado: Seg, 13 Out 2008 6:21 pm
por dr_gori
Hoje, descobri outras formas de retornar os números primos em um SQL:

Selecionar tudo

SQL> select l prime_number
  2    from (select level l from dual connect by level <= 100)
  3       , (select level m from dual connect by level <= 100)
  4   where m<=l
  5   group by l
  6  having count(case l/m when trunc(l/m) then 'Y' end) = 2
  7   order by l
  8  /

                          PRIME_NUMBER
--------------------------------------
                                     2
                                     3
                                     5
                                     7
                                    11
                                    13
                                    17
                                    19
                                    23
                                    29
                                    31
                                    37
                                    41
                                    43
                                    47
                                    53
                                    59
                                    61
                                    67
                                    71
                                    73
                                    79
                                    83
                                    89
                                    97

Outra forma mais rápida que produto cartesiano: (10g apenas)

Selecionar tudo

SQL> with t as (select level l from dual connect by level <= 100)
  2  --
  3  SELECT l prim_num FROM
  4     (select * from t
  5       model
  6        dimension by (l dim)
  7        measures (l,2 temp)
  8         rules iterate (1000000) until (power(temp[1],2)>100)
  9           (l[DIM>TEMP[1]]=decode(mod(l[CV()],temp[1]),0,null,l[CV()]),
 10            temp[1]=min(l)[dim>temp[1]])
 11     )
 12  WHERE l IS NOT NULL
 13  /

  PRIM_NUM
----------
         1
         2
         3
         5
         7
        11
        13
        17
        19
        23
        29
        31
        37
        41
        43
        47
        53
        59
        61
        67
        71
        73
        79
        83
        89
        97

26 rows selected

SQL> 

Enviado: Seg, 29 Dez 2008 4:29 pm
por Toad
Fiz uma procedure com a dica, ficou assim:

Selecionar tudo

/* imaginando que a package PKG_UTIL exista e nela tenha sido declarado um REF CURSOR chamado C_CURSOR */

create or replace procedure NUM_PRIMOS(LIMITEMAX IN NUMBER
                                     , p_retorno OUT PKG_UTIL.C_CURSOR) is

                            
begin
   
   OPEN p_retorno for 
   select P.primos
   from (select rownum+1 primos
             from user_tables
             where rownum < LIMITEMAX
             ) P
       where P.PRIMOs not in ( select y.bb
                               from ( select rownum+1 aa from user_tables a where rownum < LIMITEMAX) x
                               ,    (select rownum+1 bb from user_tables b where rownum < LIMITEMAX) y
                               where bb<>aa
                               and mod(bb, aa)=0
                               and aa<=bb
                             ) ; 
  
end NUM_PRIMOS;