Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing large prime numbers in a database

This problem struck me as a bit odd. I'm curious how you could represent a list of prime numbers in a database. I do not know of a single datatype that would be able to acuratly and consistently store a large amount of prime numbers. My concern is that when the prime numbers are starting to contain 1000s of digits, that it might be a bit difficult to reference form the database. Is there a way to represent a large set of primes in a DB? I'm quite sure that this has topic has been approached before.

One of the issues about this that makes it difficult is that prime numbers can not be broken down into factors. If they could this problem would be much easier.

like image 263
monksy Avatar asked Dec 15 '09 13:12

monksy


People also ask

Is there a database of prime numbers?

Largest Known Primes DatabaseOur central database acts as a “Guinness book” of prime number records! This list includes the 5000 largest known primes and smaller ones of selected forms updated hourly.

Why is it hard to find large prime numbers?

As numbers get larger, prime numbers - a number divisible only by 1 and itself - become very difficult to find. They become farther and farther apart, and there's no pattern to their distribution, so it's not as simple as using an algorithm.

Can you sell large prime numbers?

Prime numbers are useful for writing codes and in America they are classed as Military Material and if you find one over 100 digits long you have to tell the CIA and they buy it off you for $10,000. But it would not be a very good way of making a living.


2 Answers

If you really want to store primes as numbers and one of questions, stopping you is "prime numbers can not be broken down into factors", there are another thing: store it in list of modulus of any number ordered by sequence.

Small example:

2831781 == 2*100^3 + 83*100^2 + 17*100^1 + 81*100^0

List is:

81, 17, 83, 2

In real application is useful to split by modulus of 2^32 (32-bits integers), specially if prime numbers in processing application stored as byte arrays.

Storage in DB:

create table PRIMES
(
  PRIME_ID         NUMBER not null,
  PART_ORDER       NUMBER(20) not null,
  PRIME_PART_VALUE NUMBER not null
);

alter table PRIMES 
add constraint PRIMES_PK primary key (PRIME_ID, PART_ORDER) using index;

insert for example above (1647 is for example only):

insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 0, 81);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 1, 17);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 2, 83);
insert into primes(PRIME_ID, PART_ORDER, PRIME_PART_VALUE) values (1647, 3, 82);

prime_id value can be assigned from oracle sequence ...

create sequence seq_primes start with 1 increment by 1;

Get ID of next prime number to insert:

select seq_primes.nextval from dual;

select prime number content with specified id:

select PART_ORDER, PRIME_PART_VALUE 
from primes where prime_id = 1647 
order by part_order
like image 106
ThinkJet Avatar answered Oct 22 '22 14:10

ThinkJet


You could store them as binary data. They won't be human readable straight from the database, but that shouldn't be a problem.

like image 38
nickf Avatar answered Oct 22 '22 14:10

nickf