Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.*;
- public class Primes {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- DecimalFormat df = new DecimalFormat();
- System.out.println("input an integer: ");
- double apple = scan.nextDouble();
- ArrayList<Double> primes= new ArrayList<Double>();
- double x = 2;
- double pie = apple;
- double start = System.nanoTime();
- do{
- if (isPrime(x)){
- if (pie%x == 0){
- primes.add(x);
- pie = pie / x;
- x=2;
- }
- else {
- x++;
- }
- }
- else {
- x++;
- }
- } while(x<=Math.sqrt(pie));
- primes.add(pie);
- double total = System.nanoTime() - start;
- System.out.println("The total time is:\t" + total/1000000000);
- System.out.println("the prime roots are: ");
- for (double z : primes){
- System.out.print(df.format(z)+ " ");
- }
- }
- public static boolean isPrime(double x){
- boolean bool = true;
- for (double y = 2; y < Math.sqrt(x); y++){
- if (x%y == 0){
- bool = false;
- break;
- }
- }
- return bool;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement