📜 ⬆️ ⬇️

Denial of the sage. Analyzing the proposed algorithm

Denial of the sage. Analyzing the proposed algorithm

Objective: To write an entertainment and educational publication on the programming language java.

The publication is intended for novice programmers. For those who are just starting to learn the Java programming language. However, to understand and implement the material it is assumed that the reader is already familiar with the syntax of the language, with the types of variables, with the scope of the variables, with classes and methods, with working with strings.
')
At present, such a meme is quite common on social networks.


Figure 1.

In the inscriptions on the picture there are various versions of statements. Some even claim that we were wrongly taught to multiply at school. After all, this method of multiplying two-digit numbers is supposedly much easier. But in the picture we see only one particular case, when the sage's statement is true.

Knowing the java language, we can easily check all special cases. We can find out in which cases the algorithm is working correctly and in which such an algorithm is not executed. We will design and write a program for the derivation of all statements for two-digit numbers.
Calculate the number of correct and incorrect statements. We will not bother much with inheritance and interfaces. We will design and implement a class in which the work of checking and counting will be performed. We formulate the purpose of our work.

Objective: Design and implement a class that verifies the above algorithm.

The class will display all two-digit numbers for which the algorithm is executed correctly.
and all two-digit numbers for which the algorithm is executed incorrectly. We will also count the number of correct and the number of incorrect answers.

Let's call our class "denial of the sage" - DenialOfTheSage. We will place our class in the “denialOfTheSage01” package.

/* *   */ package denialOfTheSage01; /** * * @author */ public class DenialOfTheSage { public static void main(String[] args) { } } 

We will need variables to count the number of correct answers and to count the number of incorrect answers of the algorithm. Let's call our variables countSageIsRight and countSageIsWrong. Select the type of variables int. Variables of the whole type.

 int countSageIsRight = 0; //    int countSageIsWrong = 0; //    

We introduce another variable that will show the total number of responses. Let's call the variable countAnswer.

 int countAnswer = 0; //    

We assigned the value 0 to variables, because there are no answers at the very beginning. For the designation of the first and second two-digit numbers, we will use the variables x and y.

 int x,y; //     ,    

For the output of numbers and for our convenience, we write the output method of the variables x and y. Let's call our method - showXY:

 public static void showXY(int x, int y) { System.out.print(" x = " + x + " y = " + y + " "); } 

Let us write two methods for the conclusion that our “sage” is mistaken or not in a particular case.

 public static void showSageIsTrue() { System.out.println("!"); } public static void showSageIsFalse() { System.out.println("!"); } 

In the program, we need to sort out all two-digit numbers from ten to 99 inclusive. Closed interval: [10,99]. Let's write a cycle in which we will sort out our two-digit numbers.

 for (x = 10; x <= 99; x++) { // x   10  99  for (y = 10; y <= 99; y++) { // y   10  99  countAnswer++; System.out.print(countAnswer + ". "); //   showXY(x, y); //     x = y = if (x * y == result(x, y)) { showSageIsTrue(); //  "" countSageIsRight++; //   ""   } else { showSageIsFalse(); //  "" countSageIsWrong++;//   ""   } } } 

Place it in the main method. We write a method for calculating the result. We implement the proposed algorithm.

 public static int result(int x, int y) {//       int minus_x = 100 - x; int minus_y = 100 - y; int plus = minus_x + minus_y; int hundr_minus_summ = 100 - plus; int begin = hundr_minus_summ; //   int end = minus_x * minus_y; //   String strResult = String.valueOf(begin) + String.valueOf(end); //      int result = Integer.parseInt(strResult); return result; } 


We write a small test to make sure that string wrapping works correctly:

  public class TestCont { public static void main(String[] args) { int num = 0; String strResult; for (int x = 10; x <= 99; x++) { // x   10  99  for (int y = 10; y <= 99; y++) { // y   10  99  num++; strResult = String.valueOf(x) + String.valueOf(y); //      System.out.println(num + ". " + strResult); } } } } 


However, if the result is less than ten, then we get an error.
In the comments correctly noted that the union may not work correctly.
Rewrite our method as follows, add a leading 0 if the second number is less than 10:
 public static int result(int x, int y) {//    int minus_x = 100 - x; int minus_y = 100 - y; int plus = minus_x + minus_y; int hundr_minus_summ = 100 - plus; int begin = hundr_minus_summ; //   int end = minus_x * minus_y; //   String strResult; if (end <10) strResult = String.valueOf(begin) +"0"+ String.valueOf(end); //      else strResult = String.valueOf(begin) + String.valueOf(end); int result = Integer.parseInt(strResult); //    int return result; } 


as a result, we have such a program:

 /* *   */ package denialOfTheSage01; //    ,         /** * * @author Ar20L80 */ public class DenialOfTheSage { public static void main(String[] args) { int countSageIsRight = 0; //    int countSageIsWrong = 0; //    int countAnswer = 0; //    int x, y; //     ,    for (x = 10; x <= 99; x++) { for (y = 10; y <= 99; y++) { countAnswer++; System.out.print(countAnswer + ". "); //   showXY(x, y); //     x = y = if (x * y == result(x, y)) { showSageIsTrue(); //  "" countSageIsRight++; //   ""   } else { showSageIsFalse(); //  "" countSageIsWrong++;//   ""   } } } System.out.println(); //     System.out.println(": "); System.out.println("   " + countSageIsRight + " "); System.out.println("   " + countSageIsWrong + " "); } public static void showXY(int x, int y) { //    x  y System.out.print(" x = " + x + " y = " + y + " "); } public static void showSageIsTrue() { //    "!" System.out.println("!"); } public static void showSageIsFalse() {//    "!" System.out.println("!"); } public static int result(int x, int y) {//    int minus_x = 100 - x; int minus_y = 100 - y; int plus = minus_x + minus_y; int hundr_minus_summ = 100 - plus; int begin = hundr_minus_summ; //   int end = minus_x * minus_y; //   String strResult; if (end <10) strResult = String.valueOf(begin) +"0"+ String.valueOf(end); //      else strResult = String.valueOf(begin) + String.valueOf(end); int result = Integer.parseInt(strResult); //    int return result; } } 

The output of the program:

  run: 1.  x = 10 y = 10 ! 2.  x = 10 y = 11 ! 3.  x = 10 y = 12 ! ... 8099.  x = 99 y = 98 ! 8100.  x = 99 y = 99 ! 


Total:
Sage told the truth 536 times
Sage told a lie 7564 times

You can improve the code yourself. For example, in the result (int x, int y) method, I used a lot of unnecessary variables. Refactoring is the second thing. The main thing is that we got a working program at the initial stage. We saw for which two-digit numbers the proposed algorithm runs correctly. We considered that the number of incorrect answers was 7564, the number of correct answers was 536.
The algorithm still fails. This occurs in cases where the second part of the desired number is greater than 99. Also, you can add the program yourself and consider the transitivity to reduce the output of the program.

In the algorithm proposed by the sage, the left side is calculated by the formula (marked in red):
100 - (100-a + 100 - b) = 100 - 100 + a - 100 + b = a -100 + b = a + b - 100 = begin;

Right side (marked in blue):
(100 - a) * (100 - b) = 10000-100b -100a + ab = end;

However, a simple union does not always give the correct result.

Rewrite our result (int x, int y) method.

 public static int result(int x, int y) {//    int begin = x + y - 100; //   int end = (100 - x) * (100 - y); //   String strResult; if (end < 10) { strResult = String.valueOf(begin).concat("0").concat(String.valueOf(end)); //      } else { strResult = String.valueOf(begin).concat(String.valueOf(end)); //      } int result = Integer.parseInt(strResult); //    int return result; } 


We know that a * b = b * a.
This means that we carry out unnecessary calculations, which are repeated here. Rewrite our cycle:

 for (x = 10; x <= 99; x++) { for (y = x; y <= 99; y++) { ... 


Rewrite the output of the result:

 ... System.out.println("   " + countSageIsRight + "   " + countAnswer); System.out.println("   " + countSageIsWrong + "   " + countAnswer); ... 


As a result, we get the output of the program:

 1.  x = 10 y = 10 ! 2.  x = 10 y = 11 ! 3.  x = 10 y = 12 ! ... 4095.  x = 99 y = 99 ! :    273   4095    3822   4095 


You can also rewrite the program without using the String type to combine the result, but using only an int.

Source: https://habr.com/ru/post/458284/


All Articles