Puzzle

Project Euler - Problem 1 - Sum All Integers Below 1000 and Divisible By 3 or 5

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 - Programming Math Problems

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.

Formatting a Comma Delimated List Redux

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;

Programming Puzzle: What is the best way to format a comma delimited list?

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.

Syndicate content