Java:查找小于n的素数列表(其中n是BigNum)

时间:2015-09-15 04:09:55

标签: java primes

此代码只打印出2,我无法弄清楚为什么它不起作用。我认为它与我的链接列表实现有关。刚开始我只想找到小于100的素数。

素数(或素数)是大于1的自然数,除了1和自身之外没有正除数。大于1的自然数不是素数,称为复合数。

import java.math.*;
import static java.math.BigInteger.*;
class Node {
    BigInteger data;
    Node small;
    Node large;
    public Node(BigInteger data) {
        this.data = data;
        small = null;
        large = null;
    }
}
class TreeList {
    public static void join(Node a, Node b) {
        a.large = b;
        b.small = a;
    }
    public static Node append(Node a, Node b) {
        if (a == null) return(b);
        if (b == null) return(a);
        Node aLast = a.small;
        Node bLast = b.small;
        join(aLast, b);
        join(bLast, a);   
        return(a);
    }
    public static Node treeToList(Node root) {
        if (root == null) return (null);
        Node aList = treeToList(root.small);
        Node bList = treeToList(root.large);
        root.small = root;
        root.large = root;
        aList = append(aList, root);
        aList = append(aList, bList);   
        return(aList);
    }
    public static void treeInsert(Node root, BigInteger newData) {
        if (newData.compareTo(root.data) == 0) {
            if (root.small != null) treeInsert(root.small, newData);
            else root.small = new Node(newData);
        }
        else {
            if (root.large != null) treeInsert(root.large, newData);
            else root.large = new Node(newData);
        }
    }
    public static void printTree(Node root) {
        if (root == null) return;
        printTree(root.small);
        System.out.print(root.data.toString());
        printTree(root.large);
    }
    public static void printList(Node head) {
        Node current = head;
        while (current != null) {
            System.out.print(current.data.toString() + " ");
            current = current.large;
            if (current == head) break;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        BigInteger count = BigInteger.valueOf(1);
        BigInteger myPrime = BigInteger.valueOf(2);
        BigInteger two = BigInteger.valueOf(2);
        BigInteger three = BigInteger.valueOf(3);
        Node root = new Node(myPrime);
        Node head = treeToList(root);

        boolean isPrime;

        BigInteger n = new BigInteger("100");

        for (BigInteger i = three; count.compareTo(n) == 0; i = i.add(two)) {
            isPrime = true;
            for (BigInteger j = three; (j.multiply(j)).compareTo(i) == 0 && isPrime; j = j.add(two))
                if (i.mod(j) == BigInteger.ZERO) isPrime = false;
            if (isPrime) {
                count = count.add(BigInteger.ONE);   
                myPrime = i;
                if (myPrime.compareTo(n) == 0) treeInsert(root, myPrime);
            }
        }
        printList(head);
    }
}

1 个答案:

答案 0 :(得分:2)

你的循环根本没有运行,因为它在false上返回count.compareTo(n) == 0

For循环,仅在第二个语句返回true时运行。

你的第二个for循环也是错误的。