-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivisorInc.java
More file actions
executable file
·211 lines (201 loc) · 7.17 KB
/
Copy pathDivisorInc.java
File metadata and controls
executable file
·211 lines (201 loc) · 7.17 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.io.*;
import java.math.*;
import java.util.*;
class DivisorInc
{
public static ArrayList<Integer> primeNumbers(int endingNumber)
{
ArrayList<Integer> primeList = new ArrayList<Integer>();
boolean[] primeChecker = new boolean[endingNumber + 1];
Arrays.fill(primeChecker, true);
int sqrtValue = (int)(Math.sqrt(endingNumber));
primeList.add(2);
for(int i = 3; i <= sqrtValue; i += 2)
{
for(int j = i; j <= endingNumber; j += 2)
{
int value = i * j;
if(value <= endingNumber)
{
primeChecker[value] = false;
}
else
{
break;
}
}
}
for(int i = 3; i <= endingNumber; i += 2)
{
if(primeChecker[i])
{
primeList.add(i);
}
}
return primeList;
}
public static ArrayList<Integer> possiblePrimeFactors(int endingNumber)
{
ArrayList<Integer> primeList = new ArrayList<Integer>();
boolean[] primeChecker = new boolean[(endingNumber/2) + 1]; //Because primeNumber greater than half the value of a nummber cant be the divisor of the number
Arrays.fill(primeChecker, true);
//One factor always lies below the sqrt value an therefore the remaining factor can be found from the factors which lies below sqrtValue, in that, the left out numbers are primes.
int sqrtValue = (int)(Math.sqrt(endingNumber/2));
primeList.add(2);
for(int i = 3; i <= sqrtValue; i += 2)
{
for(int j = i; j <= (endingNumber/2); j += 2)
{
int value = i * j;
if(value <= (endingNumber / 2))
{
primeChecker[value] = false;
}
else
{
break;
}
}
}
for(int i = 3; i <= (endingNumber / 2); i += 2)
{
if(primeChecker[i])
{
primeList.add(i);
}
}
return primeList;
}
public static int[] getThePrimeArray(ArrayList<Integer> primeList)
{
int[] primeArray = new int[primeList.size()];
Iterator primeListIterator = primeList.iterator();
int count = 0;
while(primeListIterator.hasNext())
{
primeArray[count] = (int)primeListIterator.next();
// System.out.println(primeArray[count]);
count += 1;
}
return primeArray;
}
public static int[] getThePrimeDivisorsArray(ArrayList<Integer> primeList)
{
int[] primeArray = new int[primeList.size()];
Iterator primeListIterator = primeList.iterator();
int count = 0;
while(primeListIterator.hasNext())
{
primeArray[count] = (int)primeListIterator.next();
// System.out.println(primeArray[count]);
count += 1;
}
return primeArray;
}
public static ArrayList<Integer> getThePrimeDivisors(int[] primeArray, int endingNumber)
{
ArrayList<Integer> primeDivisorsList = new ArrayList<Integer>();
int arrayLenth = primeArray.length;
for(int i = 0; i < arrayLenth; i++)
{
if(endingNumber % primeArray[i] == 0)
{
primeDivisorsList.add(primeArray[i]);
}
}
return primeDivisorsList;
}
public static int[] getTheCountOfPrimeFactorsExponent(int[] primeDivisorsArray, int endingNumber)
{
int lengthOfPrimeDivisorsArray = primeDivisorsArray.length;
int[] exponentValueOfPrimeDivisors = new int[primeDivisorsArray.length];
for(int i = 0; i < lengthOfPrimeDivisorsArray; i++)
{
int exponentValue = 0;
while(endingNumber % primeDivisorsArray[i] == 0)
{
exponentValue = exponentValue + 1;
endingNumber = endingNumber / primeDivisorsArray[i];
}
exponentValueOfPrimeDivisors[i] = exponentValue;
}
return exponentValueOfPrimeDivisors;
}
public static void printTheExponents(int[] getThePrimeExponents)
{
for(int i = 0; i < getThePrimeExponents.length; i++)
{
System.out.println(getThePrimeExponents[i]);
}
}
public static void getAllDivisors(int[] primeDivisors, int[] primeExponents, int currentIndex, int currentValue, int primeDivisorsLength, ArrayList<Integer> allDivisors)
{
int value = primeDivisors[currentIndex];
for(int i = 0; i <= primeExponents[currentIndex]; i++)
{
if(i == 0)
{
if (currentIndex == 0)
{
currentValue = 1;
}
if(currentIndex == (primeDivisorsLength - 1))
{
allDivisors.add(currentValue);
}
else
{
getAllDivisors(primeDivisors, primeExponents, currentIndex + 1, currentValue, primeDivisorsLength, allDivisors);
}
}
else
{
if (currentIndex == 0)
{
currentValue = 0;
currentValue = currentValue + value;
}
else
{
currentValue = currentValue * value;
}
if(currentIndex == (primeDivisorsLength - 1))
{
allDivisors.add(currentValue);
}
else
{
getAllDivisors(primeDivisors, primeExponents, currentIndex + 1, currentValue, primeDivisorsLength, allDivisors);
}
value *= primeDivisors[currentIndex];
}
}
}
public static void getAllDivisorsArray(ArrayList<Integer> allDivisors)
{
Iterator allDivisorsIterator = allDivisors.iterator();
while(allDivisorsIterator.hasNext())
{
int divisorValue = (int)allDivisorsIterator.next();
System.out.println(divisorValue);
}
}
public static void main(String[] args)
{
int startingNumber;
int endingNumber;
Scanner in = new Scanner(System.in);
startingNumber = in.nextInt();
endingNumber = in.nextInt();
ArrayList<Integer> primeList = possiblePrimeFactors(endingNumber);
int[] primeArray = getThePrimeArray(primeList);
ArrayList<Integer> primeDivisors = getThePrimeDivisors(primeArray, endingNumber);
int[] primeDivisorsArray = getThePrimeDivisorsArray(primeDivisors);
int[] getThePrimeExponents = getTheCountOfPrimeFactorsExponent(primeDivisorsArray, endingNumber);
// printTheExponents(getThePrimeExponents);
ArrayList<Integer> allDivisors = new ArrayList<Integer>();
int primeDivisorsLength = primeDivisorsArray.length;
getAllDivisors(primeDivisorsArray, getThePrimeExponents, 0, 1, primeDivisorsLength, allDivisors);
getAllDivisorsArray(allDivisors);
}
}