ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
NumberTheory.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File NumberTheory.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
226/*--------------------------------------------------------------------------*/
227/*----------------------------- DEFINITIONS --------------------------------*/
228/*--------------------------------------------------------------------------*/
229
230#ifndef __ADS_NumberTheory
231 #define __ADS_NumberTheory
232/* self-identification: #endif constant */
233
234/*--------------------------------------------------------------------------*/
235/*------------------------------ INCLUDES ----------------------------------*/
236/*--------------------------------------------------------------------------*/
237
238#include "ADS/Contract.h"
239#include "ADS/Wide.h"
240#include "ADS/set/BoolSet.h"
241
242#include <algorithm>
243#include <array>
244#include <bit>
245#include <cmath>
246#include <cstdint>
247#include <random>
248#include <stdexcept>
249#include <tuple>
250#include <type_traits>
251#include <utility>
252#include <vector>
253
254/*--------------------------------------------------------------------------*/
255/*--------------------------- NAMESPACE ------------------------------------*/
256/*--------------------------------------------------------------------------*/
257
258namespace ADS
259{
260
261using U64 = std::uint64_t;
262
277inline U64 gcd( U64 s , U64 m );
278
298[[nodiscard]] constexpr U64 binaryGcd( U64 a , U64 b );
299
317inline std::tuple< long long , long long , long long >
318extendedEuclid( long long s , long long m );
319
336inline long long modInverse( long long s , long long m );
337
339inline constexpr U64 kMersenne61 = ( U64( 1 ) << 61 ) - 1;
340
363[[nodiscard]] constexpr U64 mulMod61( U64 a , U64 b );
364
386[[nodiscard]] constexpr U64 mulMod( U64 a , U64 b , U64 m );
387
388/*--------------------------------------------------------------------------*/
389/*-------------------------- CLASS Montgomery64 ----------------------------*/
390/*--------------------------------------------------------------------------*/
395{
396 public:
413 explicit constexpr Montgomery64( U64 n );
414
416 [[nodiscard]] constexpr U64 modulus( void ) const { return( f_n ); }
417
420 [[nodiscard]] constexpr U64 to( U64 x ) const;
421
425 [[nodiscard]] constexpr U64 from( U64 x ) const { return( reduce( 0 , x ) ); }
426
428 [[nodiscard]] constexpr U64 one( void ) const { return( f_one ); }
429
443 [[nodiscard]] [[gnu::always_inline]] constexpr U64 mul( U64 a , U64 b ) const;
444
463 [[nodiscard]] constexpr U64 pow( U64 x , U64 v ) const;
464
465 private:
482 [[nodiscard]] [[gnu::always_inline]] constexpr U64 reduce( U64 hi ,
483 U64 lo ) const;
484
485 U64 f_n;
486 U64 f_inv;
487 U64 f_one;
488 U64 f_r2;
489 }; // end( class Montgomery64 )
490
510inline U64 powMod( U64 x , U64 v , U64 m );
511
526inline bool isPrimeTrial( U64 n );
527
549inline bool witness( U64 n , U64 b );
550
569template< class R >
570bool millerRabin( U64 n , int K , R & rng );
571
595inline bool isPrime( U64 n );
596
613inline std::vector< U64 > sieve( U64 n );
614
673inline std::vector< U64 > factorize( U64 n );
674
675/*--------------------------------------------------------------------------*/
676
677} // end( namespace ADS )
678
679/*--------------------------------------------------------------------------*/
680/*-------------------------- IMPLEMENTATION --------------------------------*/
681/*--------------------------------------------------------------------------*/
682
683#include "ADS/numeric/impl/NumberTheory.tpp"
684
685/*--------------------------------------------------------------------------*/
686
687#endif /* NumberTheory.h included */
688
689/*--------------------------------------------------------------------------*/
690/*------------------------ End File NumberTheory.h -------------------------*/
691/*--------------------------------------------------------------------------*/
The SET of the integers in as a boolean vector of section 8.1: contains(), insert() and remove() in ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The arithmetic of two words that C++20 does not spell, in one form for every compiler: the product of...
constexpr U64 modulus(void) const
the modulus
Definition NumberTheory.h:416
constexpr U64 one(void) const
the form of 1
Definition NumberTheory.h:428
constexpr U64 from(U64 x) const
the number whose form is , as
Definition NumberTheory.h:425
constexpr U64 to(U64 x) const
the form of , as
constexpr U64 mul(U64 a, U64 b) const
the form of the product of the numbers of the forms and
constexpr Montgomery64(U64 n)
the arithmetic modulo , with one division of 128 bits for
constexpr U64 pow(U64 x, U64 v) const
the form of , a form
constexpr U64 binaryGcd(U64 a, U64 b)
the greatest common divisor of and by the binary algorithm, with no division;
std::vector< U64 > factorize(U64 n)
the prime factors of , with their multiplicity, increasing, by the divisions by the primes below 4096...
U64 powMod(U64 x, U64 v, U64 m)
the power by repeated squaring, the products of Montgomery when is odd
bool isPrime(U64 n)
the primality of , exact for every of 64 bits
bool witness(U64 n, U64 b)
true if is a witness that the odd is composite
std::vector< U64 > sieve(U64 n)
the primes up to , increasing, by the sieve of Eratosthenes
U64 gcd(U64 s, U64 m)
the greatest common divisor by the algorithm of Euclid, steps
constexpr U64 kMersenne61
the Mersenne prime
Definition NumberTheory.h:339
std::tuple< long long, long long, long long > extendedEuclid(long long s, long long m)
the triple with (the euclideEsteso() of the book)
long long modInverse(long long s, long long m)
the inverse of , with ; a violation of the precondition if
bool millerRabin(U64 n, int K, R &rng)
the primality of by the test of Miller and Rabin on random bases, wrong with probability at most ,...
bool isPrimeTrial(U64 n)
the primality of by the divisions by (the primo() of the book)
constexpr U64 mulMod(U64 a, U64 b, U64 m)
the product , exact for any , and : by the product of 64 bits for , by mulMod61() for ,...
constexpr U64 mulMod61(U64 a, U64 b)
the product , , by four products of 32 bits and the folding
Definition ADT.h:96
std::uint64_t U64
the unsigned integers of 64 bits
Definition NumberTheory.h:261