Number crunching

Caporegime
Joined
1 Dec 2010
Posts
53,767
Location
Welling, London
Guys, what's the best way of entering 8 numbers and generating all possible combinations of 6 numbers each?

Are there any free programs that will do this for me.
 
Here's some C++. No I can't make a windows binary. Somebody else probably can. Also, it will happily include duplicates.

PHP:
//Combo-maker by joeyjojo.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

//Globals are nice
std::ofstream file;

//I stole the following from http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
template <class T>
inline std::string to_string (const T& t)
{
	std::stringstream ss;
	ss << t;
	return ss.str();
}
//End of stolen goods

//Recursive function
void combos(std::string first, std::string second)
{
	size_t secondsize=second.size();
	switch (secondsize)
	{
	// If you want combinations of 6 from a set of 8, the 
	// number below should be 2 (i.e. 8 minus 6)
	case 2:
		file << first << std::endl;
		break;
	default:
		for(int i=0; i<secondsize; ++i)
		{
			std::string newfirst=first;
			newfirst += second[i];
			std::string newsecond;
			for(int j=0; j<secondsize; ++j)
			{
				if(i != j) newsecond += second[j];
			}
			combos(newfirst, newsecond);
		}
	}
}

int main(int argc, char* argv[])
{
	if(argc!=2)
	{
		std::cout << "Give me some work!" << std::endl;
		//call it with an argument e.g. "./a.out 01234567"
	}else{
		file.open("output.txt");
		std::string str = to_string(argv[1]);
		combos("",str);
	}
		
	return 0;
	
}
 
Added a little bit which removes duplicates, however you must put the numbers in with the duplicate numbers grouped together, e.g. 84442335.

It works beautifully, e.g. the string 01111112 produces the 43 unique strings out of the entire set of 20160.

PHP:
//Combo-maker by joeyjojo.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

//Globals are nice
std::ofstream file;

//I stole the following from http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
template <class T>
inline std::string to_string (const T& t)
{
	std::stringstream ss;
	ss << t;
	return ss.str();
}
//End of stolen goods

//Recursive function
void combos(std::string first, std::string second)
{
	size_t secondsize=second.size();
	switch (secondsize)
	{
	// If you want combinations of 6 from a set of 8, the 
	// number below should be 2 (i.e. 8 minus 6)
	case 2:
		file << first << std::endl;
		break;
	default:
		for(int i=0; i<secondsize; ++i)
		{
			std::string newfirst=first, newsecond="";
			newfirst += second[i];
			for(int j=0; j<secondsize; ++j)
			{
				if(i != j) newsecond += second[j];
			}
			//This while removes duplicates (only if they appear together in the string)
			while(newfirst[newfirst.size()-1] == second[i+1])
			{
				i++;
			}
			combos(newfirst, newsecond);
		}
	}
}

int main(int argc, char* argv[])
{
	if(argc!=2)
	{
		std::cout << "Give me some work!" << std::endl;
		//call it with an argument e.g. "./a.out 01234567"
	}else{
		file.open("output.txt");
		std::string str = to_string(argv[1]);
		combos("",str);
		//std::string rar="";
		//std::cout << rar.at(rar.size()) << std::endl;
	}
		
	return 0;
	
}
 
Back
Top Bottom