Skip to main content

Posts

Showing posts from November, 2022

Friday Quiz maths problem and Heap's algorithm

 T oday's Friday Quiz maths problem is a corker: When the official answer came, a simple solution was given with the answer (I'll save that till later) which didn't make things clear to me. But without seeing an easier way, my approach, as usual, was to find all the permutations of the 9 digits, and count the ones that meet the criteria.  The *number* of ways you can arrange some objects is easy to calculate. But how to actually shuffle those around in memory so that you can test them? Long story short, I devised my own way to shuffle the numbers around, which I thought would work and it nearly did. But not quite. I turned to the web and found Heap's algorithm, which is very similar to what I was trying to do, except that it does actually work.  It's simple, and the Wikipedia page gives some example code which is recursive. Here's my version of that in C (I made my array global, rather than passing pointers around) In my program, printNumber() also performs the