用函数求1-100内的质数的和(用sql 2000 做)

解决方案 »

  1.   

    --海爷以前写的。。
    declare @t table (
    a int
    )insert @t select a+10*b+1
    from (
    select 1 as a
    union all select 2
    union all select 3
    union all select 4
    union all select 5
    union all select 6
    union all select 7
    union all select 8
    union all select 9
    union all select 0
    ) as a,(
    select 1 as b
    union all select 2
    union all select 3
    union all select 4
    union all select 5
    union all select 6
    union all select 7
    union all select 8
    union all select 9
    union all select 0
    ) as b
    where a+10*b+1<>1delete a
    from @t a,@t b
    where a.a % b.a =0
    and a.a<>b.aselect * from @t order by a--结果
    a           
    ----------- 
    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 行)
      

  2.   

    declare @s int,@i int,@j int,@k int
    select @s=1,@i=3 
    while @i<=100
    begin 
        select @j=2,@k=0 
        while @j<=@i/2
        begin 
           if @i%@j=0 
               begin 
                   select @k=1 
                   break 
               end 
            select @j=@j+1 
        end 
        if @k=0 
        begin 
            select @s=@s+@i 
        end 
       select @i=@i+1 
    end 
    print @s--1059
      

  3.   

    --补1
    declare @s int,@i int,@j int,@k int
    select @s=2,@i=3 
    while @i<=100
    begin 
        select @j=2,@k=0 
        while @j<=@i/2
        begin 
           if @i%@j=0 
               begin 
                   select @k=1 
                   break 
               end 
            select @j=@j+1 
        end 
        if @k=0 
        begin 
            select @s=@s+@i 
        end 
       select @i=@i+1 
    end 
    print @s
    --1060
      

  4.   


    declare @var int,@s int,@a int,@temp int
    set @var=3
    set @s=2
    while @var<101
      begin
       set @a=2
       set @temp=0
       while @a<=@var/2
        begin 
           if  @var%@a=0  
             begin
              set @temp=1
              break;
             end
        set @a=@a+1
       end
      if @temp=0
           begin 
           set @s=@s+@var
           end  
      set @var=@var+1
      end
    print @s1060
      

  5.   

    1也进去,就这样:
    declare @s int,@i int,@j int,@k int
    select @s=3,@i=3 
    while @i<=10
    begin 
        select @j=2,@k=0 
        while @j<=@i/2
        begin 
           if @i%@j=0 
               begin 
                   select @k=1 
                   break 
               end 
            select @j=@j+1 
        end 
        if @k=0 
        begin 
            select @s=@s+@i 
        end 
       select @i=@i+1 
    end 
    print @s
    --18