The thinking of the fast judgement of the full square number of large integers

Source: Internet
Author: User
Tags square root
16.5.3 Perfect Square

A significant fraction of non-squares can be quickly identified by checking whether the input is a quadratic residue modul o small integers.

Mpz_perfect_square_p first tests the input mod, which means just examining the low byte. Only the different values occur for squares mod, so 82.8% of inputs can be immediately identified as Non-squares.

On a 32-bit system similar tests is done mod 9, 5, 7, + and, for a total 99.25% of inputs identified as non-squares. On a 64-bit system "is tested too, for a total 99.62%.

These moduli is chosen because they ' re factors of 2^24-1 (or 2^48-1 for 64-bits), and such a remainder can be quickly tak En just using additions (see MPN_MOD_34LSUB1).

When nails is in use Moduli is instead selected by the GEN-PSQR.C program and applied with an mpn_mod_1. The same 2^24-1 or 2^48-1 could be do with nails using some extra bit shifts, which is not currently implemented.

In all case each modulus are applied to the MPN_MOD_34LSUB1 or mpn_mod_1 remainder and a table lookup identifies Non-square S. By using a "modexact" style calculation, and suitably permuted tables, just one multiply are required, see the code For details. Moduli is also combined to save operations, so long as the lookup tables don ' t become too big. GEN-PSQR.C does all the pre-calculations.

A square root must still is taken for any value that passes these tests, to verify it's really a square and not one of the Small fraction of non-squares that get through (i.e. a pseudo-square to all the tested bases).

Clearly more residue tests could is done, mpz_perfect_square_p only uses a compact and efficient set. Big inputs would probably benefit from more residue testing, small inputs might is better off with less. The assumed distribution of squares versus non-squares in the input would affect such considerations. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.