V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
XiiLii
V2EX  ›  Python

用程序生成“九九乘法表”

  •  
  •   XiiLii · 2018-11-07 21:01:12 +08:00 · 1935 次点击
    这是一个创建于 2216 天前的主题,其中的信息可能已经有所发展或是发生改变。

    九九表至少存在了三千多年,中国早在春秋战国时代就开始使用“九九口诀”在筹算中运算。在《荀子》、《管子》、《淮南子》、《战国策》等书中就能找到“三九二十七”、“六八四十八”、“四八三十二”、“六六三十六”等句子。由此可见,早在“春秋”、“战国”的时候,《九九乘法歌诀》就已经开始流行了。后来东传入高丽、日本,经过丝绸之路西传印度、波斯,继而流行全世界,到明代则改良并用在算盘上。十进位制和九九表是古代中国对世界文化的一项重要的贡献。现在,九九表也是小学算术的基本功

    A × B:A 是被乘数,B 是乘数

    以下是用 Python 程序生成“九九乘法表”的小例子

    for b in range(1, 10):    # 乘数;同一行被乘数不变
      for a in range(1, 10):  # 被乘数;同一行乘数逐渐递增,直到与被乘数相等
        if a <= b:            # 被乘数不能大于乘数(只是针对这个乘法表)
          print(f'{a} X {b} = {a * b}'.ljust(11), end='\t')
      print()                 # 换行
    

    9X9 乘法表

    以上生成了 45 项积

    古玛雅人用 20 进位制,跟现代世界通用的十进位制最接近。一个乘法表有 190 项,比九九表的 45 项虽然大三倍多,但比巴比伦方法还是简便得多。可是考古学家至今还没有发现任何玛雅乘法表。

    以下代码生成了 20 进制的乘法表(只是将 10 改为 20,没有进行进制转换)

    >>> for b in range(1, 20):    # 乘数;同一行被乘数不变
    ...   for a in range(1, 20):  # 被乘数;同一行乘数逐渐递增,直到与被乘数相等
    ...     if a <= b:            # 被乘数不能大于乘数(只是针对这个乘法表)
    ...       print(f'{a} X {b} = {a * b}', end='\t')
    ...   print()                 # 换行
    ...
    1 X 1 = 1
    1 X 2 = 2       2 X 2 = 4
    1 X 3 = 3       2 X 3 = 6       3 X 3 = 9
    1 X 4 = 4       2 X 4 = 8       3 X 4 = 12      4 X 4 = 16
    1 X 5 = 5       2 X 5 = 10      3 X 5 = 15      4 X 5 = 20      5 X 5 = 25
    1 X 6 = 6       2 X 6 = 12      3 X 6 = 18      4 X 6 = 24      5 X 6 = 30      6 X 6 = 36
    1 X 7 = 7       2 X 7 = 14      3 X 7 = 21      4 X 7 = 28      5 X 7 = 35      6 X 7 = 42      7 X 7 = 49
    1 X 8 = 8       2 X 8 = 16      3 X 8 = 24      4 X 8 = 32      5 X 8 = 40      6 X 8 = 48      7 X 8 = 56      8 X 8 = 64
    1 X 9 = 9       2 X 9 = 18      3 X 9 = 27      4 X 9 = 36      5 X 9 = 45      6 X 9 = 54      7 X 9 = 63      8 X 9 = 72      9 X 9 = 81
    1 X 10 = 10     2 X 10 = 20     3 X 10 = 30     4 X 10 = 40     5 X 10 = 50     6 X 10 = 60     7 X 10 = 70     8 X 10 = 80     9 X 10 = 90     10 X 10 = 100
    1 X 11 = 11     2 X 11 = 22     3 X 11 = 33     4 X 11 = 44     5 X 11 = 55     6 X 11 = 66     7 X 11 = 77     8 X 11 = 88     9 X 11 = 99     10 X 11 = 110   11 X 11 = 121
    1 X 12 = 12     2 X 12 = 24     3 X 12 = 36     4 X 12 = 48     5 X 12 = 60     6 X 12 = 72     7 X 12 = 84     8 X 12 = 96     9 X 12 = 108    10 X 12 = 120   11 X 12 = 132   12 X 12 = 144
    1 X 13 = 13     2 X 13 = 26     3 X 13 = 39     4 X 13 = 52     5 X 13 = 65     6 X 13 = 78     7 X 13 = 91     8 X 13 = 104    9 X 13 = 117    10 X 13 = 130   11 X 13 = 143   12 X 13 = 156   13 X 13 = 169
    1 X 14 = 14     2 X 14 = 28     3 X 14 = 42     4 X 14 = 56     5 X 14 = 70     6 X 14 = 84     7 X 14 = 98     8 X 14 = 112    9 X 14 = 126    10 X 14 = 140   11 X 14 = 154   12 X 14 = 168   13 X 14 = 182   14 X 14 = 196
    1 X 15 = 15     2 X 15 = 30     3 X 15 = 45     4 X 15 = 60     5 X 15 = 75     6 X 15 = 90     7 X 15 = 105    8 X 15 = 120    9 X 15 = 135    10 X 15 = 150   11 X 15 = 165   12 X 15 = 180   13 X 15 = 195   14 X 15 = 210   15 X 15 = 225
    1 X 16 = 16     2 X 16 = 32     3 X 16 = 48     4 X 16 = 64     5 X 16 = 80     6 X 16 = 96     7 X 16 = 112    8 X 16 = 128    9 X 16 = 144    10 X 16 = 160   11 X 16 = 176   12 X 16 = 192   13 X 16 = 208   14 X 16 = 224   15 X 16 = 240   16 X 16 = 256
    1 X 17 = 17     2 X 17 = 34     3 X 17 = 51     4 X 17 = 68     5 X 17 = 85     6 X 17 = 102    7 X 17 = 119    8 X 17 = 136    9 X 17 = 153    10 X 17 = 170   11 X 17 = 187   12 X 17 = 204   13 X 17 = 221   14 X 17 = 238   15 X 17 = 255   16 X 17 = 272   17 X 17 = 289
    1 X 18 = 18     2 X 18 = 36     3 X 18 = 54     4 X 18 = 72     5 X 18 = 90     6 X 18 = 108    7 X 18 = 126    8 X 18 = 144    9 X 18 = 162    10 X 18 = 180   11 X 18 = 198   12 X 18 = 216   13 X 18 = 234   14 X 18 = 252   15 X 18 = 270   16 X 18 = 288   17 X 18 = 306   18 X 18 = 324
    1 X 19 = 19     2 X 19 = 38     3 X 19 = 57     4 X 19 = 76     5 X 19 = 95     6 X 19 = 114    7 X 19 = 133    8 X 19 = 152    9 X 19 = 171    10 X 19 = 190   11 X 19 = 209   12 X 19 = 228   13 X 19 = 247   14 X 19 = 266   15 X 19 = 285   16 X 19 = 304   17 X 19 = 323   18 X 19 = 342   19 X 19 = 361
    

    以下代码生成的是进行进制转换的 20 进制 JJ 乘法表

    >>> from numpy import base_repr
    >>> for b in range(1, 20):    # 乘数;同一行被乘数不变
    ...   for a in range(1, 20):  # 被乘数;同一行乘数逐渐递增,直到与被乘数相等
    ...     if a <= b:            # 被乘数不能大于乘数(只是针对这个乘法表)
    ...       print(f'{base_repr(a, 20)} X {base_repr(b, 20)} = {base_repr(a * b, 20)}', end='\t')
    ...   print()                 # 换行
    ...
    1 X 1 = 1
    1 X 2 = 2       2 X 2 = 4
    1 X 3 = 3       2 X 3 = 6       3 X 3 = 9
    1 X 4 = 4       2 X 4 = 8       3 X 4 = C       4 X 4 = G
    1 X 5 = 5       2 X 5 = A       3 X 5 = F       4 X 5 = 10      5 X 5 = 15
    1 X 6 = 6       2 X 6 = C       3 X 6 = I       4 X 6 = 14      5 X 6 = 1A      6 X 6 = 1G
    1 X 7 = 7       2 X 7 = E       3 X 7 = 11      4 X 7 = 18      5 X 7 = 1F      6 X 7 = 22      7 X 7 = 29
    1 X 8 = 8       2 X 8 = G       3 X 8 = 14      4 X 8 = 1C      5 X 8 = 20      6 X 8 = 28      7 X 8 = 2G      8 X 8 = 34
    1 X 9 = 9       2 X 9 = I       3 X 9 = 17      4 X 9 = 1G      5 X 9 = 25      6 X 9 = 2E      7 X 9 = 33      8 X 9 = 3C      9 X 9 = 41
    1 X A = A       2 X A = 10      3 X A = 1A      4 X A = 20      5 X A = 2A      6 X A = 30      7 X A = 3A      8 X A = 40      9 X A = 4A      A X A = 50
    1 X B = B       2 X B = 12      3 X B = 1D      4 X B = 24      5 X B = 2F      6 X B = 36      7 X B = 3H      8 X B = 48      9 X B = 4J      A X B = 5A      B X B = 61
    1 X C = C       2 X C = 14      3 X C = 1G      4 X C = 28      5 X C = 30      6 X C = 3C      7 X C = 44      8 X C = 4G      9 X C = 58      A X C = 60      B X C = 6C     C X C = 74
    1 X D = D       2 X D = 16      3 X D = 1J      4 X D = 2C      5 X D = 35      6 X D = 3I      7 X D = 4B      8 X D = 54      9 X D = 5H      A X D = 6A      B X D = 73     C X D = 7G      D X D = 89
    1 X E = E       2 X E = 18      3 X E = 22      4 X E = 2G      5 X E = 3A      6 X E = 44      7 X E = 4I      8 X E = 5C      9 X E = 66      A X E = 70      B X E = 7E     C X E = 88      D X E = 92      E X E = 9G
    1 X F = F       2 X F = 1A      3 X F = 25      4 X F = 30      5 X F = 3F      6 X F = 4A      7 X F = 55      8 X F = 60      9 X F = 6F      A X F = 7A      B X F = 85     C X F = 90      D X F = 9F      E X F = AA      F X F = B5
    1 X G = G       2 X G = 1C      3 X G = 28      4 X G = 34      5 X G = 40      6 X G = 4G      7 X G = 5C      8 X G = 68      9 X G = 74      A X G = 80      B X G = 8G     C X G = 9C      D X G = A8      E X G = B4      F X G = C0      G X G = CG
    1 X H = H       2 X H = 1E      3 X H = 2B      4 X H = 38      5 X H = 45      6 X H = 52      7 X H = 5J      8 X H = 6G      9 X H = 7D      A X H = 8A      B X H = 97     C X H = A4      D X H = B1      E X H = BI      F X H = CF      G X H = DC      H X H = E9
    1 X I = I       2 X I = 1G      3 X I = 2E      4 X I = 3C      5 X I = 4A      6 X I = 58      7 X I = 66      8 X I = 74      9 X I = 82      A X I = 90      B X I = 9I     C X I = AG      D X I = BE      E X I = CC      F X I = DA      G X I = E8      H X I = F6  I X I = G4
    1 X J = J       2 X J = 1I      3 X J = 2H      4 X J = 3G      5 X J = 4F      6 X J = 5E      7 X J = 6D      8 X J = 7C      9 X J = 8B      A X J = 9A      B X J = A9     C X J = B8      D X J = C7      E X J = D6      F X J = E5      G X J = F4      H X J = G3  I X J = H2  J X J = I1
    

    巴比伦算术有进位制,比希腊等几个国家有很大的进步。不过巴比伦算术采用 60 进位制,原则上一个“ 59×59 ”乘法表需要 1770 项;由于“ 59×59 ”乘法表太庞大,巴比伦人从来不用类似于九九表的“乘法表”。考古学家也从来没有发现类似于九九表的“ 59×59 ”乘法表。不过,考古学家发现巴比伦人用独特的是“平方表”:1×1=1,2×2=4,3×3=9,...,59×59=3481,巴比伦人则依靠他们最擅长的代数学来计算两个数ab的乘积: 例如:

    现代的九九表只需 45 项,玛雅乘法表需 190 项,巴比伦乘法表须 1770 项,埃及、希腊、罗马、印度等国的乘法表须无穷多项;

    参考资料:

    阅读更多

    4 条回复    2018-11-12 08:53:22 +08:00
    junjieyuanxiling
        1
    junjieyuanxiling  
       2018-11-07 21:52:37 +08:00 via Android
    个人博客?
    CEBBCAT
        2
    CEBBCAT  
       2018-11-07 23:56:25 +08:00
    楼主你再这样可能招致不少人包括我的屏蔽……
    xpresslink
        3
    xpresslink  
       2018-11-08 17:46:08 +08:00
    不要浪费论坛资源。
    glaucus
        4
    glaucus  
       2018-11-12 08:53:22 +08:00
    ?你在干嘛?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   924 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:29 · PVG 04:29 · LAX 12:29 · JFK 15:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.