guitar-player

he longest sum of consecutive primes that adds to a prime

Sep 8th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. /*******************************************************************************
  2.  * Written by I.F. Luis Mex Dzib 4/sep/2013
  3.  *
  4.  * This program Solve the following:
  5.  *
  6.  * The prime 41, can be written as the sum of six consecutive primes:
  7.         41 = 2 + 3 + 5 + 7 + 11 + 13
  8.         This is the longest sum of consecutive primes that adds to a prime below one-hundred.
  9.         The longest sum of consecutive primes below one-thousand that adds to a prime,
  10.         contains 21 terms, and is equal to 953.Which prime, below one hundred-thousand,
  11.         can be written as the sum of the most consecutive primes?
  12. ********************************************************************************
  13. * /*****************************************************************************/
  14.  
  15.  
  16. #include <iostream>
  17. #include <vector>
  18. #include <list>
  19. #include <iomanip>    
  20. #include <cstdlib>
  21. #include <cmath>
  22. #include <algorithm>
  23.  
  24.  
  25. using namespace std;
  26. struct Pair_vect {
  27.         long int sum;
  28.         vector<long int> consecutive;
  29.         void print() const {
  30.                 cout << "(" << sum << ", " ;
  31.                 cout <<"[" << consecutive.front() ;
  32.                 for (int i = 1 ; i < consecutive.size() ; i++){
  33.                         cout <<", "<< consecutive[i] ;
  34.                 }
  35.                 cout << "]"<< ")";
  36.         }
  37. };
  38.  
  39. bool is_prime(long int x){
  40.         for(int i = 2 ; i <= sqrt(x)  ; i++){
  41.                 if ( x % i == 0) {
  42.                         return false;
  43.                 }
  44.         }
  45.         return true;
  46. }
  47.  
  48. vector <long int> lista_de_primos( int x = 2){
  49.         vector<long int> lista_ini;
  50.         for (int i = 2 ; i<= x ; i++){
  51.                 if(is_prime(i)) lista_ini.push_back(i);
  52.         }
  53.         return lista_ini;
  54. }
  55.  
  56. template <class T>
  57. void prind_list(const vector<T>&v){
  58.         cout << "[";
  59.         if (!v.empty()){
  60.                 cout << v.front();
  61.                 typename vector<T> :: const_iterator it = v.begin();
  62.                 it++;
  63.                 for (; it != v.end() ; it++){
  64.                         cout <<", "<<*it ;
  65.                 }
  66.                 cout << "]" << endl;
  67.         }      
  68. }
  69.  
  70. long int suma_elemn(const vector<long int> &v, int ini = 0, int fin = 0){
  71.         long int suma = 0;
  72.         if (v.empty()) return suma;
  73.         if ( fin == 0 ){
  74.                 for (long int i = ini ; i < v.size() ; i++){
  75.                         suma = suma + v[i];
  76.                 }
  77.         }
  78.         else{
  79.                 for (long int i = ini-1 ; i < fin ; i++){
  80.                        
  81.                         suma = suma + v[i];
  82.                 }
  83.         }
  84.         return suma;  
  85. }
  86.  
  87. vector <long int > cut_vect( const vector<long int> &v , int ini = 0 , int fin = 0){
  88.         vector <long int > cortado;
  89.        
  90.         if ( fin == 0) {
  91.                 for (int i = 0 ; i < v.size(); i++){
  92.                         cortado.push_back(v[i]);
  93.                 }
  94.         }else {
  95.                 for (int i = ini ; i < fin; i++){
  96.                         cortado.push_back(v[i]);
  97.                 }
  98.         }
  99.         return cortado;
  100. }
  101.  
  102. bool compare_max(const Pair_vect &uno, const Pair_vect &dos){
  103.         return  uno.consecutive.size() > dos.consecutive.size();
  104. }
  105.  
  106. int main(){
  107.         // Input value  : below
  108.         int below = 1000;
  109.        
  110.         vector<long int> primos(lista_de_primos(below));
  111.         list< Pair_vect> long_list;
  112.        
  113.         for ( int I = 0 ; I < primos.size(); I++){
  114.                 Pair_vect piv;
  115.                
  116.                 for (int J = I + 1 ; J < primos.size() ; J++ ){
  117.                         piv.sum = suma_elemn(primos,I+1,J+1);
  118.                                                        
  119.                         if ( is_prime(piv.sum) && piv.sum < below){
  120.                                 piv.consecutive = cut_vect(primos,I,J+1);      
  121.                                 long_list.push_back(piv);
  122.                         }              
  123.                 }
  124.         }
  125.        
  126.         cout << "El vector corresponde : " << endl;
  127.         long_list.sort(compare_max);
  128.         list<Pair_vect> :: iterator it = long_list.begin();
  129.         it->print(); cout << endl << endl;;
  130.         cout << "con " << it-> consecutive.size() << " elementos " << endl;
  131.        
  132.         return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment