Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1.  
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4.  
  5. public class Primes {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         DecimalFormat df = new DecimalFormat();
  10.         System.out.println("input an integer: ");
  11.         double apple = scan.nextDouble();
  12.         ArrayList<Double> primes= new ArrayList<Double>();
  13.         double x = 2;
  14.         double pie = apple;
  15.         double start = System.nanoTime();
  16.         do{
  17.             if (isPrime(x)){
  18.                 if (pie%x == 0){
  19.                     primes.add(x);
  20.                     pie = pie / x;
  21.                     x=2;
  22.                 }
  23.                 else {
  24.                     x++;
  25.                 }
  26.             }
  27.             else {
  28.                 x++;
  29.             }
  30.         } while(x<=Math.sqrt(pie));
  31.         primes.add(pie);
  32.        
  33.         double total = System.nanoTime() - start;
  34.         System.out.println("The total time is:\t" + total/1000000000);
  35.         System.out.println("the prime roots are: ");
  36.         for (double z : primes){
  37.             System.out.print(df.format(z)+ " ");
  38.         }
  39.     }
  40.    
  41.     public static boolean isPrime(double x){
  42.         boolean bool = true;
  43.         for (double y = 2; y < Math.sqrt(x); y++){
  44.             if (x%y == 0){
  45.                 bool = false;
  46.                 break;
  47.             }
  48.         }
  49.         return bool;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement