Re: storing factors
Date: Sat, 21 Jul 2001 23:27:20 GMT
Message-ID: <9f405f$2aa$1_at_bob.news.rcn.net>
this is yet another master-detail model (what ARE they teaching kids in college these days???)
create table IntProduct (product number primary key);
create table PrimeFactors
(product number
constraint fk_intproduct foreign key references IntProduct (product)
,prime number
, constraint pk_factors primary key (product, prime)
)
it was unclear whether or not only prime factors were required and whether or not an indication of exponents is required, ie, f(27) = (3,3,3) or f(27) = 3^3 or f(27) = (3). if you want all factors, prime or otherwise, just rename PrimeFactors to Factors. if you want to distinguish non-prime factors from prime factors, just add a set-membership column and make the set-id part of a key and add an is-prime column to the Factors table to indicate which sets are factors of a given product and which set members are prime or non-prime. if exponentiation is desired, add an exponent column. you may need some logic to ensure the result set for a query provides the desired results if using non-prime factors.
keep in mind it is very likely that the number of prime numbers is infinite, which suggests one avoids creating a table of prime numbers, a table of products, and then a junction table to resolve the m-to-m mapping. however, if you know - absolutely - the largest factor you will ever store, one could use this approach. in doing so, assuming values in the product table are unique and not null, the product itself can serve as the primary key (unique and not null) in the product table and likewise for prime numbers in the PrimeFactor table. in this case, and exponentiation column would be required.
possible scenarios:
- all factors (prime and non-prime) with exponents:
f(20) = (2,10) = (4,5) = (2,5)
==> Product(20) Factor(20, 1, 2, 1, 'P') Factor(20, 1, 10, 1, 'P') Factor(20, 2, 4, 1, 'N') Factor(20, 2, 5, 1, 'P') Factor(20, 3, 2, 2, 'P') Factor(20, 3, 5, 1, 'P')
select factor, exponent
from factor f
where product = &prod
group by setID
2. only prime factors with exponentiation
f(20) = (2^2, 5)
==> Product(20) Factor(20, 2, 2)
Factor(20, 5, 1)
select factor, exponent
from factor
where product = ∏
3. just prime factors, no exponents
f(20) = (2,5)
==> Product(20) Factor(20, 2)
Factor(20, 5)
select prime from factor where product = ∏
Enjoy!
"Aloha Kakuikanu" <nospam_at_newsranger.com> wrote in message
news:c8_O6.5780$r4.363925_at_www.newsranger.com...
> In our AMS database we need to store a sets of prime factors for an
integer. For
> example: factors(10)={2,5}. I'm not sure how to design the table that can
hadle
> that, since the number of factors is not constant. Any solutions? > >Received on Sun Jul 22 2001 - 01:27:20 CEST