Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.shaunren.misc;
- import java.util.*;
- public class PrimeNumberFinder {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in); // this is an object that reads stuff from console
- System.out.print("How many numbers do you want today? [1-10000]: ");
- int count = sc.nextInt(); // gets the input from the user
- if (count < 1 || count > 10000) {
- System.out.println("Invalid input.");
- return;
- }
- int[] primes = new int[count]; // this is the array that contains all the primes
- primes[0] = 2;
- int curNum = 3;
- for (int i=1; i<count; i++) {
- boolean foundPrime = false;
- while (!foundPrime) {
- boolean primeNumber = true;
- for (int j=0; j<count; j++) {
- if (primes[j] == 0) break;
- if (curNum % primes[j] == 0) {
- primeNumber = false;
- break;
- }
- }
- if (primeNumber) { // put it into the array
- primes[i] = curNum;
- foundPrime = true;
- }
- curNum += 2;
- }
- }
- // print the array
- System.out.println("Prime numbers: ");
- for (int i=0; i<count; i++) {
- System.out.println(primes[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement