File: numbertheory.cpp

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (198 lines) | stat: -rw-r--r-- 6,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2008, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public             *
 *  License along with this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

#include <algorithm>
#include <functional>
#include "maths/numbertheory.h"
#include "utilities/stlutils.h"

namespace regina {

long reducedMod(long k, long modBase) {
    long ans = k % modBase;
    if (ans < 0) {
        if ((ans + modBase) <= (-ans))
            return (ans + modBase);
    } else if (-(ans - modBase) < ans)
        return (ans - modBase);
    return ans;
}

unsigned long gcd(unsigned long a, unsigned long b) {
    long tmp;
    while (a != b && b != 0) {
        tmp = a;
        a = b;
        b = tmp % b;
    }
    return a;
}

namespace {
    long gcdWithCoeffsInternal(long a, long b, long& u, long& v) {
        // This routine assumes a and b to be non-negative.
        long a_orig = a;
        long b_orig = b;
        u = 1;
        v = 0;
        long uu = 0;
        long vv = 1;
        long tmp1, tmp2, q;
        while (a != b && b != 0) {
            // At each stage:
            // u*(a_orig) + v*(b_orig) = a_curr;
            // uu*(a_orig) + vv*(b_orig) = b_curr.
            tmp1 = u; tmp2 = v;
            u = uu; v = vv;
            q = a / b;
            uu = tmp1 - (q * uu); vv = tmp2 - (q * vv);

            tmp1 = a;
            a = b;
            b = tmp1 % b;
        }

        // a is now our gcd.

        // Put u and v in the correct range.
        if (b_orig == 0)
            return a;

        // We are allowed to add (b_orig/d, -a_orig/d) to (u,v).
        a_orig = -(a_orig / a);
        b_orig = b_orig / a;

        // Now we are allowed to add (b_orig, a_orig), where b_orig >= 0.
        // Add enough copies to put u between 1 and b_orig inclusive.
        long k;
        if (u > 0)
            k = -((u-1) / b_orig);
        else
            k = (b_orig-u) / b_orig;
        if (k) {
            u += k * b_orig;
            v += k * a_orig;
        }

        return a;
    }
}

long gcdWithCoeffs(long a, long b, long& u, long& v) {
    long signA = (a > 0 ? 1 : a == 0 ? 0 : -1);
    long signB = (b > 0 ? 1 : b == 0 ? 0 : -1);
    long ans = gcdWithCoeffsInternal(a >= 0 ? a : -a,
        b >= 0 ? b : -b, u, v);
    u *= signA;
    v *= signB;
    return ans;
}

unsigned long modularInverse(unsigned long n, unsigned long k) {
    if (n == 1)
        return 0;

    long u, v;
    gcdWithCoeffs(n, k % n, u, v);
    // GCD should equal 1, so u*n + k*v = 1.
    // Inverse is v; note that -n < v <= 0.
    // Since n >= 2 now and (n,k) = 1, we know v != 0.
    return v + n;
}

namespace {
    /**
     * Finds the smallest prime factor of the given odd integer.
     * You may specify a known lower bound for this smallest prime factor.
     * If the given integer is prime, 0 is returned.
     *
     * \pre \a n is odd.
     * \pre The smallest prime factor of \a n is known to
     * be at least as large as (and possibly equal to) \a lowerBound.
     * \pre \a lowerBound is odd.
     * 
     * @param n the integer whose smallest prime factor we wish to find.
     * @param lowerBound a known lower bound for this smallest prime factor.
     * @return the smallest prime factor of \a n, or 0 if \a n is prime.
     */
    unsigned long smallestPrimeFactor(unsigned long n,
            unsigned long lowerBound = 1) {
        while (lowerBound * lowerBound <= n) {
            if (n % lowerBound == 0)
                return lowerBound;
            lowerBound += 2;
        }
        return 0;
    }
}

void factorise(unsigned long n, std::list<unsigned long>& factors) {
    // n > 0 is a precondition, but the effects are too unpleasant if
    // it's broken (infinite memory consumption).
    if (n == 0)
        return;

    // First take out all factors of 2.
    while (n % 2 == 0) {
        n = n / 2;
        factors.push_back(2);
    }

    // Run through finding smallest factors.
    unsigned long factor = 3;
    while ((factor = smallestPrimeFactor(n, factor))) {
        factors.push_back(factor);
        n = n / factor;
    }

    // Anything left is prime.
    if (n > 1)
        factors.push_back(n);
}

void primesUpTo(const NLargeInteger& roof, std::list<NLargeInteger>& primes) {
    // First check 2.
    if (roof < 2)
        return;
    primes.push_back(NLargeInteger(2));

    // Run through the rest.
    NLargeInteger current(3);
    while (current <= roof) {
        // Is current prime?
        if (find_if(primes.begin(), primes.end(), regina::stl::compose1(
                bind2nd(std::equal_to<NLargeInteger>(), NLargeInteger::zero),
                bind1st(std::modulus<NLargeInteger>(), current))) ==
                primes.end())
            primes.push_back(current);
        current += 2;
    }
}

} // namespace regina