C 练习实例36 之mysql实现

题目:求100之内的素数。

程序分析:质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数整除。

drop PROCEDURE if exists test;
 
create PROCEDURE test (  )  
 begin 

	   declare i,j,k  int;
		 set i=2;
		
			create TEMPORARY table tmp(rst int);

			while i<=100 do 
				 set k=convert( sqrt(i),SIGNED);
				 set j=2;
				 x:while j<=k do 
				    if i%j=0  then 
								LEAVE x;
						end if;
						set j=j+1;
					end while;
        
        if j>k then
					insert into tmp values(i);
				end if;
           
        

				set i=i+1;
			end  while;
    
      select *  from  tmp;
			drop  temporary table if exists tmp;

end;  

 

你可能感兴趣的:(mysql)