Last active
October 29, 2017 12:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generates output like this: