BGC Tools
Static Public Member Functions
BGC.Mathematics.NumberTheory Class Reference

Static Public Member Functions

static int LeastCommonMultiple (IEnumerable< int > numbers)
 
static IEnumerable< int > MergedFactorization (IEnumerable< int > numbers)
 
static IEnumerable< int > Factorize (int number)
 
static IEnumerable< int > PrimesUpTo (int number)
 

Detailed Description

Definition at line 8 of file NumberTheory.cs.

Member Function Documentation

◆ Factorize()

static IEnumerable<int> BGC.Mathematics.NumberTheory.Factorize ( int  number)
inlinestatic

Definition at line 82 of file NumberTheory.cs.

83  {
84  if (number < 1)
85  {
86  yield break;
87  }
88 
89  //Strip out factors of 2.
90  //Many numbers requested will have many (or only) factors of 2.
91  //This is an optimization
92  while (number % 2 == 0)
93  {
94  yield return 2;
95  number /= 2;
96  }
97 
98  foreach (int prime in PrimesUpTo((int)Math.Sqrt(number)))
99  {
100  while (number % prime == 0)
101  {
102  yield return prime;
103  number /= prime;
104  }
105 
106  if (number == 1)
107  {
108  break;
109  }
110  }
111 
112  if (number > 1)
113  {
114  yield return number;
115  }
116  }
static IEnumerable< int > PrimesUpTo(int number)

◆ LeastCommonMultiple()

static int BGC.Mathematics.NumberTheory.LeastCommonMultiple ( IEnumerable< int >  numbers)
inlinestatic

Definition at line 10 of file NumberTheory.cs.

11  {
12  if (numbers.Count() == 1)
13  {
14  return numbers.First();
15  }
16 
17  return MergedFactorization(numbers).Aggregate(1, (acc, value) => acc * value);
18  }
static IEnumerable< int > MergedFactorization(IEnumerable< int > numbers)
Definition: NumberTheory.cs:20

◆ MergedFactorization()

static IEnumerable<int> BGC.Mathematics.NumberTheory.MergedFactorization ( IEnumerable< int >  numbers)
inlinestatic

Definition at line 20 of file NumberTheory.cs.

21  {
22  if (numbers == null || numbers.Count() == 0)
23  {
24  yield break;
25  }
26 
27  if (numbers.Count() == 1)
28  {
29  foreach (int factor in Factorize(numbers.First()))
30  {
31  yield return factor;
32  }
33 
34  yield break;
35  }
36 
37  int[] nums = numbers.ToArray();
38 
39  for (int i = 0; i < nums.Length; i++)
40  {
41  //While any number is divisible by 2...
42  while (nums[i] % 2 == 0)
43  {
44  yield return 2;
45 
46  for (int j = 0; j < nums.Length; j++)
47  {
48  //Remove a factor of 2 from all numbers that retain it
49  if (nums[j] % 2 == 0)
50  {
51  nums[j] /= 2;
52  }
53  }
54  }
55  }
56 
57  int max = nums.Max();
58  foreach (int prime in PrimesUpTo((int)Math.Sqrt(max)))
59  {
60  for (int i = 0; i < nums.Length; i++)
61  {
62  //While any number is divisible by prime...
63  while (nums[i] % prime == 0)
64  {
65  yield return prime;
66 
67  for (int j = 0; j < nums.Length; j++)
68  {
69  //Remove a factor of prime from all numbers that retain it
70  if (nums[j] % prime == 0)
71  {
72  nums[j] /= prime;
73  }
74  }
75  }
76  }
77  }
78 
79  }
static IEnumerable< int > Factorize(int number)
Definition: NumberTheory.cs:82
static IEnumerable< int > PrimesUpTo(int number)

◆ PrimesUpTo()

static IEnumerable<int> BGC.Mathematics.NumberTheory.PrimesUpTo ( int  number)
inlinestatic

Definition at line 118 of file NumberTheory.cs.

119  {
120  if (number < 2)
121  {
122  yield break;
123  }
124 
125  //Include the boundary
126  number++;
127 
128  BitArray primeField = new BitArray(number, true);
129  primeField.Set(0, false);
130  primeField.Set(1, false);
131  yield return 2;
132 
134  //for (int j = 4; j < number; j += 2)
135  //{
136  // primeField.Set(j, false);
137  //}
138 
139  //Clear Others
140  for (int i = 3; i * i < number; i += 2)
141  {
142  if (primeField.Get(i))
143  {
144  //i Is Prime
145  yield return i;
146 
147  //Clear new odd factors
148  //All our primes are now odd, as are our primes Squared.
149  //This maens the numbers we need to clear start at i*i, and advance by 2*i
150  //For example j=3: 9 is the first odd composite, 15 is the next odd composite
151  // that's a factor of 3
152  for (int j = i * i; j < number; j += 2 * i)
153  {
154  primeField.Set(j, false);
155  }
156  }
157  }
158  }

The documentation for this class was generated from the following file: