Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Algorithm for cancelling common factors between two lists of multiplicands

by QM (Parson)
on Aug 10, 2005 at 17:51 UTC ( [id://482678]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Algorithm for cancelling common factors between two lists of multiplicands
in thread Algorithm for cancelling common factors between two lists of multiplicands

Coming up with an efficient, pure perl implementation of prime factorisation (limiting myself to integers 0 .. 2**32 ) is my current fun challenge :)
You might have a look at Math::Big::Factors, which I found useful, and is pure Perl. (Note that the docs have a typo, and mention factor_wheel where the function is actually named factors_wheel.)

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^4: Algorithm for cancelling common factors between two lists of multiplicands
by BrowserUk (Patriarch) on Aug 10, 2005 at 18:48 UTC

    Without actually trying it, Math::Big::Factors says:

    For very small numbers the computation of the wheel of order 7 may actually take longer than the factorization, but anything that has more than 10 digits will usually benefit from order 7.

    As I'm limiting myself to 2**32, it probably won't help?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Here's a benchmark for Math::Big::Factors wheels of orders 5-7:
      Rate 7 6 5 7 34.1/s -- -95% -100% 6 627/s 1739% -- -94% 5 9770/s 28548% 1458% --
      and for factors_wheel of numbers near 32 bits, using an order 7 wheel:
      s/iter 4294967291 4294967293 4294967295 4294967291 4.45 -- -61% -71% 4294967293 1.74 156% -- -25% 4294967295 1.31 241% 33% --
      Note that 4294967291 is prime, 4294967293 has 2 large factors, and 4294967295 has 5 factors, the largest of which is 2**16+1.

      For 4294967291, and wheel orders 5-7:

      s/iter 7 5 6 7 4.44 -- -16% -21% 5 3.71 19% -- -6% 6 3.49 27% 6% --
      In all fairness, I should probably mention that I've customized my own version of Math::Big::Factors to Memoize results where possible, and to reduce the calls to Math::BigInt::new() for constants.

      Note that factors_wheel creates a new wheel every time (what a shame), instead of requiring a wheel reference be passed in. Caching wheel goes a little way in correcting this, without changing the interface.

      You might also consider avoiding the use of Math::BigInt where possible, as you don't need numbers that big.

      Now, back to the question at hand. For numbers near 2**32, factoring a prime number seems to take 150 times longer than creating the order 7 wheel. Creating the order 6 wheel is considerably faster, and so is the factoring based on that wheel.

      I'm not sure where the breakpoint is for order 7 wheels. A casual search hints that it is very large.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        For numbers near 2**32, factoring a prime number seems to take 150 times longer

        I'm not sure that I understand this? Factoring a prime how?

        (I should also admit that I don't know what a "n-order wheel" is, or why it would benefit me to use one :)

        Also, as I'm limiting my inputs to 0 .. 2**32, I only need the primes upto 65537 in order to factor that range of inputs. Right now, I am trading a little space (130k) to gain speed by building an array of the first 6534 primes at compile time.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://482678]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found