Skip to main content


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
Recent posts

Friday Quiz maths problem 14 Oct 22

I did spend a little time trying to reason it out and arrive at an answer through logic but soon gave up and resorted to my usual method of 'try all the numbers and count how many fit'. Using my trusty Minstrel Forth (Jupiter Ace clone) I have written a program to cycle through all 10-digit numbers that use 1,2 and 3  and test / count  the number that qualify (adjacent numbers differ by 1) It's taking a while. As I write this it seems to be running but it'll take a little while longer before I have an answer. Once it's finished and I know it's working properly I'll write my program below for Forth fans. [update] The answer is 64 and this week we do have some working.   My program did eventually come up with the right answer after a bit of debugging and you can see the pattern referred to. The listing is below, please feel free to type it in, or paste it if you have one of my USB keyboard / serial adaptors .   I love using recursion. Here I've re-used som

Friday quiz maths problem 16 Sep 2022

Sadly the Friday quiz that I subscribe to is having a break for three weeks. I have subscribed to other maths problems and this one that caught my eye this week: The Baseball Game Ivy has created a game for her school’s math fair. She put three baseballs, numbered 1, 2, and 3, into a bag. Without looking, a player will randomly draw a baseball from the bag, record its number, and then put the baseball back into the bag. They will do this two more times and then calculate the sum of the three numbers recorded. If the sum is less than 8, the player will win a prize. What is the probability that a player will win a prize when they play this game once? This isn't too difficult to work out. I did it with a spreadsheet but you can reason it out I think.  Look away now if you want to work it out for yourself first.  I haven't yet had the official answer, but the answer that I and my partner have both found is 85.185%, that being 23 winning combinations out of 27 possible combinations.

Friday Quiz maths question 8 Jul 22

  H o w  many three-digit numbers are ther e where the middle digit is lower than  both the first digit and the third digit ? My reaction to this is to loop through all of the numbers 100-999 and count the ones that pass the test. (yes, lines 40 and 45 look a little inelegant with that repetition, but I tried it with a goto (yuk) and gosub and this was the shortest and neatest of my variations.  I wanted to print the qualifying numbers as well as inc'ing the count). I remember a very similar question in the past, involving 3-digit numbers. I think it was Mr Beckett who used a single loop in Forth and wrote words to separate out the hundreds, tens and units.  (Seemed a little counter-intuitive to me but Forth doesn't allow you access to the indices of three nested loops). So I thought I'd give that a go to see whether it would be more elegant than the BASIC version.  Here are my 'hundreds', 'tens' and 'units' words (made easier as the default type in

Friday Quiz maths question, 24 Jun 2022, a day late.

  There is just one pair of numbers (x,y)   where  when you add them together, mult iply them t ogether or divide x by y , the answers are all equal .   What are those numbers ? If you'd like to have a go at finding the answer, please do, and I'd love to hear how you did it. Below are my thoughts, experiments and answer. I should have found the answer sooner than I did.  First of all I used BASIC, set up a couple of loops for x and y and tested for meeting the conditions. That didn't produce an answer and so I did try smaller steps and included negative numbers. I now know that I should have found the answer then but for some reason my program didn't produce the result.  Wanting to at least find the ballpark , I thought about ways to plot these three conditions on a graph. A 2d graph wouldn't really do it because we want to test all values of x and y. I tried using some 3d graphing systems to plot z=x+y, z=x*y and z=x/y,  to see where they all intersected.  But it w

Friday quiz problem 10 Jun, an exercise in the mod function

T oday's Friday Quiz question seems to have been devised as an exercise in writing a computer program using the mod() function: A pl a toon of soldiers is lining up for a parade.   When they line up in rows of 3, one soldier is left over.  When they line up in rows of 4,  two soldiers are left over.  When they line up in rows of 5,  three soldiers are left over.  When they line up in rows of 6, four soldiers are left over.  However, they can line up in rows of  7  with no-one left over.  What is the smallest possible number o f soldiers in the platoon ? Spoiler alert: my answer is in the video. But writing the program is the fun part for me, not coming up with the answer. As I say in the video, any language should have the mod() function. If not, you can write your own function / routine / word ,  it's the remainder after dividing one number by another.  Which is basically what the question is asking for. The lowest number where n mod 3 = 1, n mod 4 = 2 etc. Here's my progr

Friday Quiz problem 20 May - ?prime in Forth

T oday's question is:  In the Quarante Republic, every month contains 40 days, numbered 1 to 40.  Any day whose number is divisible by 6 is a holiday, and any day whose number is a prime is also a holiday.  How many times in a month does a single working day occur between two holidays? This lends itself very well to a Forth program. The first interesting challenge is to write a ?prime word in Forth. I don't remember doing this before. Google tells me that someone has published a Forth program for exactly this word but it looks unnecessarily complex for our purposes today.  I didn't know this, but you can find out whether a number is prime by trying to divide it by each prime up to the square root of your number. In our case that's only 2, 3 and 5. And only then if those primes are smaller than our number. The solution I have right now is less efficient, but does the job. I'll be looking to improve this. Please  share your own code if you have something more elegant