aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMister Wrong <misterwrong@farnsworth.fritz.box>2020-03-18 11:51:09 +0100
committerMister Wrong <misterwrong@farnsworth.fritz.box>2020-03-18 11:51:09 +0100
commite2e995dbb8fa56f40ac3d9510428cb543ebcec00 (patch)
tree09f187403c85c6798eda1929b6fca34960b89acb
parent7ed386ad3d3d37c719d36d484c36a1eef7ccee34 (diff)
downloadcalcusaurus-feat/no-primes.tar.gz
calcusaurus-feat/no-primes.tar.bz2
calcusaurus-feat/no-primes.zip
feat:no primes in multiplationsfeat/no-primes
-rw-r--r--src/main/java/net/diederickdevries/calcusaurus/service/MultiplicationService.java27
-rw-r--r--src/main/java/net/diederickdevries/calcusaurus/util/NumberUtil.java97
-rw-r--r--src/test/java/net/diederickdevries/calcusaurus/util/NumberUtilTest.java19
3 files changed, 79 insertions, 64 deletions
diff --git a/src/main/java/net/diederickdevries/calcusaurus/service/MultiplicationService.java b/src/main/java/net/diederickdevries/calcusaurus/service/MultiplicationService.java
index 9778fe1..7649272 100644
--- a/src/main/java/net/diederickdevries/calcusaurus/service/MultiplicationService.java
+++ b/src/main/java/net/diederickdevries/calcusaurus/service/MultiplicationService.java
@@ -44,17 +44,26 @@ public class MultiplicationService implements AssignmentService
44 { 44 {
45 SimpleAssignment m = new SimpleMultiplicationAssignment(); 45 SimpleAssignment m = new SimpleMultiplicationAssignment();
46 46
47 // Rule out primes, or most of the assignments will be 1 * x = x 47 // Rule out primes and ones, or most of the assignments will be 1 * x = x
48 List<Integer> factors = new ArrayList<>(); 48 List<Integer> factors;
49 while (factors.size() < 3) { 49// while (factors.size() < 3) {
50 m.setSolution(NumberUtil.getRandomInt(min, max)); 50 int i = NumberUtil.getRandomInt(min, max);
51 factors = NumberUtil.findFactors(m.getSolution()); 51 m.setSolution(i);
52 } 52 factors = NumberUtil.findFactors(i);
53 Collections.shuffle(factors); 53// }
54 54
55 m.setFirstNumber(factors.get(0)); 55 m.setFirstNumber(factors.get(NumberUtil.getRandomInt(1, factors.size() - 1)));
56 m.setSecondNumber(m.getSolution()/ m.getFirstNumber()); 56 m.setSecondNumber(m.getSolution()/ m.getFirstNumber());
57 57
58 return m; 58 return m;
59 } 59 }
60
61 private List<Integer> findMitigatedFactors(int i)
62 {
63 List<Integer> factors = NumberUtil.findFactors(i);
64 factors.removeIf(f -> f == 1);
65 factors.removeIf(f -> f == i);
66 factors.forEach(System.out::println);
67 return factors;
68 }
60} 69}
diff --git a/src/main/java/net/diederickdevries/calcusaurus/util/NumberUtil.java b/src/main/java/net/diederickdevries/calcusaurus/util/NumberUtil.java
index bb20e3e..9cd720f 100644
--- a/src/main/java/net/diederickdevries/calcusaurus/util/NumberUtil.java
+++ b/src/main/java/net/diederickdevries/calcusaurus/util/NumberUtil.java
@@ -26,45 +26,38 @@ import java.util.concurrent.ThreadLocalRandom;
26 * 26 *
27 * @author Diederick de Vries <diederick@diederickdevries.net> 27 * @author Diederick de Vries <diederick@diederickdevries.net>
28 */ 28 */
29public class NumberUtil 29public class NumberUtil {
30{ 30
31 public static int getInt(String s) 31 public static int getInt(String s) {
32 {
33 if (s.isBlank()) { 32 if (s.isBlank()) {
34 throw new IllegalArgumentException("Input cannot be blank"); 33 throw new IllegalArgumentException("Input cannot be blank");
35 } 34 }
36 return Integer.parseInt(s.strip()); 35 return Integer.parseInt(s.strip());
37 } 36 }
38 37
39 /** 38 /**
40 * Returns a non zero random integer between min and max. 39 * Returns a non zero random integer between min and max.
41 * 40 *
42 * @param min The minimum (inclusive) value of the returned number 41 * @param min The minimum (inclusive) value of the returned number
43 * @param max The maximum (inclusive) value of the returned number 42 * @param max The maximum (inclusive) value of the returned number
44 * @return 43 * @return
45 */ 44 */
46 public static int getRandomDenominator(int min, int max) 45 public static int getRandomDenominator(int min, int max) {
47 { 46 if (min == 0) {
48 if (min == 0)
49 {
50 throw new IllegalArgumentException("Denominators can not be zero"); 47 throw new IllegalArgumentException("Denominators can not be zero");
51 } 48 }
52 49
53 if (min > max) 50 if (min > max) {
54 {
55 throw new IllegalArgumentException("Min cannot be higher than max"); 51 throw new IllegalArgumentException("Min cannot be higher than max");
56 } 52 }
57 53
58 final ThreadLocalRandom random = ThreadLocalRandom.current(); 54 final ThreadLocalRandom random = ThreadLocalRandom.current();
59 55
60 int n; 56 int n;
61 57
62 if (min < 0) 58 if (min < 0) {
63 {
64 return random.nextInt(1, max + (1 - min)); 59 return random.nextInt(1, max + (1 - min));
65 } 60 } else {
66 else
67 {
68 n = random.nextInt(min, max); 61 n = random.nextInt(min, max);
69 } 62 }
70 return n; 63 return n;
@@ -72,19 +65,15 @@ public class NumberUtil
72 65
73 /** 66 /**
74 * Returns a random integer between min and max given. 67 * Returns a random integer between min and max given.
75 * 68 *
76 * @param min The minimum (inclusive) value of the returned number 69 * @param min The minimum (inclusive) value of the returned number
77 * @param max The maximum (inclusive) value of the returned number 70 * @param max The maximum (inclusive) value of the returned number
78 * @return 71 * @return
79 */ 72 */
80 public static int getRandomInt(int min, int max) 73 public static int getRandomInt(int min, int max) {
81 { 74 if (min == max) {
82 if (min == max)
83 {
84 return min; 75 return min;
85 } 76 } else if (min > max) {
86 else if (min > max)
87 {
88 throw new IllegalArgumentException("Min cannot be higher than max"); 77 throw new IllegalArgumentException("Min cannot be higher than max");
89 } 78 }
90 79
@@ -92,25 +81,18 @@ public class NumberUtil
92 81
93 int n; 82 int n;
94 83
95 if (min == 0) 84 if (min == 0) {
96 {
97 return (random.nextInt(min + 1, max + 1)) - 1; 85 return (random.nextInt(min + 1, max + 1)) - 1;
98 } 86 } else if (min < 0) {
99 else if (min < 0)
100 {
101 return (random.nextInt(1, max + (1 - min))) - (1 - min); 87 return (random.nextInt(1, max + (1 - min))) - (1 - min);
102 } 88 } else {
103 else
104 {
105 n = random.nextInt(min, max); 89 n = random.nextInt(min, max);
106 } 90 }
107 return n; 91 return n;
108 } 92 }
109 93
110 public static List<Integer> findFactors(int n) 94 public static List<Integer> findFactors(int n) {
111 { 95 if (0 == n) {
112 if (0 == n)
113 {
114 return Arrays.asList(0); 96 return Arrays.asList(0);
115 } 97 }
116 98
@@ -119,29 +101,34 @@ public class NumberUtil
119 var factors = new ArrayList<Integer>(); 101 var factors = new ArrayList<Integer>();
120 int factor = negative ? -1 : 1; 102 int factor = negative ? -1 : 1;
121 103
122 if (negative) 104 if (negative) {
123 { 105 while (factor >= n) {
124 while (factor >= n) 106 if (n % factor == 0) {
125 {
126 if (n % factor == 0)
127 {
128 factors.add(factor); 107 factors.add(factor);
129 } 108 }
130 factor--; 109 factor--;
131 } 110 }
132 } 111 } else {
133 else 112 while (factor <= n) {
134 { 113 if (n % factor == 0) {
135 while (factor <= n)
136 {
137 if (n % factor == 0)
138 {
139 factors.add(factor); 114 factors.add(factor);
140 } 115 }
141 factor++; 116 factor++;
142 } 117 }
143 } 118 }
144 119
145 return factors; 120 return factors;
146 } 121 }
122
123 public static boolean isPrime(int num) {
124 if (num <= 1) {
125 return false;
126 }
127 for (int i = 2; i <= Math.sqrt(num); i++) {
128 if (num % i == 0) {
129 return false;
130 }
131 }
132 return true;
133 }
147} 134}
diff --git a/src/test/java/net/diederickdevries/calcusaurus/util/NumberUtilTest.java b/src/test/java/net/diederickdevries/calcusaurus/util/NumberUtilTest.java
index 0b5befb..ce223c3 100644
--- a/src/test/java/net/diederickdevries/calcusaurus/util/NumberUtilTest.java
+++ b/src/test/java/net/diederickdevries/calcusaurus/util/NumberUtilTest.java
@@ -154,4 +154,23 @@ public class NumberUtilTest
154 var found = NumberUtil.findFactors(-15); 154 var found = NumberUtil.findFactors(-15);
155 assertEquals(factors, found); 155 assertEquals(factors, found);
156 } 156 }
157
158 @org.junit.jupiter.api.Test
159 public void testIsPrimeNumber()
160 {
161 var primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
162 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
163 primes.forEach(prime -> assertTrue(NumberUtil.isPrime(prime)));
164 }
165
166 @org.junit.jupiter.api.Test
167 public void testIsNotPrimeNumber()
168 {
169 var primes = Arrays.asList(1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20,
170 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40,
171 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62,
172 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82,
173 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100);
174 primes.forEach(prime -> assertFalse(NumberUtil.isPrime(prime)));
175 }
157} 176}