Run the following code to popula
You now need to create a table of primes and populate it with
at least
all primes smaller than the highest primorial you want to support (in our
case 19# = 9699690). You can use the following stored procedure to
create and populate the primes table:
-- procedure to populate primes table
if object_id('dbo.usp_populateprimes') is not null
drop proc dbo.usp_populateprimes;
go
create proc dbo.usp_populateprimes
@max as bigint = 3037000507,
@batchsize as
bigint =
1000000
as
set nocount on;
declare
@from as bigint,
@to as bigint,
@start as datetime,
@startbatch as
datetime,
@end as datetime,
@rc as bigint,
@totalrc as bigint,
@square as bigint,
@msg as varchar(500);
set @start = getdate();
set @totalrc =
0;
if object_id('dbo.primes') is null
begin
create table dbo.primes(p bigint not null primary key);
insert into dbo.primes(p) values(2);
insert into dbo.primes(p) values(3);
insert into dbo.primes(p) values(5);
insert into dbo.primes(p) values(7);
insert into dbo.primes(p) values(11);
insert into dbo.primes(p) values(13);
insert into dbo.primes(p) values(17);
insert into dbo.primes(p) values(19);
set @totalrc = @totalrc + 8;
end
set @to = (select max(p) from dbo.primes);
while @to <
@max
begin
set
@startbatch =
getdate();
if @to > 3037000499 set
@square = 9223372036854775807
else set @square = square(@to);
set @from = @to + 1;
set @to = @from + @batchsize - 1;
if @to > @square set @to = @square;
if @to > @max set @to = @max;
insert into dbo.primes(p)
select n
from
dbo.fn_nums(@from, @to) as nums
where n%2 > 0 and n%3 > 0 and n%5 > 0 and n%7 > 0
and n%11 > 0 and n%13 > 0 and n%17 > 0 and n%19 > 0
and not exists(select *
from
dbo.primes ps
where
ps.p > 19
and
ps.p <= cast(sqrt(nums.n) as bigint)
and
nums.n % ps.p = 0);
set @rc = @@rowcount;
set @totalrc = @totalrc + @rc;
set @end = getdate();
set @msg =
'range: '
+ cast(@from as varchar(20)) + ' - '
+ cast(@to as varchar(20)) + ', batch: ' +
+ cast(@rc as varchar(20)) + ' primes in ' +
+ cast(datediff(second, @startbatch, @end) as varchar(20))
+ ' seconds, total:'
+ cast(@totalrc as varchar(20)) + ' primes in ' +
+ convert(varchar(8), dateadd(second, datediff(second, @start, @end), 0), 114)
+ ' (hh:mm:ss).';
print @msg;
end
go
Run the following code to populate the primes table with all
primes
smaller than 19# (should take about two minutes for the following code
to run):
exec dbo.usp_populateprimes
@max = 9699690;
Prev. page
1
2
3
[4]
5
6
7
next page