The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
–The prime factors of 13195 are 5, 7, 13 and 29.
–What is the largest prime factor of the number 600851475143?
— this number is bigger than my database
— so we will add it
CREATE PROCEDURE [dbo].[FactorNumber]
@number AS BIGINT,
@originalnumber AS BIGINT
DECLARE @isPrime BIT
SET @isPrime = 1
DECLARE @factor BIGINT
SET @factor = FLOOR(SQRT(@number))
DECLARE @divisor BIGINT
WHILE @factor > 1
IF @number % @factor = 0
EXEC FactorNumber @factor, @originalnumber
— can’t do the math in the function call
— so I used another variable
SET @divisor = @number / @factor
EXEC FactorNumber @divisor, @originalnumber
SET @isPrime = 0
SET @factor = @factor – 1
— here we store the information for later
IF @isPrime = 1
INSERT INTO NumberFactor (number, factor) VALUES (@originalnumber, @number)
–here are the remaining steps to get the answer
–exec FactorNumber 600851475143, 600851475143
–select MAX(factor) from NumberFactor where number = 600851475143
I’ve included the procedure and the select statement here because this number exceeded my number list. It follows the same philosophy as some of the previous examples except that it inserts its values into a table instead of returning them to the calling function. This is why we need to pass the original number in as well.