示例#1
0
文件: 023.c 项目: cfredric/euler
//return true if n can be expressed as the sum of two abundant numbers
int summer(int n){
	int i = 2;
	int j;
	for(i = 2;i <= n/2;i++){
		j = n-i;
		if( (abund(i) == TRUE) && (abund(j) == TRUE)){
			return TRUE;
		}
	}
	return FALSE;
}
void initializeMetaCommunity()
{
	int Jm = P.Jm;
	std::vector<int> abund(1,0);
	std::size_t nsp = 1;
	abund.push_back(1);
	//std::cout << "progress: " << "\t";
	(*(P.FORM))->add_systemLog("progress: \r\n");
	int disp = (int)(Jm * 0.1);
	for(int j = 1; j < Jm; ++j)
	{
		if(j % disp == 0)
		{
			int x = j / disp;

			std::ostringstream osstream;
			osstream << x;
			std::string str = osstream.str();
			str += "\r\n";
			(*(P.FORM))->add_systemLog(str);//std::cout << j / disp << "\t";
		}
		//std::cout.flush();
		double x = uniform();
		double val = P.theta / (P.theta + j -1);
		if(x < val)
		{
			nsp++;
			if(nsp > (abund.size()-1))
			{
				int dif = 1+nsp - abund.size();
				for(int k = 0; k < dif; ++k) abund.push_back(0);
				
			}
			
			abund[nsp] = 1;
		}
		else
		{
			int translate_to_abund = (int)((x * j)-1);
			//now find corresponding species
			std::size_t index = 0;
			while(index < abund.size())
			{
				translate_to_abund -= abund[index];
				if(translate_to_abund <= 0) break;

				index++;
			}

			abund[index] = abund[index] + 1;
		}
	}

	for(std::size_t i = 0; i < abund.size() ;++i)
	{
		species newS = newSpecies();
		newS.count = abund[i];

		metaCommunity.push_back(newS);
	}

	//remove all empty species
	std::vector<species> temp;
	for(std::vector<species>::iterator m = metaCommunity.begin(); m != metaCommunity.end(); ++m)
	{
		if((*m).count > 0) temp.push_back((*m));
	}
	metaCommunity = temp;

	std::sort(metaCommunity.begin(), metaCommunity.end(), sortSpeciesCount);
	//update fractions
	double cumsum = 0.0;
	for(std::vector<species>::iterator it = metaCommunity.begin(); it != metaCommunity.end(); ++it)
	{
		double add = 1.0 * (*it).count / Jm;
		cumsum += add;
		(*it).fraction = cumsum;		
	}
}