[SQL] 利用函数(Function)判断输入的数是不是质数
2017-06-11 17:42
323 查看
作用: EN:Description: Determines if a given integer is a prime;
JP:入力したデータが素数かどうか判断する;
CN:判断输入的数是不是质数;
参照文献:http://bbs.csdn.net/topics/390081893
注:本文原创由`bluetata`发布于blog.csdn.net、转载请务必注明出处。
JP:入力したデータが素数かどうか判断する;
CN:判断输入的数是不是质数;
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========================================================= -- Author : http://blog.csnd.net/dietime1943 -- Create date: 06/11/2017 -- Description: EN:Determines if a given integer is a prime; -- JP:入力したデータが素数かどうか判断する; -- CN:判断输入的数是不是质数; /* SELECT dbo.udf_IsPrime(1) SELECT dbo.udf_IsPrime(9) SELECT dbo.udf_IsPrime(7867) */ -- ========================================================= CREATE FUNCTION [dbo].[udf_IsPrime] ( @NumberToTest int ) RETURNS bit AS BEGIN DECLARE @IsPrime bit, @Divider int -- To speed things up, we will only attempt dividing by odd numbers -- We first take care of all evens, except 2 IF (@NumberToTest % 2 = 0 AND @NumberToTest > 2) SET @IsPrime = 0 ELSE SET @IsPrime = 1 -- By default, declare the number a prime -- We then use a loop to attempt to disprove the number is a prime SET @Divider = 3 -- Start with the first odd superior to 1 -- We loop up through the odds until the square root of the number to test -- or until we disprove the number is a prime WHILE (@Divider <= floor(sqrt(@NumberToTest))) AND (@IsPrime = 1) BEGIN -- Simply use a modulo IF @NumberToTest % @Divider = 0 SET @IsPrime = 0 -- We only consider odds, therefore the step is 2 SET @Divider = @Divider + 2 END -- Return the result of the function RETURN @IsPrime END
参照文献:http://bbs.csdn.net/topics/390081893
注:本文原创由`bluetata`发布于blog.csdn.net、转载请务必注明出处。
相关文章推荐
- 通过一个函数,判断输入的数是不是素数
- 让用户输入一个正整数,判断该数是不是素数(质数)
- 201111621401-白乐乐-判断一个正整数是否为质数的算法。函数签名如下 int isPrime(long a) 输入:一个长整数a 输出:返回1(为质数),返回0(非质数)
- js中利用isNaN函数判断用户输入是否为数字
- 编写一个函数,判断一个数是不是素数。在主函数中输入一个整数,输出是否是素数的信息
- 利用if,else判断输入的是不是一个正整数
- 判断输入的整数是不是质数(素数)
- 通过一个函数,判断输入的数是不是素数
- 随机输入一个数,判断它是不是对称数(回文数)(如3,121,12321,45254)。不能用字符串库函数
- SQL查询时常用,但不熟悉的相关语句-----邮标,行号,临时表,类型转换,字符串函数,sql 判断语句,检查是不是数字,变量,sql split 函数
- js如何判断输入是否为正整数、浮点数等数字的函数
- SQL利用Case When Then多条件判断
- SQL里利用自定义函数Group By 数据得到字符串
- SQL Server function (函数)
- [导入]判断输入是否为中文的函数
- 判断是不是64bit 64位Windows的函数
- js入门·循环与判断/利用函数的简单实例/使用对象/列举对象属性的名称
- 判断输入是否为中文的函数
- SQL利用Case When Then多条件判断
- SQL內置Function--系统函数