Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package proj1;
- import java.util.*;
- public class P41 {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- System.out.println ("n =");
- int n = in.nextInt();
- System.out.println ("m =");
- int m = in.nextInt();
- {
- boolean[] isPrime = new boolean [m];
- for (int i = 2; i < m ; i++)
- isPrime[i] = true ;
- //Ciurul lui Eratostene pt a gasi numerele prime pana la m
- for ( int factor = 2 ; factor*factor < m ; factor ++)
- {
- if(isPrime[factor])
- {
- for ( int j = factor ; factor * j < m ; j++)
- {
- isPrime[factor*j] = false ;
- }
- }
- }
- //Numaram cate numere prime avem
- int primes = 0 ;
- for(int i = 2 ; i < m ; i++)
- {
- if(isPrime[i]) primes ++ ;
- }
- int[] list = new int [primes];
- int k = 0 ;
- //Adaugam numerele prime intr-o lista
- for(int i = 0 ; i < m ; i++)
- {
- if(isPrime[i]) list[k++] = i ;
- }
- //Facem sumele de numere prime pana gasim perechea care adunata
- //da i sau pana ramanem fara numere prime
- for (int i = n ; i <= m ; i++)
- {
- int l = 0 , r = k-1 ;
- while ( l < r )
- {
- if ( list[l] + list[r] == i ) break ;
- else if( list[l] + list[r] < i ) l++;
- else r--;
- }
- if( i % 2 != 0)
- {
- System.out.println(i + " is odd and therefore can't be expressed as a sum of 2 prime numbers");
- }
- else
- {
- if ( list[l] + list[r] == i)
- {
- System.out.println ( i + " = " + list[l] + " + " + list[r]);
- }
- else
- {
- System.out.println ( i + " can't be expressed as a sum of two primes" );
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement