Interested in prime number factorization?This function returns all prime number factors for a given numberThis function was previously posted under a different topicCREATE FUNCTION dbo.fnGetPrimeFactors( @Number INT)RETURNS @Primes TABLE ( Prime INT )ASBEGIN IF @Number < 2 RETURN WHILE @Number % 2 = 0 BEGIN INSERT @Primes SELECT 2 SELECT @Number = @Number / 2 END DECLARE @PseudoPrimes BIGINT, @PseudoPrime BIGINT SELECT @PseudoPrime = 1, @PseudoPrimes = (SQRT(@Number) - 1) / 2, @Number = (@Number - 1) / 2 WHILE @PseudoPrime <= @PseudoPrimes IF (@Number - 2 * @PseudoPrime * @PseudoPrime - 2 * @PseudoPrime) % (2 * @PseudoPrime + 1) = 0 BEGIN INSERT @Primes SELECT 2 * @PseudoPrime + 1 SELECT @Number = (2 * @Number + 1) / (2 * @PseudoPrime + 1), @PseudoPrime = 1, @PseudoPrimes = (SQRT(@Number) - 1) / 2, @Number = (@Number - 1) / 2 END ELSE SELECT @PseudoPrime = @PseudoPrime + 1 IF @Number > 0 INSERT @Primes SELECT 2 * @Number + 1 RETURNEND
Peter LarssonHelsingborg, Sweden