What is an algorithm

From Zhihu big head sister D

An algorithm is a computational method or step to solve a problem. For example, elementary school multiplication table, ninety-nine eighty-one. For example, mother cooking brain recipes, first stir-fry, then stew, and then small fire to collect juice (I am hungry again).

I wake up in the morning and read The Comic Book Algorithm, and I wonder if there’s a good algorithm for multiplying two-digit numbers.

For example: 91*91, according to the calculation method taught by primary school teachers, as shown in the figure below

I saw a fast method on the Internet, so I could do it in my head

81 times 81 is not going to work out with this algorithm, and that’s a special feature of the algorithm.

51 * 51 52 * 52… 59 times 59, you can use the following formula

Formula: head * head + tail, tail * tail is insufficient to fill 0

25 times 5 times 5 plus 1 is 26, and 1 times 1 is 1 and that’s equal to 2601

Five features of the algorithm

  • Have a poor sexual

    The fineness of an algorithm means that it must be able to terminate after a finite number of steps are performed

  • tangibility

    Each step of the algorithm must be clearly defined

  • Input item

    An algorithm has zero or more inputs to describe the initial conditions of the operation object. The so-called zero inputs mean that the algorithm itself determines the initial conditions

  • Output item

    An algorithm has one or more outputs that reflect the results of processing the input data. An algorithm without output is meaningless

  • The feasibility of

    Any computational step performed in an algorithm can be broken down into basic executable steps, that is, each computational step can be completed in a finite amount of time (also called validity)

Two main measures of the algorithm

  • Time complexity

    The length of the running time, for example to figure out the largest number from a bunch of numbers, my algorithm takes 5 seconds, and your algorithm takes 1 second, so that’s probably better for you

  • Spatial complexity

    The size of the footprint, for example, my code runs on 5MB of memory and your code runs on 100MB.

    So it depends on the situation, if the user has a lot of memory, if the user cares more about how long the code takes, if you waste a little bit of memory, then your algorithm is better, if the user has a lot of memory and doesn’t care about how long the code takes, then my algorithm is better. Now the memory of the machine is no longer the bottleneck, more important is the speed of the algorithm.

Practical application of the algorithm

  • Douyin recommendation algorithm
  • Baidu’s search engine
  • Amap optimal route