Advertisement
Khadija_Assem

Untitled

Dec 1st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class SievePrimeFactors {
  5. public static void main(String args[]) {
  6. Scanner sc = new Scanner(System.in);
  7. System.out.println("Enter a number");
  8. long startTime = System.currentTimeMillis();
  9. int num = sc.nextInt();
  10. boolean[] bool = new boolean[num];
  11.  
  12. for (int i = 0; i< bool.length; i++)
  13. bool[i] = true;
  14.  
  15. for (int i = 2; i< Math.sqrt(num); i++)
  16. if(bool[i] == true)
  17. for(int j = (i*i); j<num; j = j+i)
  18. bool[j] = false;
  19.  
  20. long stopTime = System.currentTimeMillis();
  21. long elapsedTime = stopTime - startTime;
  22. System.out.println("Elapsed time "+elapsedTime/1000+" Seconds");
  23.  
  24. System.out.println("List of prime numbers upto given number are : ");
  25. for (int i = 2; i< bool.length; i++)
  26. if(bool[i]==true)
  27. System.out.println(i);
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement