The first Project Euler problem is to calculate the sum of all integers below 1000 which are divisible by either 3 or 5.
My solution is implemented entirely in C++ templates. The value is recursively calculated at compile time. The template specialization struct Problem1<0> stops the recursion and returns 0.
To compile this code with gcc you must expand the maximum allowed template recursion depth to at least 1000.
Project Euler presents a rather extensive list of problems which require a combination of programming skills and math skills to solve.
The problems are interesting. I hope to from time to time post solutions to them on this website. However, I'm going to use an atypical programming language. I plan that each one I solve will be solved with C++ templates. By solving with templates the solution to the problem will be calculated at compile time and made available at runtime.
A few weeks back, I posited the question, "What is the best way to format a comma delimited list?"
After seeing all of the succinct ways to accomplish this in other languages, I got a little jealous and decided to write this C++ algorithm which acts about the same.
I feel like there must be a c++ standard library way of doing this that I'm some how overlooking.
template <typename InItr> std::string join(InItr begin, InItr end, const std::string &joiner) { std::string ret; while (begin != end) { std::stringstream ss;
Say you have a list, array, vector, storage format doesn't matter:
std::vector<int> vec; vec.push_back(1); vec.push_back(17); vec.push_back(9);
And you want to format this list into a comma delimited string without the trailing comma, what is the best way to do this?
Example output:
1,17,9
In any language, with any list type. Post your answers here. Be sure to use code formatting for your answer, to make it easier for everyone to read.
Recent comments
2 days 20 hours ago
6 days 16 hours ago
6 days 16 hours ago
1 week 12 hours ago
1 week 5 days ago
1 week 5 days ago
2 weeks 5 days ago
10 weeks 20 hours ago
14 weeks 1 day ago
17 weeks 2 hours ago