Click here to Skip to main content
15,902,299 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: OpenSource Audio DSP Algorithms/Filters? Pin
Yuval Naveh22-Dec-10 23:25
Yuval Naveh22-Dec-10 23:25 
QuestionNumber of Integer Solutions to an Equation Pin
Skippums23-Nov-10 15:03
Skippums23-Nov-10 15:03 
AnswerRe: Number of Integer Solutions to an Equation Pin
Sauro Viti24-Nov-10 0:19
professionalSauro Viti24-Nov-10 0:19 
AnswerRe: Number of Integer Solutions to an Equation Pin
Luc Pattyn24-Nov-10 3:56
sitebuilderLuc Pattyn24-Nov-10 3:56 
GeneralRe: Number of Integer Solutions to an Equation Pin
Skippums24-Nov-10 4:26
Skippums24-Nov-10 4:26 
AnswerRe: Number of Integer Solutions to an Equation Pin
Luc Pattyn24-Nov-10 4:47
sitebuilderLuc Pattyn24-Nov-10 4:47 
GeneralRe: Number of Integer Solutions to an Equation Pin
_Erik_24-Nov-10 6:00
_Erik_24-Nov-10 6:00 
AnswerRe: Number of Integer Solutions to an Equation [modified] Pin
Alain Rist26-Nov-10 1:50
Alain Rist26-Nov-10 1:50 
Hi Jeff,

With VC2010 (or gcc 4.5) C++0x implementation:

#include <utility>
#include <array>
#include <algorithm>

template <unsigned int z>
size_t NumSolutions()
{
	typedef std::pair<unsigned int, unsigned int> Solution;
	std::array<Solution, z * z + 2 * z> All;

	std::generate(All.begin(), All.end(), 
		[]()->Solution{
		static unsigned int x = 0, y = 0;
		return y == z ? Solution(++x, y = 0) : Solution(x , ++y);});

	return std::count_if(All.begin(), All.end(), 
		[](const Solution& sol){return z == 3 * sol.first * sol.first + sol.second * sol.second;});
}


With previous C++03 compilers:
#include <utility>
#include <vector>
#include <algorithm>

typedef std::pair<unsigned int, unsigned int> Solution;

template <unsigned int z>
bool IsSolution(const Solution& sol)
{
	return z == 3 * sol.first * sol.first + sol.second * sol.second;
}

template <unsigned int z>
Solution Next()
{
	static unsigned int x = 0, y = 0;
	return y == z ? Solution(++x, y = 0) : Solution(x , ++y);
}

template <unsigned int z>
size_t NumSolutions()
{
	std::vector<Solution> All(z *z + 2 * z);
	std::generate(All.begin(), All.end(), Next<z>);
	return std::count_if(All.begin(), All.end(), IsSolution<z>);
}


You may test both with:
#include <iostream>
int main()
{
	using std::cout;
	using std::endl;

	cout << "NumSolutions<4>(): " << NumSolutions<4>() << endl;
	cout << "NumSolutions<7>(): " << NumSolutions<7>() << endl;
	cout << "NumSolutions<28>(): " << NumSolutions<28>() << endl;
	return 0;
}


cheers,
AR

Edited for correct size of All vector.
Edited for use of adequate std::array in C++0x version.
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Friday, November 26, 2010 4:45 PM

Questionfind lower bound Pin
liquid_18-Nov-10 6:11
liquid_18-Nov-10 6:11 
AnswerRe: find lower bound Pin
NeverHeardOfMe18-Nov-10 6:22
NeverHeardOfMe18-Nov-10 6:22 
GeneralRe: find lower bound Pin
liquid_18-Nov-10 20:25
liquid_18-Nov-10 20:25 
GeneralRe: find lower bound Pin
NeverHeardOfMe18-Nov-10 21:49
NeverHeardOfMe18-Nov-10 21:49 
GeneralRe: find lower bound Pin
harold aptroot18-Nov-10 21:35
harold aptroot18-Nov-10 21:35 
AnswerRe: find lower bound Pin
Alain Rist18-Nov-10 23:24
Alain Rist18-Nov-10 23:24 
GeneralRe: find lower bound Pin
liquid_20-Nov-10 11:32
liquid_20-Nov-10 11:32 
GeneralRe: find lower bound Pin
Alain Rist20-Nov-10 20:04
Alain Rist20-Nov-10 20:04 
GeneralRe: find lower bound Pin
liquid_21-Nov-10 5:09
liquid_21-Nov-10 5:09 
QuestionTIL...... [modified] Pin
NeverHeardOfMe3-Nov-10 23:57
NeverHeardOfMe3-Nov-10 23:57 
AnswerRe: TIL...... Pin
Radhakrishnan G.4-Nov-10 1:40
Radhakrishnan G.4-Nov-10 1:40 
GeneralRe: TIL...... Pin
NeverHeardOfMe4-Nov-10 2:38
NeverHeardOfMe4-Nov-10 2:38 
AnswerRe: TIL...... Pin
Richard MacCutchan4-Nov-10 2:46
mveRichard MacCutchan4-Nov-10 2:46 
GeneralRe: TIL...... Pin
NeverHeardOfMe4-Nov-10 2:57
NeverHeardOfMe4-Nov-10 2:57 
GeneralRe: TIL...... Pin
Richard MacCutchan4-Nov-10 3:24
mveRichard MacCutchan4-Nov-10 3:24 
GeneralRe: TIL...... Pin
NeverHeardOfMe4-Nov-10 3:28
NeverHeardOfMe4-Nov-10 3:28 
GeneralRe: TIL...... Pin
Richard MacCutchan4-Nov-10 6:09
mveRichard MacCutchan4-Nov-10 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.