source: git/ntl/src/FacVec.c @ 287cc8

spielwiese
Last change on this file since 287cc8 was 287cc8, checked in by Hans Schönemann <hannes@…>, 14 years ago
ntl 5.5.2 git-svn-id: file:///usr/local/Singular/svn/trunk@12402 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 1.4 KB
Line 
1
2#include <NTL/FacVec.h>
3#include <NTL/ZZ.h>
4
5#include <NTL/new.h>
6
7NTL_START_IMPL
8
9NTL_vector_impl(IntFactor,vec_IntFactor)
10
11static
12void swap(IntFactor& x, IntFactor& y)
13{
14   IntFactor t;
15
16   t = x;  x = y;  y = t;
17}
18
19static
20void FindMin(FacVec& v, long lo, long hi)
21{
22   long minv = 0;
23   long minp = -1;
24   long i;
25
26   for (i = lo; i <= hi; i++) {
27      if (minv == 0 || v[i].val < minv) {
28         minv = v[i].val;
29         minp = i;
30      }
31   }
32
33   swap(v[lo], v[minp]);
34}
35
36
37void FactorInt(FacVec& fvec, long n)
38{
39   if (n <= 1) Error("internal error: FactorInt(FacVec,long n) with n<=1");
40
41   if (NTL_OVERFLOW(n, 1, 0))
42      Error("internal error: FactorInt(FacVec,long n) with n too large");
43
44   long NumFactors;
45   long q;
46
47   fvec.SetLength(2*NextPowerOfTwo(n));
48
49   NumFactors = 0;
50   q = 2;
51
52   while (n != 1) {
53      if (n%q == 0) {
54         fvec[NumFactors].q = q;
55         n = n/q;
56         fvec[NumFactors].a = 1;
57         fvec[NumFactors].val = q;
58         while (n%q == 0) {
59            n = n/q;
60            (fvec[NumFactors].a)++;
61            fvec[NumFactors].val *= q;
62         }
63         fvec[NumFactors].link = -1;
64         NumFactors++;
65      }
66
67      q++;
68   }
69
70   fvec.SetLength(2*NumFactors-1);
71
72   long lo = 0;
73   long hi = NumFactors - 1;
74
75   while (lo < hi) {
76      FindMin(fvec, lo, hi);
77      FindMin(fvec, lo+1, hi);
78      hi++;
79      fvec[hi].link = lo;
80      fvec[hi].val = fvec[lo].val * fvec[lo+1].val;
81      lo += 2;
82   }
83}
84
85
86
87NTL_END_IMPL
Note: See TracBrowser for help on using the repository browser.