-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizer.java
More file actions
66 lines (53 loc) · 1.35 KB
/
Randomizer.java
File metadata and controls
66 lines (53 loc) · 1.35 KB
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
package com.sai;
import java.util.*;
public class Randomizer {
// Enqueue All random values within generated maximum range
public static Queue<Integer> GetRandomValues() {
Random r = new Random();
int maxValue = r.nextInt(20) + 1;
// Generates random range of numbers to be enqueued
Queue<Integer> q = new LinkedList<Integer>();
for (int i = 0; i < maxValue; i++) {
q.add(r.nextInt(19) + 2);
}
// below print statement can be removed
return q;
}
public static void main(String[] args) {
Prime p = new Prime();
Queue<QueuePair> q = p.GetRandomPrimeValues();
for (QueuePair qp : q)
System.out.println(qp.data + " ," + qp.isPrime);
}
}
class QueuePair {
int data;
boolean isPrime;
QueuePair(int data, boolean isPrime) {
this.data = data;
this.isPrime = isPrime;
}
}
class Prime {
// Method to return Queue(random number, isPrime) of generated random
// numbers and check if its prime or not
public Queue<QueuePair> GetRandomPrimeValues() {
Queue<Integer> q = Randomizer.GetRandomValues();
Queue<QueuePair> qp = new LinkedList<QueuePair>();
for (int i : q) {
int notPrime = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
notPrime = 1;
break;
}
}
if (notPrime == 0 || i == 2) {
qp.add(new QueuePair(i, true));
} else {
qp.add(new QueuePair(i, false));
}
}
return qp;
}
}