请用SQL语句求下列两个数的最大公约数:::
4294967294 3465465468
------解决方案--------------------
- SQL code
/*求两个数的最大公约数*/create function GetGys(@num1 int,@num2 int)returns int --返回值asbegindeclare @times int --计数器declare @min int --存储两个数的较小者declare @result int --保存结果if(@num1>[email protected])set @[email protected]elseset @[email protected]set @[email protected]while(@times<[email protected]) --循环beginif(@[email protected]=0 and @[email protected]=0)beginset @[email protected]breakendset @[email protected]endreturn @resultend/*求两个数的最小公倍数*/create function GetGbs(@num1 int,@num2 int)returns intasbegindeclare @result int --结果declare @max int --保存两个数的大者declare @times intif @num1<[email protected]set @[email protected]elseset @[email protected]set @[email protected]while(@times>[email protected])beginif(@[email protected]=0 and @[email protected]=0)beginset @[email protected]breakendset @[email protected]+1endreturn @resultend 最后测试:运行select dbo.GetGys(15,20) as 最大公约数,dbo.GetGbs(15,20) as 最小公倍数显示结果:最大公约数 最小公倍数 5 60赞同23| 评论
------解决方案--------------------
- SQL code
GOCreate FUNCTION fn_num(@a BIGINT,@b BIGINT)RETURNS BIGINTASBEGIN DECLARE @c BIGINT SET @[email protected][email protected] WHILE @c>0 select @[email protected],@[email protected],@[email protected][email protected] RETURN @bENDGOSELECT dbo.fn_num(4294967294,3465465468)