Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Prime Number Factorization

Author  Topic 

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-08-01 : 20:40:12
This piece of code depends on you having a present table with prime numbers ranging from 2 to 3037000493.
DECLARE	@Number BIGINT

SET @Number = 2020208534430421

SELECT Prime AS Number,
CAST(1 AS TINYINT) AS Items
INTO #Temp
FROM Primes
WHERE Prime <= SQRT(@Number)
AND @Number % Prime = 0

WHILE @@ROWCOUNT > 0
UPDATE #Temp
SET Items = Items + 1,
@Number = @Number / Number
WHERE @Number % Number = 0

SELECT Number,
Items
FROM #Temp

UNION ALL

SELECT @Number,
1
WHERE @Number > 1

DROP TABLE #Temp



N 56°04'39.26"
E 12°55'05.63"
   

- Advertisement -