fucketh1cs

[prg] Prime Number Generator

Nov 6th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. /*
  2.  * Prime Numbers
  3.  * @author Maximilian Krause
  4.  * @since 11-06-2015
  5.  * @ver 1.1.1
  6.  *
  7.  * This script is capable of finding the prime numbers between two specific numbers.
  8.  *
  9.  * Changelog
  10.  * ---------
  11.  * v1.1
  12.  * @since 11-06-2015
  13.  * - Added feature to remove the last comma through a substring.
  14.  *
  15.  * v1.1.1
  16.  * @since 11-06-2015
  17.  * - Fixed too early printing of the number 2.
  18.  *
  19.  * Licensed under CC BY 4.0.
  20.  *
  21.  * TODO: Add multi-threading
  22.  */
  23.  
  24. package de.maxkrause;
  25. import java.util.*;
  26.  
  27. public class main {
  28.     public static void main(String[] args) {
  29.         Scanner scan = new Scanner(System.in);
  30.         System.out.print("Which number should be checked first? ");
  31.         int firstCheck = scan.nextInt();
  32.         System.out.print("Which number should be checked last? ");
  33.         int lastCheck = scan.nextInt();
  34.         scan.close();
  35.         System.out.println("Job starting. Please wait...");
  36.         String output = "";
  37.         if(firstCheck < 3){
  38.             firstCheck = 3;
  39.         }
  40.         for(int i = firstCheck; i <= lastCheck; i++){
  41.             boolean isPrime = true;
  42.             for(int j = i-1; j > 0; j--){
  43.                 if(j != 0 && j != 1){
  44.                     if(i % j == 0){
  45.                         isPrime = false;
  46.                     }
  47.                 }
  48.             }
  49.             if(isPrime){
  50.                 output += i + ", ";
  51.             }
  52.         }
  53.         if(!(firstCheck > 2)){
  54.             System.out.print("2, ");
  55.         }
  56.         System.out.println(output.substring(0, output.length()-2));
  57.         System.out.println("Job completed.");
  58.     }
  59. }
Add Comment
Please, Sign In to add comment