Can you solve this math problem? (The long Math night: an embarrassing tale.)

Davide de Paolis - Dec 16 '19 - - Dev Community

A Software Engineer, a Lead ( of aircraft engineers ) and a startup CEO, enter a room.

They are handed over 20 Math problems and they have 4 hours to solve them. No calculator, no mobile phone, no internet, no help from home.
A nerdy fun evening? A nightmare? A joke?

All of them if you consider that those 20 Math problems were targeted for kids of the 6th and 7h grade...

Lange Nacht der Mathematik

A couple of weeks ago my kid (11 years old) decided to join with some classmates the "Long Math night" at his school.

An evening of Math games, pizza, and sweets.

Some support from the parents was required, to organize the kids into groups and eventually provide some guidance into solving the tasks.
I never really loved Math but I like code challenges, so I decided to join, despite the additional difficulty of understanding the tasks in German - where I am not yet so proficient.

cmon, these will be tasks for 12-year-old kids!

The first couple of exercises were easy, tricky for some kids but fun to solve and they managed quickly.
Then shit got real. Kids got bored and most of them left to go play hide & seek in the empty school, and the parents were there with the "hardest" tasks to solve.

parents in classroom

One of the task was the following:

// find the smallest of 4 sequential integers smaller 
// than the provided cap, that comply with following requirements:

// the first and smallest integer can be divided by 5
// the second integer can be divided by 7
// the third integer can be divided by 9
// the forth and bigger integer can be divided by 11
Enter fullscreen mode Exit fullscreen mode

No rocket science. And the next day with CodePen, I solved it in a couple of minutes, but there, on a Friday evening, with all that chaos and just with pencil and paper, we all had some hard time.

I must say I wasted quite some time trying out a completely wrong path.
The fact that I quickly wrote down the conditions like this
(x/5) & (x+1)/7 & (x+2)/9 & (x+3)/11) < y
and that I recently helped my kid with homework about Fractions and LeastCommonMultiple / GreatCommonDivider mislead me and convinced me the solution had to do with that topic

when you have a hammer everything looks like a nail

Of course, it would have something to do with LeastCommon Multiple, if we had to find the same number dividable by 5 & 7 & 9 & 11, but here we had 4 different numbers and 4 different dividers.

The second guy started writing an ideal chart where each line was the linear growth of each multiplier and tried to look for patterns or intersections...

Alt Text

The third guy tried brute-forcing every single operation for every multiplier of 5:

can 25 be divided by 5?
can 26 be divided by 7?
can 27 be divided by 9?
can 28 be divided by 11?

as so on, but as you can imagine, he got lost after a few minutes and hundred of numbers on his block note...

In the end a teacher stepped in and showed on the projector what the logic was - using excel - to visually represent both the logical steps, the thinking process and visualize the intersections.

easy

I can'r remember exactly what he did on excel but I tried out a couple of solutions and by adding a Modulo Conditional Formatting I was able to visually show my kid at home how to find the solution.

Solution with Excel

With Excel it was pretty clear, and with code it was trivial, still I really can´t imagine how the kids should have found the solution on their own.
Even adopting the best approach there were still a lot of calculations to be done. and with pretty big numbers too..

const bruteFind= (cap) => {
  let n=1
  while(n<cap) {
    if(n%5 == 0 &&  (n+1)%7 == 0 &&  (n+2)%9 == 0  && (n+3)%11 ==0){
      return n;
    }
    n++;
  }
  return -1;
}
console.log("brute: ", bruteFind(2019))

const betterFind= (cap) => {
  let n=11
  while(n<cap) {
     if((n-1)%9 == 0  &&  (n-2)%7 == 0 && (n-3)%5 == 0 ){
       n-3
    }
    n+=11;
  }
  return -1;
}

console.log("better: ",betterFind(2019))

const verboseFind= (cap) => {
  let steps = 1
  let calculations = 0;
  let n=11
  while(n<cap) {
    if((n-1)%9==0) {
      calculations++;
      if((n-2)%7==0) {
        calculations++;
        if((n-3)%5 == 0) {
          calculations++
          return {found: n-3, steps, calculations};
        }
      }
    }
    n+=11;
    steps++
  }
  return -1;
}
console.log("verbose: ",verboseFind(2019))

Enter fullscreen mode Exit fullscreen mode

What I still wonder is, is there a specific formula to find that number in just a bunch of operations?


PS: Something else that somehow blocked me during that evening, was that I was already thinking of patterns/algorithms or methods to optimize the search and avoid brute-forcing. I really could not just take the pencil and start writing out all the combination, but most of the exercises were indeed thought to be solved like that.. basic math over and over :-)


Photo by Roman Mager on Unsplash

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player