Advertisement
s_m4rt

Untitled

Nov 7th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package liczbypierwsze;
  2.  
  3. import javax.swing.JOptionPane;
  4. import java.math.*;
  5. import java.util.*;
  6.  
  7. public class Main {
  8. static int liczba;
  9. static List<Integer> list = new ArrayList<Integer>();
  10.  
  11. public static void main(String[] args) {
  12. //int liczba;
  13. liczba=Integer.parseInt(JOptionPane.showInputDialog("Podaj liczbe dodatnia calkowita:"));
  14. while(liczba<=0){
  15. if(liczba<=0) {JOptionPane.showMessageDialog(null, "Wybrales niepoprawna opcje. Sprobuj ponownie.");
  16. liczba=Integer.parseInt(JOptionPane.showInputDialog("Niepoprawne dane. Podaj liczbe dodatnia calkowita:"));}}
  17. pierwsza();
  18. wypisywanie();
  19.  
  20. }
  21.  
  22. public static boolean pierwsza() {
  23. boolean success = true;
  24.  
  25. //List<Integer> list = new ArrayList<Integer>();
  26. if (liczba%2==0){
  27.  
  28. for(int i=3; i<Math.sqrt(liczba); i=i+2){
  29. if (liczba%i==0) {
  30. success = false;
  31. break;}
  32. }
  33. }
  34.  
  35.  
  36. return success;
  37. }
  38.  
  39. public static void wypisywanie(){
  40. for (int i=1;i<=liczba/2;i++){
  41. if(liczba%i==0){
  42. list.add(i);
  43. }
  44. }
  45. list.add(liczba);
  46. int size = list.size();
  47. if (size<=2){
  48. JOptionPane.showMessageDialog(null, "To jest liczba pierwsza.");
  49. JOptionPane.showMessageDialog(null, list);
  50. }
  51. else{
  52. JOptionPane.showMessageDialog(null, "To nie jest liczba pierwsza.");
  53. JOptionPane.showMessageDialog(null, list);
  54. }
  55. //JOptionPane.showMessageDialog(null, size);
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement