Hacker News new | past | comments | ask | show | jobs | submit login

> a number is divisible by 3 iff the sum of the individual digits is divisible by 3

And how do easily verify this divisibility when "Numeric types, number literals and their associated methods and operations are forbidden?"






Since I read the constraint to allow for single digit numeric values*, I guess they are looking for a solution similar to:

  function isDivisibleByThree(num: string): boolean {
      let mod3 = "012012012012";
      let modulo = "0";
      for (const digit of num) {
          modulo = mod3[Number(digit) + Number(modulo)];
      }
      return modulo === "0";
  }
If adding two single digit numbers is also prohibited it can be implemented with a lookup and keep everything in string representation.

"The programmer can use whatever representation they see fit with the only restriction being that it could only contain letters, numbers and symbols that could be typed with a single stroke"




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: