1 /* 2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 package stream.parallel; 40 41 import java.math.BigInteger; 42 import java.util.Random; 43 import java.util.stream.LongStream; 44 45 /** 46 * This demo shows how to use the parallel mode to find several number of 47 * unknown length�prime number. The process easily use new Random.LongStream 48 * generates a desired length stream. Following with filter operation. Here 49 * input length is desirable long stream length instead of number of prime 50 * number. 51 * 52 * @author tyan 53 */ 54 public class RandomPrimeNumber { 55 /** 56 * By default we only calculate at most 64 numbers once.. 57 */ 58 private final static long RANGE_SIZE = 1 << 6; 59 60 /** 61 * Try to get length from command line. Use the length to create random long 62 * stream, that integer's digit is length. 63 * 64 * @param args This has to be a 1-element or 2-elements array, The first 65 * element must be a positive number, which equals to length of generated 66 * prime number stream. Second element is optional, it will be used as how 67 * many prime number that will be generated if it's set. 68 */ 69 public static void main(String[] args) { 70 if (args.length != 1 && args.length != 2) { 71 usage(); 72 return; 73 } 74 try { 75 int length = Integer.parseInt(args[0]); 76 if (length > 16) { 77 System.out.println("Can calculate maximun only l6 digits " 78 + "prime number"); 79 return; 80 } 81 long numberOfPrime = RANGE_SIZE; 82 if (args.length == 2) { 83 numberOfPrime = Long.parseLong(args[1]); 84 } 85 long minNDigit = BigInteger.valueOf(10L).pow(length - 1).longValue(); 86 long maxNDigit = BigInteger.valueOf(10L).pow(length).longValue(); 87 88 new Random(System.currentTimeMillis()). 89 longs(numberOfPrime, minNDigit, maxNDigit). 90 filter(N -> isPrime(N)). 91 forEach(System.out::println); 92 } catch (NumberFormatException nfe) { 93 usage(); 94 } 95 } 96 97 /** 98 * Decide if a BigInteger is a prime number 99 * 100 * @param integer a number 101 * @return true if integer is a prime number false if integer is not a prime 102 * number 103 */ 104 private static boolean isPrime(long number) { 105 //This is a parall version that checks if a number is a prime number 106 return !LongStream.range(2L, Math.round(Math.sqrt(number))).parallel(). 107 anyMatch(divisor -> number % divisor == 0); 108 } 109 110 /** 111 * Usage of this program 112 */ 113 public static void usage() { 114 System.out.println("Usage: java RandomPrimeNumber lentgh[size]"); 115 System.out.println("length is a positive integer not greater than 16"); 116 System.out.println("size is a positive integer"); 117 } 118 }