Advertisement
printesoi

Untitled

Oct 14th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Prob7 {
  4.     public static void main(String args[]) {
  5.         // pornesc cu valoarea default 10 pentru n
  6.         int n = 10;
  7.  
  8.         // daca avem un argument in linie de comanda, il luam pe el drept n
  9.         if (args.length == 1){
  10.             try {
  11.                 n = Integer.parseInt(args[0]);
  12.             }
  13.             catch (NumberFormatException e) {
  14.                 e.printStackTrace();
  15.             }
  16.         }
  17.  
  18.         int primes[] = new int[100];
  19.         int i,j,k = 0;
  20.  
  21.         // generez in vectorul primes toate numerele prime mai mici sau egale
  22.         // cu al n-lea numar par mai mare decat 2
  23.         for (i=1;i<=2*(n-1)+4;i++)
  24.             if (prime(i))
  25.                 primes[k++]=i;
  26.  
  27.         for (i=4,j=0;j<n;j++,i+=2){
  28.             for (int t=0;t<k && primes[t]<=i/2;t++) {
  29.                 // nu functioneaza si nu inteleg de ce :
  30.                 // if (Arrays.binarySearch(primes,i-primes[t]) >= 0)
  31.                 if (contains(primes,i-primes[t]))
  32.                     System.out.println(i + " = " + primes[t] + " + " + (i-primes[t]));
  33.             }
  34.         }
  35.  
  36.     }
  37.  
  38.     static boolean prime(int nr) {
  39.         for (int i=2;i<=Math.sqrt(nr);i++)
  40.             if (nr % i == 0)
  41.                 return false;
  42.         return true;
  43.     }
  44.  
  45.     // fuctian returneaza true daca vectorul v contine valoare val,false
  46.     // altfel
  47.     static boolean contains(int v[], int val) {
  48.         for (int i=0; i< v.length; i++)
  49.             if (v[i]==val)
  50.                 return true;
  51.         return false;
  52.     }
  53. }
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement