0% found this document useful (0 votes)
413 views7 pages

Prime Numbers

This document discusses prime numbers and describes a procedure used to find prime numbers up to 10 million. It summarizes that the earliest known largest prime number was found between 1320-1350 and contains 39 digits. It then outlines a SQL procedure to find prime numbers up to 10 million by testing divisibility and inserting qualifying numbers into a table. Analysis of the results is also provided, such as the count of prime numbers by last digit and difference between consecutive primes.

Uploaded by

JP Vijaykumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
413 views7 pages

Prime Numbers

This document discusses prime numbers and describes a procedure used to find prime numbers up to 10 million. It summarizes that the earliest known largest prime number was found between 1320-1350 and contains 39 digits. It then outlines a SQL procedure to find prime numbers up to 10 million by testing divisibility and inserting qualifying numbers into a table. Analysis of the results is also provided, such as the count of prime numbers by last digit and difference between consecutive primes.

Uploaded by

JP Vijaykumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRIME NUMBERS

Written JP Vijaykumar
Date
Jan 4th 2014
Modified
Jan 5th 2014
Modified
Jan 8th 2014
The earliest known largest prime number is 1701411834604692317316873037158841057
27,
containing 39 digits, was shown by Lord Sriparada Srivallabha, during his lifeti
me
between 1320 - 1350.
This number is a product of 2**127 -1 (2 to the power 127 minus 1).And 127 is a
prime.
[Sreepada Sreevallabha Charithamruthamu, Chapter - 4]
---------------------------------------------------------------------------------THE FOLLOWING PROCEDURE WAS TESTED ON 11.2.0.3.0 VERSION DB
-------------------------------------------------------------------------------connect saketh/saketh
set serverout on size 1000000 timing on
declare
v_num number;
v_lst number;
v_sqr number;
v_str char(3);
/*****************************************
drop sequence prime_seq;
CREATE SEQUENCE prime_seq
START WITH
12
INCREMENT BY 1
NOCACHE
NOCYCLE;
CREATE TABLE prime_num
(prime_num number,
primary key(prime_num)
)
organization index
partition by range(prime_num)
(
PARTITION part1 VALUES LESS
PARTITION part2 VALUES LESS
PARTITION part3 VALUES LESS
PARTITION part4 VALUES LESS
truncate table prime_num;

THAN
THAN
THAN
THAN

insert
insert
insert
insert
insert
insert

into
into
into
into
into
into

prime_num
prime_num
prime_num
prime_num
prime_num
prime_num

values(1);
values(2);
values(3);
values(5);
values(7);
values(11);

( 2500000)
( 5000000)
( 7500000)
(10000000)

TABLESPACE
TABLESPACE
TABLESPACE
TABLESPACE

users,
users,
users,
users )

commit;
set head off
select
'select prime_seq.nextval from dual;'||chr(10)||
'drop sequence prime_seq;'||chr(10)||
'create sequence prime_seq start with '||(max(prime_num) +1)||' nomaxvalue cache
10000;'
from prime_num;
*****************************************/
begin
execute immediate 'select prime_seq.nextval,sqrt(prime_seq.nextval),
substr(prime_seq.nextval,length(prime_seq.nextval),1)
from dual ' into v_num,v_sqr,v_lst;
--v_num:=prime_seq.nextval;
while v_num < 10000000 loop
begin
if ((mod(v_sqr,1)
(v_lst
V_lst
v_lst
v_lst
v_lst
v_lst
) then

<> 0 ) AND
<> 0
OR
<> 2
OR
<> 4
OR
<> 5
OR
<> 6
OR
<>8)

v_str:= 'YES';
for c1 in (select prime_num from prime_num where prime_num >= 2
and prime_num <= (v_sqr +1 ) order by prime_num) loop
begin
if ( mod(v_num,c1.prime_num) = 0 ) then
v_str:='NO';
exit;
--HERE IF ANY NUMBER CAN DIVIDE THE NUMBER, STOP FURTHER PROCESSING
end if;
exception
when others then
dbms_output.put_line(v_num||' '||sqlerrm);
end;
end loop;
end if;
if ( v_str = 'YES') then
execute immediate 'insert into prime_num values( :1)' using v_nu
m;
commit;
end if;
execute immediate 'select prime_seq.nextval,sqrt(prime_seq.nextval),

substr(prime_seq.nextval,length(prime_seq.nextval),1)
from dual ' into v_num,v_sqr,v_lst;
exception
when others then
dbms_output.put_line(v_num||' '||sqlerrm);
end;
end loop;
end;
/
The above procedure can be run in multiple sessions in parallel.
--PRIME NUMBERS ANALYSIS
--COUNT OF PRIME NUMBERS UNDER 10 MILLION, GROUP BY THEIR LAST DIGIT
select substr(prime_num,length(prime_num),1), count(*) from prime_num
group by substr(prime_num,length(prime_num),1) order by 1;
S COUNT(*)
- ---------1
166105
2
1
3
166230
5
1
7
166211
9
166032
6 rows selected.
--COUNT OF PRIME NUMBERS UNDER 10 MILLION, GROUP BY SUM OF ALL DIGITS
(UNTILL ALL THE DIGITS IN THE PRIME NUMBER BECOMES A SINGLE DIGIT)
select num_tot(prime_num),count(*) tot_num from prime_num
group by num_tot(prime_num) order by 1;
NUM_TOT(PRIME_NUM)
TOT_NUM
------------------ ---------1
110773
2
110836
3
1
4
110743
5
110760
7
110679
8
110788
7 rows selected.
--THE DIFFERENCE BETWEEN ANY TWO ADJACENT PRIME_NUMBERS AND HOW MANY
--TIMES SUCH DIFFERENCES OCCURED IN PRIME_NUMBERS UNDER 10 MILLION
select
with t
select
from t

diff,count(*) from (
as (select rownum row_num,prime_num from prime_num order by prime_num)
a.prime_num,b.prime_num - a.prime_num diff
a, t b where a.row_num + 1 = b.row_num ) group by diff order by 1;

DIFF COUNT(*)
---------- ----------

1
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
102
104
106
108
110
112
114
116
118

2
58980
58621
99987
42352
54431
65513
35394
25099
43851
22084
19451
27170
12249
13255
21741
6364
6721
10194
4498
5318
7180
2779
2326
3784
2048
1449
2403
1072
1052
1834
543
559
973
358
524
468
218
194
362
165
100
247
66
71
141
37
39
65
29
36
34
21
12
26
11
11
11
7
4

120
122
124
126
128
130
132
134
136
138
140
146
148
152
154

10
3
4
8
2
1
5
1
2
2
2
1
2
1
1

75 rows selected.

--TOTAL NUMBER OF PRIME NUMBERS UPTO 10 MILLION


select count(1) from prime_num;
COUNT(1)
---------664580
--WHAT IS THE MAX(PRIME NUMBER) UNDER 10 MILLION
select max(prime_num) from prime_num;
MAX(PRIME_NUM)
-------------9999991
--PLOTTING THE DIFFERENCE BETWEEN ANY TWO
CONSECUTIVE PRIME NUMBERS INTO A SPREAD SHEET.
select a_prime||','||b_prime||','||
decode(diff,1,diff,null) ||','||
decode(diff,2,diff,null) ||','||
decode(diff,4,diff,null) ||','||
decode(diff,6,diff,null)||','||
decode(diff,8,diff,null) ||','||
decode(diff,10,diff,null) ||','||
decode(diff,12,diff,null)||','||
decode(diff,14,diff,null) ||','||
decode(diff,16,diff,null)||','||
decode(diff,18,diff,null) ||','||
decode(diff,20,diff,null) ||','||
decode(diff,22,diff,null) ||','||
decode(diff,24,diff,null) ||','||
decode(diff,26,diff,null) ||','||
decode(diff,28,diff,null) ||','||
decode(diff,30,diff,null) ||','||
decode(diff,32,diff,null) ||','||
decode(diff,34,diff,null) ||','||
decode(diff,36,diff,null)||','||
decode(diff,38,diff,null) ||','||

decode(diff,40,diff,null) ||','||
decode(diff,42,diff,null) ||','||
decode(diff,44,diff,null) ||','||
decode(diff,46,diff,null)||','||
decode(diff,48,diff,null) ||','||
decode(diff,50,diff,null)||','||
decode(diff,52,diff,null) ||','||
decode(diff,54,diff,null) ||','||
decode(diff,56,diff,null) ||','||
decode(diff,58,diff,null) ||','||
decode(diff,60,diff,null) ||','||
decode(diff,62,diff,null)||','||
decode(diff,64,diff,null)||','||
decode(diff,66,diff,null) ||','||
decode(diff,68,diff,null) ||','||
decode(diff,70,diff,null) ||','||
decode(diff,72,diff,null) ||','||
decode(diff,74,diff,null)||','||
decode(diff,76,diff,null)||','||
decode(diff,78,diff,null) ||','||
decode(diff,80,diff,null) ||','||
decode(diff,82,diff,null) ||','||
decode(diff,84,diff,null)||','||
decode(diff,86,diff,null)||','||
decode(diff,88,diff,null)||','||
decode(diff,90,diff,null)||','||
decode(diff,92,diff,null) ||','||
decode(diff,94,diff,null) ||','||
decode(diff,96,diff,null) ||','||
decode(diff,98,diff,null)||','||
decode(diff,100,diff,null) ||','||
decode(diff,102,diff,null) ||','||
decode(diff,104,diff,null) ||','||
decode(diff,106,diff,null) ||','||
decode(diff,108,diff,null)||','||
decode(diff,110,diff,null) ||','||
decode(diff,112,diff,null)||','||
decode(diff,114,diff,null) ||','||
decode(diff,116,diff,null) ||','||
decode(diff,118,diff,null) ||','||
decode(diff,120,diff,null) ||','||
decode(diff,122,diff,null) ||','||
decode(diff,124,diff,null) ||','||
decode(diff,126,diff,null) ||','||
decode(diff,128,diff,null) ||','||
decode(diff,130,diff,null)||','||
decode(diff,132,diff,null) ||','||
decode(diff,134,diff,null) ||','||
decode(diff,136,diff,null) ||','||
decode(diff,138,diff,null) ||','||
decode(diff,140,diff,null) ||','||
decode(diff,146,diff,null) ||','||
decode(diff,148,diff,null) ||','||
decode(diff,152,diff,null) ||','||
decode(diff,154,diff,null)
from (
with t as (select rownum row_num,prime_num from prime_num order by prime_num)
select a.prime_num a_prime,b.prime_num b_prime,b.prime_num - a.prime_num diff
from t a, t b
where a.row_num + 1 = b.row_num order by 1)

Unfortunately, Oracle had a limitation in handling large numbers.


(Refer my blog "Handling Large Numbers in Oracle")
I switched to python programming for further research.
Challenges ahead Availibility of super computing resources for testing/listing large prime nu
mbers
Efficient algorythms in processing large prime numbers
Happy scripting.
References:
"SREEPADA SREEVALLABHA CHARITAAMRUTAM"
Hagiography of Sreepada Sreevallabha.
Original sanskrit script by Sriman Shankar Bhatt.
English Translation by Sri Perepa Sreeramamutrhy
Published by Sree Sreepada Sreevallabha Maha Samsthanam,
Pithapuram-533450 AP India
www.sripadasrivallabha.org
[email protected]
http://www.scribd.com/doc/190601994/Handling-Large-Numbers-in-Oracle
http://www.scribd.com/doc/45720272/NUMBERS-FOR-FUN
https://community.oracle.com/thread/2371753?start=0&tstart=0
http://www.mersenne.org/
http://primes.utm.edu/notes/proofs/Theorem2.html
http://www.proofwiki.org/wiki/Definition:Mersenne_Prime
http://www.troubleshooters.com/codecorn/primenumbers/primenumbers.htm
http://www.javadb.com/how-to-find-and-calculate-prime-numbers
http://www.morganslibrary.org/hci/hci021.html

You might also like