Palindrome decimal number

A number is a palindrome if the numbers are arranged in reverse order and the result is the same as the original number. For example, 123454321 is a palindrome.

The problem

Find the minimum value of all palindromes represented in decimal, binary, and octal numbers greater than the decimal number 10.

Examples of decimal, binary, and octal numbers

Train of thought

Since it is a binary palindrome, if the lowest digit is 0, then the corresponding highest digit is also 0. However, starting with 0 is definitely inappropriate, so the lowest position is 1.

If the lowest digit in binary is 1, then this number must be odd, so you can just think about odd numbers. You can then simply write a program to search in order, starting with the next digit of 10, 11. In Ruby, for example, you can find the numbers that match the criteria in the following code (Listing 01.01).

Code Listing 01.01 (q01_01.rb)

While true if num.to_s == num.to_s.reverse && num.to_s(8) == num.to_s(8).reverse && num.to_s(2) == Num.to_s (2). Reverse puts num break end # Searches for odd numbers only, adding 2 num += 2 end each timeCopy the code

Let’s implement the same logic in JavaScript. There is no standard function built into JavaScript to reverse a string, so you first need to encapsulate a method that returns the reversed string, and the rest of the flow is the same as in Listing 01.01. The implementation of the JavaScript version is shown in Listing 01.02.

Code Listing 01.02 (q01_02.js)

/* Add method to return reverse String */ string.prototype.reverse = function (){return this.split("").reverse().join(""); } /* start from 11 */ var num = 11; while (true){ if ((num.toString() == num.toString().reverse()) && (num.toString(8) == num.toString(8).reverse()) && (num.toString(2) == num.toString(2).reverse())){ console.log(num); break; } /* only search for odd numbers, each increment 2 */ num += 2; }Copy the code

Point

Many languages provide methods for converting integers to binary or octal numbers. Table 2 summarizes the relevant functions or methods for representative languages, but C does not provide an interface for direct transformation

Interfaces for base conversions in various programming languages

The answer

  • The binary number is 1001001001
  • The octal number is 1111

The four operations of a sequence

When you were a kid, you probably played the game of “combine four numbers in your license plate number to get 10.”

The method of combination is to insert four operators between each digit to form an expression, and then calculate the result of the expression (some digits can have no operator, but at least one operator must be inserted).

  • 1234 → 1+ 2× 3-4 = 3
  • 9,876 → 9 times 87+ 6 = 789

Suppose that the condition here is that the calculation result of the combined formula is “the number obtained by reversing the order of the numbers on each digit of the original number”, and the operation of the formula is carried out in the order of four operations (first multiplication and division, then addition and subtraction).

Then between 100 and 999, meet the following conditions.

  • 351-3 x 51 = 153
  • The 621-6 x = 21 126
  • The 886-8 x 86 = 688

The problem

Find the number between 1000 and 9999 that satisfies the above conditions.

Train of thought

When solving this problem, the “computer approach” affects the implementation approach. If you want to implement in A calculator, you usually use the inverse Polish notation A, whereas in this case it is easier to use the built-in functionality of the English language.

Many scripting languages provide standard functions such as eval. If implemented in JavaScript, listing 02.01 can be used to solve the problem.

var op = ["+", "-", "*", "/", ""]; for (i = 1000; i < 10000; i++){ var c = String(i); for (j = 0; j < op.length; j++){ for (k = 0; k < op.length; k++){ for (l = 0; l < op.length; l++){ val = c.charAt(3) + op[j] + c.charAt(2) + op[k] + c.charAt(1) + op[l] + c.charAt(0); If (val.length > 4){/* Must insert 1 operator */ if (I == eval(val)){console.log(val + "=" + I); } } } } } }Copy the code

Eval in line 10 is the key point in this case, and it’s just a matter of selecting and setting the operators. Although there is deep loop nesting, there is no problem as long as the number of digits are determined.

Reverse Polish notation (RPN) is a mathematical expression introduced by Polish mathematician Jan Vukasiewicz in 1920. In Reverse Polish notation, all operators are placed after operands, so it is also called suffix notation. – editor’s note

With this in mind, if you set the op variable on line 1 of your code to the following value, you can further improve your program execution.

var op = ["*", ""];
Copy the code

Point

If you implement the same logic in another language, you need to do something special with 0. In Ruby, for example, “numbers starting with 0” are treated as octal numbers, so numbers starting with 0 must be excluded. In addition, you also need to exclude the case where the divisor is 0.

The answer

5931 (5 * 9 * 31 = 1395)

The last

Ruby writes source code for the main language, but there are cases where it is implemented in JavaScript as in this case. When implemented in JavaScript, the output is console.log, which can be verified by the browser. Use Mozilla Firefox to open the HTML file loaded with JavaScript source code, and then go to “Developers” → “Web Console” (or “More tools” → “Developer Tools” if you use Google Chrome). You can verify the results of your code execution.