mercredi 1 février 2017

correct abundant sums algorithm [on hold]

could you please cross check my code , it compiles but outputs the wrong sum of numbers that cannot be made by a sum of to abundant numbers.i have tried proof reading it but cant see an errors. its actually solution to a project euler problem. I dont need new methods , i would like to know my mistakes . thanks.

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
bool pness(long a)
{
bool ans = false;
long b=a;
long holder=0;
for(long i=1;i<b/2+1;i++)
{
    if(b%i==0)
    {
        holder+=i;
    }
}
if(holder>a)
{
    ans=true;
}
return ans;
}

int main()
{

vector<long>mall;

for(int i=1;i<28123;i++)
{
    mall.push_back(i);
}

vector<long>ans;

vector<long>::iterator anst ;

vector<long>store;
vector<long>::iterator it;
cout<<"Finding abundant numbers... "<<endl;

for(long i=1;i<28123;i++)
{
   if(pness(i))
   {
       store.push_back(i);
   }
}

cout<<"Abundant numbers found"<<endl;
 cout<<"Stage 1 complete."<<endl;
 cout<<"Number of Abundate numbers = "<<store.size()<<endl;
 cout<<"Generating sums ..."<<endl;

 vector<long> ware;

 for(it=store.begin();it<store.end();it++)
 {
   vector<long>::iterator bt=it;
   for(long i=1;i<(long)(store.end()-bt);i++)
   {
       long champ = (*bt)+ (*(bt+i));
      if(champ<28123 && find(ware.begin(),ware.end(),champ)==ware.end())
       {
           ware.push_back(champ);
       }
   }
 }
  cout<< "All sums generated."<<endl;
 cout<<"Number of sums = "<<ware.size()<<endl;
 cout<<"Finding and summing exceptions..."<<endl;

 sort(mall.begin(),mall.end());
 sort(ware.begin(),ware.end());
                                                                       `   `set_difference(mall.begin(),mall.end(),ware.begin(),ware.end(),
 insert_iterator<vector<long> >(ans, ans.end()));
 long dun = accumulate(ans.begin(),ans.end(),0);
 cout<<dun;

  return 0;

}

Aucun commentaire:

Enregistrer un commentaire