a是偶数?

a % 2 == 0

a能被3整除,但不能被2整除。

a % 3 == 0 && a % 2 != 0

如何判定一个年份是否是闰年?

4的倍数、100的倍数、400的倍数,。。。

 

While summing up a bunch of numbers without knowing the how many numbers in advance, then we will design a special number, usually \(-1\), to denote the end of the numbers.

Summation of a bunch of numbers is very typical problem in computation.

To sum up 10 numbers, we do

Given two integers \(a\) and \(b\), with \(a > b\). If a % b == 0, then \(b\) is the GCD of \(a\) and \(b\).

In Mathematics, GCD has the property:

$$\gcd(a,b)=\gcd(b,a\bmod b)$$

So, a "big" problem(with size \((a,b)\)) can be solved by solving a "smaller" problem(with problem size \((b,a\mod b)\)).

Question

  • What happens behind Google search?

Answer

''今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?''

This puzzle can be translated into the solution of the system of congruences

$$x \equiv 2 \pmod 3\\x \equiv 3 \pmod 5\\x \equiv 2 \pmod 7$$

https://ideone.com/CBrmyi

有条件转移是图灵完备的一个重要的必要条件。

/*****************************************
 *
 *  场景:人机对话
 *      人和机器轮流说一句话,问候、询问,。。。
 *      要说的话事前组织好
 *
 *  人通过嘴说话,转换成文字通过键盘传递给机器,
 *      机器经由键盘通过耳朵接收到人说话的文字,
 *      并保存到机器的大脑记忆中,然后作思考。。。
 *  机器将要说的话以文字形式通过其嘴说到屏幕上,
 *                (也可以转换成语音,通过其嘴说到microphone)
 *      人通过眼睛看到屏幕上的文字,理解其含义。。。
 *        (或通过耳朵听到声音)
 *  现阶段
 *      人到机器的信息传递通道:键盘——机器的耳朵:scanf
 *      机器到人的信息传递通道:屏幕——机器的嘴:printf
 *
 *  如何实现以下对话?
 *      小明:“你好,机器人!”
 *      计算机:“哦哦,你叫什么名字?”
 *      小明:“小明”。
 *      计算机:“你好,小明!”
 *
 *  What if 小明骂机器人?for example: you are an idiot!
 *
 *  What if in such a scenario: 你说10遍“Hello World!”
 *
 * ****************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char greating[20];
    char name[20];

    scanf("%s", greating);
    // 如果友善,不必关心这里的条件是什么,如何实现
// 关注在某个条件成立时,做什么操作,不成立时又做什么操作
if (strstr(greating, "idiot") != NULL) { exit(1); } else if (strstr(greating,"shit") != NULL) { exit(2); } else { printf("What's your nsme?\n"); } scanf("%s", name); printf("你好,%s\n", name); return 0; }

理解自然语言中,如果。。。则。。。否则。。。的含义。

现实中按条件分别做不同的事情,在计算机系统中如何实现?

就是有条件的控制转移,选择结构、分支结构、条件结构,等等名称

在C语言中,就是:

    if (some condition is true)
    {   // here is so-called then clause
        // do something;
    }
    else
    {   // here is so-called else clause
        // do somethine else;
    }

Problem solving consists of using generic or ad hoc methods, in an orderly manner, for finding solutions to problems.

Computational problem

  • Given a positive integer \(n\), find a nontrivial prime factor of \(n\).

With variables, we can calculate any expressions. For example, $$3+5=?\\c=a+b$$

A more complicated example is to solving a quadratic equation with one unknown. 

$$ax^2+bx+c=0$$

where \(a \neq 0\). The formula of roots of above quadric equation is:

$$r_{1,2}=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$