that_one_nigga_you_k

problema de bachgold

Jun 7th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <limits>
  4.  
  5. using namespace std;
  6.  
  7. bool naoehprimo(int n){
  8.     bool flag = true;
  9.  
  10.     if(n <= 1) return true;
  11.     if(n == 2) return false;
  12.     if(n%2 == 0) return true;
  13.  
  14.     for(int i = 3 ; i< sqrt(n); i++){
  15.        
  16.         if(n%i == 0){
  17.             flag == false;
  18.             return true;
  19.         }
  20.  
  21.         else flag = true;
  22.  
  23.     }
  24.  
  25.     return(false);
  26. }
  27.  
  28. int main(){
  29.  
  30.  
  31.     int t;
  32.  
  33.     cin>>t;
  34.  
  35.     for(int i = 0; i < t ; i++){
  36.         int entrada;
  37.         cout<<"digite a entrada: ";
  38.         cin >> entrada;
  39.         cout<<"\n";
  40.  
  41.         if(entrada %2 == 0){
  42.             continue;
  43.         }
  44.         else{
  45.             int sol_1, sol_2;
  46.             sol_1 = -1;
  47.             sol_2 = sol_1;
  48.  
  49.             int distancia = numeric_limits<int>::max();
  50.  
  51.             for(int j = 2 ; j < entrada ; j++){
  52.                 if(naoehprimo(j) && naoehprimo(entrada - j)){
  53.                     int distancia_atual = abs(j - (entrada - j));
  54.  
  55.                     if(distancia_atual < distancia){
  56.                         distancia = distancia_atual;
  57.                         sol_1 = j;
  58.                         sol_2 = entrada - j;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             cout<<"essa é a soma de não primos mais proximos: "<<sol_1<<" "<<sol_2<<"\n";
  64.  
  65.         }
  66.  
  67.        
  68.     }
  69.  
  70.     return 0;
  71. }
Add Comment
Please, Sign In to add comment