Skip to content

Instantly share code, notes, and snippets.

@asaushkin
Last active October 29, 2017 12:39
Show Gist options
  • Save asaushkin/d47556223eeda6f8416551350a2c3ab8 to your computer and use it in GitHub Desktop.
Save asaushkin/d47556223eeda6f8416551350a2c3ab8 to your computer and use it in GitHub Desktop.
Generates two non-prime list numbers. First one without answers for my child, the second one with answers for me.
package me.asaushkin.desing.classes.collection;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
public class GenerateNumbers {
static Queue<Integer> factorizeNumber(Integer num) {
Queue<Integer> queue = new ArrayDeque<>();
for (int i = 2; Math.pow(i, 2) < num; i++) {
if (num % i == 0) {
queue.offer(i);
num = num / i;
i = 1;
}
}
queue.offer(num);
return queue;
}
static final int MAX_PRIME = 11;
public static void main(String[] args) {
Map<Integer, Queue<Integer>> result = new HashMap<>();
while (result.size() < 10) {
Integer nextInt = ThreadLocalRandom.current().nextInt(100, 1000);
Queue<Integer> factorQueue = factorizeNumber(nextInt);
if (factorQueue.stream().noneMatch(s -> s > MAX_PRIME))
result.put(nextInt, factorQueue);
}
System.out.println(result.entrySet().stream().
peek(m -> System.out.println(m.getKey() + ": " + m.getValue())).
map(e -> e.getKey()).
collect(Collectors.toList())
);
}
}
@asaushkin
Copy link
Author

asaushkin commented Sep 23, 2017

Generates output like this:

144: [2, 2, 2, 2, 9]
224: [2, 2, 2, 2, 2, 7]
324: [2, 2, 3, 3, 9]
693: [3, 3, 7, 11]
135: [3, 3, 3, 5]
840: [2, 2, 2, 3, 5, 7]
539: [7, 7, 11]
189: [3, 3, 3, 7]
126: [2, 3, 3, 7]
495: [3, 3, 5, 11]
[144, 224, 324, 693, 135, 840, 539, 189, 126, 495]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment