This article is a note and summary of the
李戈
teacher's计算概论与程序设计基础
course in the School of Information Technology of Peking University . The source course address
中国大学MOOC
The course is really good, a sentence好的大学 没有围墙
that makes people feel a little moving, hahaha.
PS: I am really not Toha
Calculation origin
Three mathematics crises came 哥德尔不完备性定理
, 可计算
and 不可计算
the boundary problem with and was created . As for the discussion of the boundary issue, it has triggered the 图灵机
advent.
The first mathematical crisis (the discovery of irrational numbers)
The story begins in 500 BC
Pythagorean School
faith:
- Numbers are the origin of all things, and the nature of things is determined by a certain quantitative relationship. All things constitute a harmonious order according to a certain quantitative ratio.
- All numbers can be expressed as integers or ratios of integers
Later, Pythagoras also proved the Pythagorean theorem, but at the same time it was discovered 『某些直角三角形的三边比不能用整数来表达』
that Pythagoras did not publish it for various reasons. Later, Hippassos also discovered it and published it.
Hippassos Paradox
Hippassos considered a question: What is the diagonal length of a square with side length 1?
- At that time, there was no concept of irrational numbers, and the diagonal "√2" could not be expressed by the ratio of two integers.
- When the other disciples of Pythagoras learned about it, in order to maintain the legitimacy of the sect, they killed Hippassos and threw it into the sea.
The mathematical crisis began.
Crisis relief
Two hundred years later, Eudoxus used geometric methods to establish a complete set of ratio theory, avoiding the problem of irrational numbers.
Crisis resolution
In the second half of the 19th century, after the establishment of real number theory, the nature of irrational numbers was thoroughly understood.
The establishment of the legal status of irrational numbers in mathematics has completely and satisfactorily resolved the first mathematical crisis.
The Second Mathematical Crisis (Calculus)
In the seventeenth century, Newton and Leibniz independently discovered calculus, but their theories were based on infinitesimal analysis.
Berkeley Paradox
Infinitesimal in Newton’s theory『一会儿是零,一会儿又不是零』
From the derivation of calculus, we can see that △x is not zero when used as the denominator, but it is equal to zero in the final formula. The result of this contradiction is disastrous, and mathematicians cannot find it for a long time. To the solution.
Crisis resolution
More than 100 years after the invention of calculus, French mathematicians 柯西
used the limit to define infinitesimals, which completely solved this problem.
The third mathematics crisis (set theory)
Mathematicians always have a dream, trying to establish some basic axioms, and then use strict mathematical logic to derive and prove all theorems of mathematics.
After Cantor invented set theory, Poincaré, a French scientist, believed that: we can use set theory to build an entire mathematics building.
Russell Paradox
S is composed of a collection of all elements that are not itself. So does S belong to S?
Russell’s paradox is popularly described as: In a certain city, a well-known barber said: “I will shave all the people in this city who don’t shave themselves, and I will only shave these people.” Then may I ask for a haircut. Who should shave the teacher's own face?
Crisis resolution
The mathematics building that mathematicians worked so hard to build finally found that the foundation was flawed, and mathematicians put forward their own solutions; it was not until 1908 that the establishment of the first axiomatic set theory system made up for the flaws of set theory.
Although the three mathematical crises have been solved, the impact on the history of mathematics is very profound. Mathematicians try to establish a strict mathematical system, but no matter how careful, there will be defects.
Gödel's incompleteness theorem
For any mathematical system, as long as it is derived from finite axioms and basic concepts, and the natural number system can be derived from it, a proposition can be found in it. We can neither prove nor disprove it.
Theorem
Any formal system containing first-order predicate logic and elementary number theory has a proposition, which can neither be proved to be true nor can it be proved to be false in this system.
The second theorem
If the system S contains elementary number theory, when S has no contradiction, its non-contradiction cannot be proved in S.
Gödel’s incompleteness theorem declares that the desire to thoroughly formalize mathematics is impossible to achieve
Computable boundary problem
Although Gödel’s incompleteness theorem declares that the desire to formalize mathematics is impossible. But we still want to clarify which problems can be proved (
计算
) and which cannot. This is可计算的边界问题
The mathematician gave a research idea: establish a mathematical model for calculation, called a calculation model, and then prove that all tasks that this calculation model can complete are tasks that can be calculated.
Turing proposed a model for this problem, this model is图灵机
Turing and Turing Machine
Speaking of Turing machines, we must first talk about people who are worth remembering in the history of computers
Alan Turing
Turing
Take a look at their resume:
- Born in London in June 1912
- During middle school, won the King Edward VI Golden Shield Medal in Mathematics
- In 1935, he was elected a Fellow of King's College, Cambridge University
- In May 1936, Turing proposed the Turing machine (published in the "London Mathematical Society Collection")
- In 1938, Princeton University received a Ph.D.
- During World War II from 1938 to 1945, he worked on code breaking (formerly the general counsel of the British and American code breaking departments)
- In 1946, he was awarded the Order of the British Empire
- In 1950, the famous "Turing Test" was proposed
- In October 1950, published the paper "Can machines think?" (opened research on artificial intelligence)
- In 1951, he was elected as a member of the Royal Society (the fourth member of the Royal Society in the family)
- In 1952, Turing wrote a chess program
- 1954, died
In 1936, Turing proposed an ideal mathematical model of computing machine-Turing Machine in his famous paper "On the Application of Computable Numbers in Decision Problems".
Turing Machine
The composition of Turing machine
This is a somewhat blurred but very clear picture:
A storage tape
- Two-way unlimited extension
- There are small squares
- Each small square can store a number/letter
A controller
- Can store the current state of itself;
- Contains a read-write head, which can read, write and change the numbers/letters of each cell on the storage tape
- It can change its state according to the letters/numbers read
- Can be along the storage belt
Turing machine working steps
- ready:
- Symbol initialization on storage tape;
- The controller sets its current state;
- The read-write head is placed in the starting position;
- Prepare work procedures;
- Repeat the following tasks until the shutdown:
- The read-write head reads the letters/numbers in the current grid on the storage tape;
- Find the corresponding program statement according to its current state and the characters read;
- According to the corresponding program statement, do three actions:
- Write a corresponding letter/number on the grid of the current storage tape;
- Change its state to a new state;
- The read-write head moves one step to the left or right;
The Theoretical Significance of Turing Machine
-
Judgment of computability:
Given the symbol sequence A, if a Turing machine can be found and the corresponding symbol sequence B can be obtained, then the sequence from A to B is computable.
-
Gives an achievable general calculation model;
-
Introduced the idea of computing through "read and write symbols" and "state changes";
-
Proved the ability to complete complex calculations based on simple alphabets;
-
Introduced prototypes of concepts such as storage areas, programs, and controllers;
Why can a computer calculate?
We all know how the computer is
数
used二进制
to represent it, so how does the computer complete the calculation?
Let's prepare a knowledge point first布尔代数
Boolean algebra
In 1854, he G Boole
published "Research on the Laws of Thinking-Mathematical Theoretical Foundations of Logic and Probability", and combined his other article "Mathematical Analysis of Logic" to create a brand-new subject- 布尔代数
. It provides an important mathematical method and theoretical basis for the design of computer switching circuits.
Logical operation method
Logical and
Circuit diagram:
Function expression:F = A * B
Truth table:
A | B | =F |
---|---|---|
0 | 0 | =0 |
0 | 1 | =0 |
1 | 0 | =0 |
1 | 1 | =1 |
Logical OR
Circuit diagram:
Function expression:F = A + B
Truth table:
A | B | =F |
---|---|---|
0 | 0 | =0 |
0 | 1 | =1 |
1 | 0 | =1 |
1 | 1 | =1 |
Logical negation
Circuit diagram:
Truth table:
A | =F |
---|---|
0 | =1 |
1 | =0 |
Same or
- Two numbers are the same as "1"
- The difference between two numbers is "0"
XOR
- Both numbers are the same as "0"
- The difference between two numbers is "1"
Calculation
Conventional calculation method
Example: A=
0b
1101, B=0b
1001, find A+B.
According to the conventional calculation method, it is like this:
Computer calculation
First look at the lower half adder
Through the combination of multiple half adders, we can get a full adder
Ask again: Why can a computer calculate?
Knowing this, we can answer:
- First of all, the operations that can be converted
二进制数
by digital operations; 二进制运算
Can be converted to basic布尔运算
;- The basics
布尔运算
can be电路
done by ; - The computer is composed of these basic circuits
So the computer has computing power.
More professional descriptionALU
We described above 布尔运算
and the 计算方式
two parts that make up a computer ALU
two core elements:
逻辑单元
: Perform logical operations, such as: AND, OR, NOT, etc.算术单元
: Responsible for all digital operations in the computer
In this case, use code to simulate a full adder!
Adder implementation
Look at the next schematic diagram first:
Code example:
public class ALU {
/**
* 半加器
*
* @param a 待加数
* @param b 待加数
* @return 半加后的本位和进位
*/
public int[] halfAdder(int a, int b) {
int carry;
int sum;
sum = XOR(a, b);
carry = AND(a, b);
return new int[]{
sum, carry};
}
/**
* 全加器
*
* @param a 待加数
* @param b 待加数
* @param c 待进位数
* @return 全加后的本位和进位
*/
public int[] fullAdder(int a, int b, int c) {
int carry;
int sum;
int[] first = halfAdder(a, b);
int[] second = halfAdder(first[0], c);
sum = second[0];
carry = OR(second[1], first[1]);
return new int[]{
sum, carry};
}
/**
* 异或运算
*/
public int XOR(int a, int b) {
return a ^ b;
}
/**
* 与运算
*/
public int AND(int a, int b) {
return a & b;
}
/**
* 或运算
*/
public int OR(int a, int b) {
return a | b;
}
/**
* 8 位加法器,一步一个脚印版本
*
* @param binaryA 二进制数组
* @param binaryB 二进制数组
* @return 二进制
*/
public int[] FullAdder_8_Bit(int[] binaryA, int[] binaryB) {
int[] out = new int[8];
int[] first = halfAdder(binaryA[7], binaryB[7]);
out[7] = first[0];
int[] second = fullAdder(binaryA[6], binaryB[6], first[1]);
out[6] = second[0];
int[] three = fullAdder(binaryA[5], binaryB[5], second[1]);
out[5] = three[0];
int[] four = fullAdder(binaryA[4], binaryB[4], three[1]);
out[4] = four[0];
int[] five = fullAdder(binaryA[3], binaryB[3], four[1]);
out[3] = five[0];
int[] six = fullAdder(binaryA[2], binaryB[2], five[1]);
out[2] = six[0];
int[] seven = fullAdder(binaryA[1], binaryB[1], six[1]);
out[1] = seven[0];
out[0] = seven[1];
System.out.println(Arrays.toString(out));
return out;
}
/**
* 优化版本,自定义加法器长度
*
* @param binaryA 待加数组
* @param binaryB 待加数组
* @param bitLen 运算长度
* @return 计算后的数组长度
*/
public int[] FullAdderBits(int[] binaryA, int[] binaryB, int bitLen) {
int[] out = new int[bitLen];
int[] tmpRes;
int[] first = halfAdder(binaryA[7], binaryB[7]);
out[bitLen - 1] = first[0];
tmpRes = first;
for (int i = bitLen - 1; i > 0; i--) {
tmpRes = fullAdder(binaryA[i], binaryB[i], tmpRes[1]);
out[i] = tmpRes[0];
}
out[0] = tmpRes[1];
System.out.println(Arrays.toString(out));
return out;
}
public static void main(String[] args) {
ALU alu = new ALU();
// {高位-----------低位}
int[] a = new int[]{
0, 1, 1, 1, 0, 0, 1, 0};//114
int[] b = new int[]{
0, 0, 1, 1, 0, 0, 1, 1};//51
alu.FullAdder_8_Bit(a, b);
alu.FullAdderBits(a, b, 8);
}
}
Let's look at the test output:
[1, 0, 1, 0, 0, 1, 0, 1] //165
[1, 0, 1, 0, 0, 1, 0, 1] //165
There are wood, with wood there, there is no code +
, , —
, *
(please/
ignore the for loop Ha!), Is fully implemented by logic operations.