Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * Written by I.F. Luis Mex Dzib 4/sep/2013
- *
- * This program Solve the following:
- *
- * The prime 41, can be written as the sum of six consecutive primes:
- 41 = 2 + 3 + 5 + 7 + 11 + 13
- This is the longest sum of consecutive primes that adds to a prime below one-hundred.
- The longest sum of consecutive primes below one-thousand that adds to a prime,
- contains 21 terms, and is equal to 953.Which prime, below one hundred-thousand,
- can be written as the sum of the most consecutive primes?
- ********************************************************************************
- * /*****************************************************************************/
- #include <iostream>
- #include <vector>
- #include <list>
- #include <iomanip>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- struct Pair_vect {
- long int sum;
- vector<long int> consecutive;
- void print() const {
- cout << "(" << sum << ", " ;
- cout <<"[" << consecutive.front() ;
- for (int i = 1 ; i < consecutive.size() ; i++){
- cout <<", "<< consecutive[i] ;
- }
- cout << "]"<< ")";
- }
- };
- bool is_prime(long int x){
- for(int i = 2 ; i <= sqrt(x) ; i++){
- if ( x % i == 0) {
- return false;
- }
- }
- return true;
- }
- vector <long int> lista_de_primos( int x = 2){
- vector<long int> lista_ini;
- for (int i = 2 ; i<= x ; i++){
- if(is_prime(i)) lista_ini.push_back(i);
- }
- return lista_ini;
- }
- template <class T>
- void prind_list(const vector<T>&v){
- cout << "[";
- if (!v.empty()){
- cout << v.front();
- typename vector<T> :: const_iterator it = v.begin();
- it++;
- for (; it != v.end() ; it++){
- cout <<", "<<*it ;
- }
- cout << "]" << endl;
- }
- }
- long int suma_elemn(const vector<long int> &v, int ini = 0, int fin = 0){
- long int suma = 0;
- if (v.empty()) return suma;
- if ( fin == 0 ){
- for (long int i = ini ; i < v.size() ; i++){
- suma = suma + v[i];
- }
- }
- else{
- for (long int i = ini-1 ; i < fin ; i++){
- suma = suma + v[i];
- }
- }
- return suma;
- }
- vector <long int > cut_vect( const vector<long int> &v , int ini = 0 , int fin = 0){
- vector <long int > cortado;
- if ( fin == 0) {
- for (int i = 0 ; i < v.size(); i++){
- cortado.push_back(v[i]);
- }
- }else {
- for (int i = ini ; i < fin; i++){
- cortado.push_back(v[i]);
- }
- }
- return cortado;
- }
- bool compare_max(const Pair_vect &uno, const Pair_vect &dos){
- return uno.consecutive.size() > dos.consecutive.size();
- }
- int main(){
- // Input value : below
- int below = 1000;
- vector<long int> primos(lista_de_primos(below));
- list< Pair_vect> long_list;
- for ( int I = 0 ; I < primos.size(); I++){
- Pair_vect piv;
- for (int J = I + 1 ; J < primos.size() ; J++ ){
- piv.sum = suma_elemn(primos,I+1,J+1);
- if ( is_prime(piv.sum) && piv.sum < below){
- piv.consecutive = cut_vect(primos,I,J+1);
- long_list.push_back(piv);
- }
- }
- }
- cout << "El vector corresponde : " << endl;
- long_list.sort(compare_max);
- list<Pair_vect> :: iterator it = long_list.begin();
- it->print(); cout << endl << endl;;
- cout << "con " << it-> consecutive.size() << " elementos " << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment