Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4. * Goldbach’s Conjecture
  5. */
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. while (sc.hasNextInt()) {
  11. int num = sc.nextInt();
  12. if (num == 0) {
  13. break;
  14. }
  15.  
  16. for (int i = 2; i < num; i++){
  17. if (isPrime(i)){
  18. int secondPrime = num - i;
  19. if (isPrime(secondPrime)) {
  20. System.out.printf("%d = %d + %d\n", num, i, secondPrime);
  21. break;
  22. }
  23. }
  24. }
  25. }
  26. }
  27.  
  28. public static boolean isPrime(int num) {
  29. if (num < 2) return false;
  30. if (num == 2) return true;
  31. if (num % 2 == 0) return false;
  32. for (int i = 3; i * i <= num; i += 2)
  33. if (num % i == 0) return false;
  34. return true;
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement