aboutsummaryrefslogtreecommitdiff
path: root/challenge-147/abigail/java/ch-1.java
blob: 9b3b57be9e029813a7f4d728c279dd546d8b18ea (plain)
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
//
// See https://theweeklychallenge.org/blog/perl-weekly-challenge-147
//

//
// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1
//

import java.util.*;
import java.util.ArrayList;

public class ch1 {
    public static boolean is_prime (int p) {
        if (p == 2)     {return true;}
        if (p % 2 == 0) {return false;}
        for (int i = 3; i * i <= p; i += 2) {
            if (p % i == 0) {return false;}
        }
        return (true);
    }

    public static void main (String [] args) {
        ArrayList <Integer> todo = new ArrayList <Integer> ();
        todo . add (2);
        todo . add (3);
        todo . add (5);
        todo . add (7);
        for (int p: todo) {
            System . out . printf ("%d ", p);
        }
        int count = 20 - todo . size ();
        int pow   = 10;
      main:
        while (todo . size () > 0) {
            ArrayList <Integer> new_todo = new ArrayList <Integer> ();
            for (int d = 1; d <= 9; d ++) {
                for (int p: todo) {
                    int candidate = d * pow + p;
                    if (is_prime (candidate)) {
                        System . out . printf ("%d ", candidate);
                        new_todo . add (candidate);
                        count --;
                        if (count <= 0) {
                            break main;
                        }
                    }
                }
            }
            pow = pow * 10;
            todo = new_todo;
        }
        System . out . print ("\n");
    }
}