create Prime BigInteger - Java java.math

Java examples for java.math:BigInteger

Description

create Prime BigInteger

Demo Code

/*******************************************************************************
 * $Id: $/*from  ww w .j a va2 s  .c  o m*/
 * Copyright (c) 2009-2010 Tim Tiemens.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 *
 * Contributors:
 *     Tim Tiemens - initial API and implementation
 ******************************************************************************/
//package com.java2s;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigInteger valueThatDeterminesNumberOfBits = new BigInteger("1234");
        System.out
                .println(createPrimeBigger(valueThatDeterminesNumberOfBits));
    }

    public static BigInteger createPrimeBigger(
            BigInteger valueThatDeterminesNumberOfBits) {
        Random random = new SecureRandom();
        return createPrimeBigger(valueThatDeterminesNumberOfBits, random);
    }

    public static BigInteger createPrimeBigger(
            BigInteger valueThatDeterminesNumberOfBits, Random random) {
        int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1;

        BigInteger ret = BigInteger.probablePrime(numbits, random);
        return ret;
    }
}

Related Tutorials