SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Table Valued Function to factorize numbers up to 100 trillion

By Jonathan Roberts,

To use this function you will need a populated table of primes. See script: http://www.sqlservercentral.com/scripts/Prime+Numbers/155286/
Theoretically the maximum number that can be guaranteed to be factorized is the maximum value of a bigint (9223372036854775807) but to guarantee factorisation you will need all primes up to the square root of that value in the dbo.Primes table.
To factorise any number up to 100 trillion you will need a Primes table with all prime less than 10,000,000. To do this use the the script here to generate a table of 10 million primes.
Sampe call to factorise a number: 
SELECT * FROM dbo.PrimeFactors(99999820000081) 

Total article views: 289 | Views in the last 30 days: 2
Related Articles

Finding Primes

While it's not likely that many of you need to find prime numbers using T-SQL, it is an interesting ...


Table Valued Function to return a range of prime numbers

A fast table valued function to generate a list of prime numbers within the range specified by the p...


Prime Number Table Generator (new ver crunches to 1 Mil in <10.5 sec!)

Generates prime numbers and puts them into a table for future reference by adhoc queries and databas...


Creating Stored Procedure in SQL server 2000 for Printing Prime Numbers

"Trying to create a sp that prints Prime numbers upto 500"