gtozoom,

Some friends and I came up with this years ago when we were trying to generate large primes ourselves, but I'm sure it already exists somewhere in MathWorld or Wikipedia...

You can save a lot of space if, instead of computing (n-1)!, you compute the "minimal factorial" (which isn't the real name for it, that I know of).

The idea is that you don't need all the "junk" in (n-1)! - you only need a number that will be divisible by anything (n-1)! would have been divisible by.

What you do is, instead of computing 1*2*...*(n-1), you compute the prime factors for each number in 1..n-1, AND THEIR MULTIPLICITY. For example, 80 is 2^4*5, so its prime factors are 2 (with multiplicity 4), and 5 (multiplicity 1).

Now, for all the prime factors you collected, you only need to keep _the highest multiplicity_. Multiply the prime factors at the highest multiplicity for each, and you've got a number just as good as (n-1)!, but MUCH smaller. For example, you've eliminated about 2^(n/2) un-needed powers of 2, since half the numbers you were multiplying together were even...

Make sense??


Mike

In reply to Re: Prime Numbers by RMGir
in thread Prime Numbers by gtozoom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.