I was trying to find a nice article on number reduction, for example the one from sourceryforge could be useful. Last week I implemented a function which returns the reduction of any sequence, could be date or number. I was intending to apply it on prime numbers, but even if can be observed certain similarities between different chains, is difficult to believe this will lead anywhere.
CREATE FUNCTION dbo.NumberReduction(
@text varchar(50))
RETURNS smallint
/*
Purpose: reduces to number a sequence of digits
Parameters: @text varchar(50) - digits sequence
Notes: the function works with any sequnce of alphanumeric, not numeric values being ignored
Sample:
SELECT dbo.NumberReduction('11/17/2005')
SELECT dbo.NumberReduction('1234')
SELECT dbo.NumberReduction('1234 8993 90003 3456')
*/
BEGIN
DECLARE @index smallint
DECLARE @total int
SET @index = 1
SET @total = 0
--for each char in alphanumeric sequence
WHILE (@index<=len(@text))
BEGIN
IF IsNumeric(Substring(@text, @index, 1)) = 1
SET @total = @total + Cast(Substring(@text, @index, 1) as smallint)
SET @index = @index+1
END
--if the number is not in 1..9 range, it calls further the NumberReduction for the new total
IF len(@total)>1
SET @total = dbo.NumberReduction(@total)
RETURN @total
END
I tried to generate the prime numbers between 1 and 1000000 to analyze how many prime numbers are for each thousand.
SELECT PrimeNumber/1000 DataSet
, count(*) NumberPrimeNumbers
FROM PrimeNUmbers
GROUP BY PrimeNumber/1000
ORDER BY DataSet
As it seems the maximum number of prime numbers is reached for the first thousand and decreases.
DataSet*1000
Number Prime Numbers
-------------------- ------------------
0 (2..1000) 168
1 (1001..2000) 135
2 (2001..3000) 127
3 ........... 120
4 ........... 119
5 ........... 114
6 ........... 117
7 ........... 107
8 ........... 110
9 ........... 112
10 .......... 106
11 .......... 103
12 .......... 109
13 .......... 105
14 .......... 102
15 .......... 108
16 .......... 98
17 .......... 104
18 .......... 94
19 .......... 104
20 .......... 98
...........................
980 ......... 67
981 ......... 75
982 ......... 70
983 ......... 70
984 ......... 70
985 ......... 76
986 ......... 76
987 ......... 63
988 ......... 71
989 ......... 72
990 ......... 71
991 ......... 79
992 ......... 65
993 ......... 68
994 ......... 78
995 ......... 69
996 ......... 69
997 ......... 83
998 ......... 74
999 ......... 65
No comments:
Post a Comment