Robert Eisele
Engineer, Systems Architect and DBA

Misc: Pre-School Math Problem

This problem can be solved by pre-school children in 5-10 minutes, by programer - in 1 hour, by people with higher education... well, check it yourself! :)

8809=6
7111=0
2172=0
6666=4
1111=0
3213=0
7662=2
9313=1
0000=4
2222=0
3333=0
5555=0
8193=3
8096=5
7777=0
9999=4
7756=1
6855=3
9881=5
5531=0
2581=?

Solution

I leave it to you to find the rule. But as I call me a programmer, here is my 1 hour solution:

function solution(n) {

   var cnt = [
      1,0,0,0,1,0,1,0,2,1];

   var s = 0;
   while (n > 0) {
      s+= cnt[n % 10];
      n = n / 10 | 0;
    }
    return s;
}
solution(2581);

« Back to problem overview