guitar-player

This is the longest sum of consecutive primes

Sep 8th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 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. #include <iostream>
  16. #include <vector>
  17. #include <list>
  18. #include <iomanip> 
  19. #include <cstdlib>
  20. #include <cmath>
  21. #include <algorithm>
  22.  
  23.  
  24. using namespace std;
  25. struct Pair_vect {
  26.     long int sum;
  27.     vector<long int> consecutive;
  28.     void print() const {
  29.         cout << "(" << sum << ", " ;
  30.         cout <<"[" << consecutive.front() ;
  31.         for (int i = 1 ; i < consecutive.size() ; i++){
  32.             cout <<", "<< consecutive[i] ;
  33.         }
  34.         cout << "]"<< ")";
  35.     }
  36. };
  37.  
  38. bool is_prime(long int x){
  39.     for(int i = 2 ; i <= sqrt(x)  ; i++){
  40.         if ( x % i == 0) {
  41.             return false;
  42.         }
  43.     }
  44.     return true;
  45. }
  46.  
  47. vector <long int> lista_de_primos( int x = 2){
  48.     vector<long int> lista_ini;
  49.     if ( x <= 1000){
  50.         for (int i = 2 ; i<= (x)/7 ; i++){
  51.             if(is_prime(i)) lista_ini.push_back(i);
  52.         }
  53.     }
  54.     if ( x <= 150000 && x > 1000){
  55.         for (int i = 2 ; i<= (x)/7 ; i++){
  56.             if(is_prime(i)) lista_ini.push_back(i);
  57.         }
  58.     }
  59.     if ( x > 150000){
  60.         for (int i = 2 ; i<= (x)/100 ; i++){
  61.             if(is_prime(i)) lista_ini.push_back(i);
  62.         }
  63.     }
  64.  
  65.     return lista_ini;
  66. }
  67.  
  68. template <class T>
  69. void prind_list(const vector<T>&v){
  70.     cout << "[";
  71.     if (!v.empty()){
  72.         cout << v.front();
  73.         typename vector<T> :: const_iterator it = v.begin();
  74.         it++;
  75.         for (; it != v.end() ; it++){
  76.             cout <<", "<<*it ;
  77.         }
  78.         cout << "]" << endl;
  79.     }  
  80. }
  81.  
  82. long int suma_elemn(const vector<long int> &v, int ini = 0, int fin = 0){
  83.     long int suma = 0;
  84.     if (v.empty()) return suma;
  85.     if ( fin == 0 ){
  86.         for (long int i = ini ; i < v.size() ; i++){
  87.             suma = suma + v[i];
  88.         }
  89.     }
  90.     else{
  91.         for (long int i = ini-1 ; i < fin ; i++){
  92.            
  93.             suma = suma + v[i];
  94.         }
  95.     }
  96.     return suma;   
  97. }
  98.  
  99. vector <long int > cut_vect( const vector<long int> &v , int ini = 0 , int fin = 0){
  100.     vector <long int > cortado;
  101.    
  102.     if ( fin == 0) {
  103.         for (int i = 0 ; i < v.size(); i++){
  104.             cortado.push_back(v[i]);
  105.         }
  106.     }else {
  107.         for (int i = ini ; i < fin; i++){
  108.             cortado.push_back(v[i]);
  109.         }
  110.     }
  111.     return cortado;
  112. }
  113.  
  114. bool compare_max(const Pair_vect &uno, const Pair_vect &dos){
  115.     return  uno.consecutive.size() > dos.consecutive.size();
  116. }
  117.  
  118. int main(){
  119.     // Input value  : below
  120.     int below = 1000000;
  121.    
  122.     vector<long int> primos(lista_de_primos(below));
  123.     list< Pair_vect> long_list;
  124.    
  125.     for ( int I = 0 ; I < primos.size(); I++){
  126.         Pair_vect piv;
  127.        
  128.         for (int J = I + 1 ; J < primos.size() ; J++ ){
  129.             piv.sum = suma_elemn(primos,I+1,J+1);
  130.                            
  131.             if ( is_prime(piv.sum) && piv.sum < below){
  132.                 piv.consecutive = cut_vect(primos,I,J+1);  
  133.                 long_list.push_back(piv);
  134.             }
  135.             if (piv.sum > below) {
  136.                 J=J+1;
  137.                
  138.             }      
  139.         }
  140.     }
  141.    
  142.     cout << "El vector corresponde : " << endl;
  143.     long_list.sort(compare_max);
  144.     list<Pair_vect> :: iterator it = long_list.begin();
  145.     it->print(); cout << endl << endl;;
  146.     cout << "con " << it-> consecutive.size() << " elementos " << endl;
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment