forked from elenam/Sorting-Competition-Materials-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup0.java
168 lines (123 loc) · 4.77 KB
/
Group0.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
// To run on a single core, compile and then run as:
// taskset -c 0 java GroupN
// To avoid file reading/writing connections to the server, run in /tmp
// of your lab machine.
public class Group0 {
private static class Data implements Comparable<Data>{
private int value;
private int sumPrimes;
private String valStr;
public Data(String str) {
valStr = str;
value = Integer.parseInt(str);
if (value == 0 || value == 1) sumPrimes = 0;
else sumPrimes = getSumPrimeFactors(value);
}
private static int getSumPrimeFactors(int n) {
if (n == 1 || n == 0) return 0; // special cases: don't have prime factors
int sum = 0;
// checking all numbers until n, including n itself
// since n can itself be prime
for (int i = 2; i <= n; ++i) {
// if i divides n and is prime, add it to the sum
if (n % i == 0 && isPrime(i)) {
sum += i;
while (n % i == 0) {
n = n/i;
}
}
}
return sum;
}
private static boolean isPrime(int n) {
if (n == 2) return true;
if (n % 2 == 0) return false; // an even number is not prime
// going up to the largest candidate, checking only odd numbers
for (int i = 3; i < Math.sqrt(n) + 1; i += 2) {
if (n % i == 0) return false;
}
return true;
}
public static void runTests() {
System.out.println("isPime(12) = " + isPrime(12)); // not prime
System.out.println("isPime(2) = " + isPrime(2)); // prime
System.out.println("isPime(11) = " + isPrime(11)); // prime
System.out.println("isPime(999999937) = " + isPrime(999999937)); // prime
System.out.println("isPime(23785129) = " + isPrime(23785129)); // not prime
System.out.println("isPime(40464469) = " + isPrime(40464469)); // not prime
System.out.println("getSumPrimeFactors(11) = " + getSumPrimeFactors(11)); //should be 11
System.out.println("getSumPrimeFactors(20) = " + getSumPrimeFactors(20)); //should be 7
System.out.println("getSumPrimeFactors(100) = " + getSumPrimeFactors(100)); //should be 7
System.out.println("getSumPrimeFactors(999999937) = " + getSumPrimeFactors(999999937)); //should be 999999937
System.out.println("getSumPrimeFactors(23785129) = " + getSumPrimeFactors(23785129)); //should be 4877
System.out.println("getSumPrimeFactors(40464469) = " + getSumPrimeFactors(40464469)); //should be 13174
}
@Override
public int compareTo(Data other) {
if (this.sumPrimes - other.sumPrimes != 0) {
return this.sumPrimes - other.sumPrimes;
}
return other.value - this.value;
}
public String toString() {
return this.valStr;
}
}
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
if (args.length < 2) {
System.out.println(
"Please run with two command line arguments: input and output file names");
System.exit(0);
}
String inputFileName = args[0];
String outFileName = args[1];
// Uncomment to test comparator methods
//Data.runTests();
String[] data = readData(inputFileName); // read data as strings
String[] toSort = data.clone(); // clone the data
Data[] sortedThrowAway = sort(toSort); // call the sorting method once for JVM warmup
toSort = data.clone(); // clone again
Thread.sleep(10); // to let other things finish before timing; adds stability of runs
long start = System.currentTimeMillis();
Data[] sorted = sort(toSort); // sort again
long end = System.currentTimeMillis();
System.out.println(end - start);
writeOutResult(sorted, outFileName); // write out the results
}
private static String[] readData(String inputFileName) throws FileNotFoundException {
ArrayList<String> input = new ArrayList<>();
Scanner in = new Scanner(new File(inputFileName));
while (in.hasNext()) {
input.add(in.next());
}
in.close();
// the string array is passed just so that the correct type can be created
return input.toArray(new String[0]);
}
// YOUR SORTING METHOD GOES HERE.
// You may call other methods and use other classes.
// Note: you may change the return type of the method.
// You would need to provide your own function that prints your sorted array to
// a file in the exact same format that my program outputs
private static Data[] sort(String[] toSort) {
Data [] data = new Data[toSort.length];
for (int i = 0; i < data.length; ++i) {
data[i] = new Data(toSort[i]);
}
Arrays.sort(data);
return data;
}
private static void writeOutResult(Data[] sorted, String outputFilename) throws FileNotFoundException {
PrintWriter out = new PrintWriter(outputFilename);
for (Data s : sorted) {
out.println(s);
}
out.close();
}
}