aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/deadmarshal/java/Ch2.java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-241/deadmarshal/java/Ch2.java')
-rw-r--r--challenge-241/deadmarshal/java/Ch2.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-241/deadmarshal/java/Ch2.java b/challenge-241/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..e810fe1429
--- /dev/null
+++ b/challenge-241/deadmarshal/java/Ch2.java
@@ -0,0 +1,35 @@
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Comparator;
+
+public class Ch2 {
+ public static void main(String[] args) {
+ ArrayList<Integer> list = new ArrayList<>(List.of(11,8,27,4));
+ prime_order(list);
+ System.out.println(list);
+ }
+
+ private static int count_factors(int n) {
+ int c = 2,count = 0;
+ while(n > 1) {
+ if(n % c == 0) {
+ n /= c;
+ count++;
+ }
+ else c++;
+ }
+ return count;
+ }
+
+ private static void prime_order(List<Integer> list) {
+ list.sort(new Comparator<Integer>() {
+ @Override
+ public int compare(Integer a, Integer b) {
+ int fa = count_factors(a);
+ int fb = count_factors(b);
+ return fa == fb ? Integer.compare(a,b) :
+ Integer.compare(fa,fb);
+ }});
+ }
+}
+