DEV Community

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

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

dvddpl profile image Davide de Paolis ・4 min read

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

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.


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;
  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 ){
  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) {
      if((n-2)%7==0) {
        if((n-3)%5 == 0) {
          return {found: n-3, steps, calculations};
  return -1;
console.log("verbose: ",verboseFind(2019))

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

Discussion (5)

Editor guide
nickholmesde profile image
Nick Holmes

Well, all the solutions are given by the integer solution to

 5a + 3 = 7b + 2 = 9c + 1= 11d

Which is

 a = 693 n + 347     
 b = 495 n + 248
 c = 385 n + 193
 d = 315 n + 158

where n is an integer

As we know these are sequential, we only really need to know the inequality

11 * (315 n + 158) < cap

As n needs to be an integer, we can write that like this:

n = ⌊(cap-1738)/3465)⌋

So if we want a function that returns the lowest of the 4 number that satisfies the condition,

lowest = 5 * (693 * ⌊(cap-1738)/3465)⌋ + 347)

       = 3465 * ⌊(cap-1738)/3465)⌋ + 1735

or in F#;

let lowest cap = 3465. * Math.Floor((cap-1738.) / 3465.) + 1735.
amnemonic profile image
Adam Mnemonic

I like this approach. Maybe it's only me but it still seems to sophisticated for 12 years old kids 😁

dvddpl profile image
Davide de Paolis Author

i guess i need some time to digest this..

amnemonic profile image
Adam Mnemonic

So I see that sample solution is: 1735, 1736, 1737 and 1738.

If we do:
When we divide this by two then we get 1735.5 which is close to first number... I just need to think how to get rid of 1 from first calculation and reason to divide that by two 😁

dvddpl profile image
Davide de Paolis Author

I also noted that correlation but then could not find anything to get closer to the solution..