Sherlock and The Beast

  • + 0 comments

    While not the optimal, I was happy with this one in Java:

        public static void decentNumber(int n) {
            // Decent number s of length n, can be represented as
            // x = Number of sets of 5
            // y = Number of Sets of 3
            //  (3*x) + (5*y) = n;
            // Isolating x
            // x = (n - (5*y)) / 3
            // Need to solve for lowest integer y > 0 than gives integer x > 0
            
            for(int y = 0; y < n; y++){
                if((n - (5 * y)) % 3 == 0) {
                    int x = (n - (5 * y)) / 3;
                    if (x < 0) {
                        break;
                    }
                    String decentForLength = "5".repeat(x * 3) + "3".repeat(y * 5);
                    System.out.println(decentForLength);
                    return;
                }
            }
            System.out.println(-1);
        }