--- title: "如何判別是否為 1~13 的倍數" date: 2020-08-10 modified: 2020-08-10 disqus: cynthiahackmd categories: - "先備知識" tags: - "算術數學" - "Python" --- {%hackmd @CynthiaChuang/Github-Page-Theme %} <br> 那天在社團看到有人在詢問倍數的判斷的寫法,在家監工閒著也是閒著,就按這篇[判別法](https://leestar2013.pixnet.net/blog/post/45638266)把剩下的判斷式也補齊了。 雖然我覺得我在實際專案中應該不會這麼判斷 XD <!--more--> <p class="illustration"> <img src="https://i.imgur.com/6urZjMj.jpg" alt="Math"> (圖片來源: <a href="https://www.pexels.com/zh-tw/photo/3729557/">Ian Panelo | Pexels</a>) </p> ## 1的倍數 1 的倍數:任何數皆為 1 的倍數。 ```python= def is_multiple_of_1(number): return True ``` ## 2的倍數 2 的倍數:個位數字為偶數(含0)。 ```python= def is_multiple_of_2(number): sd = int(number[-1]) return sd % 2 == 0 ``` ## 3的倍數 3 的倍數:各個數字和為 3 的倍數。 ```python= def is_multiple_of_3(number): s = sum([int(n) for n in number[::]]) return s % 3 == 0 ``` ## 4的倍數 4 的倍數:末二位數為 4 的倍數。 ```python= def is_multiple_of_4(number): s = int(number[-2:]) return s % 4 == 0 ``` ## 5的倍數 5 的倍數:個位數字為 5 或 0。 ```python= def is_multiple_of_5(number): s = int(number[-1]) return s==0 or s==5 ``` ## 6的倍數 6 的倍數:各個數字和為 6 的倍數(同時是2和3的倍數)。 ```python= def is_multiple_of_6(number): return is_multiple_of_2(number) and is_multiple_of_3(number) ``` ## 7的倍數 7 的倍數:由個數起每三位數字一節,各奇數節的和與偶數節的和相減,其差是 7 的倍數。 ```python= def is_multiple_of_7(number): group = [number[max(i-3,0):i] for i in range(len(number), 0, -3)] odd_group = sum([int(n) for n in group[::2]]) even_group = sum([int(n) for n in group[1::2]]) df = odd_group - even_group return df % 7 == 0 ``` ## 8的倍數 8 的倍數:末三位數為 8 的倍數。 ```python= def is_multiple_of_8(number): s = int(number[-3:]) return s % 8 == 0 ``` ## 9的倍數 9 的倍數:各個數字和為 9 的倍數。 ```python= def is_multiple_of_9(number): s = sum([int(n) for n in number[::]]) return s % 9 == 0 ``` ## 10的倍數 10 的倍數:個位數字為 0。 ```python= def is_multiple_of_10(number): sd = int(number[-1]) return sd == 0 ``` ## 11的倍數 11 的倍數:奇數位數字和與偶數位數字和相差為 11 的倍數。 ```python= def is_multiple_of_11(number): odd_idx = sum([int(n) for n in number[::2]]) even_idx = sum([int(n) for n in number[1::2]]) d = odd_idx-even_idx return d % 11 == 0 ``` ## 12的倍數 12 的倍數:同時是 3 和 4 的倍數。 ```python= def is_multiple_of_12(number): return is_multiple_of_3(number) and is_multiple_of_4(number) ``` ## 13的倍數 13 的倍數:由個數起每三位數字一節,各奇數節的和與偶數節的和相減,其差是 13 的倍數。 ```python= def is_multiple_of_13(number): group = [number[ max(i-3,0):i] for i in range(len(number), 0, -3)] odd_group = sum([int(n) for n in group[::2]]) even_group = sum([int(n) for n in group[1::2]]) df = odd_group - even_group return df % 13 == 0 ``` ## 參考資料 1. 李老師 (2012-09-17)。[[數學]1~13的倍數判別法](https://leestar2013.pixnet.net/blog/post/45638266) 。檢自 痞客邦 (2020-07-02)。 ## 更新紀錄 :::spoiler 最後更新日期:2020-08-10 - 2020-08-10 發布 - 2020-07-09 完稿 - 2020-07-02 起稿 ::: <br><br> > **本文作者**: 辛西亞.Cynthia > **本文連結**: [辛西亞的技能樹](https://cynthiachuang.github.io/How-to-Determine-Whether-It-Is-a-Multiple-of-1-to-13) / [hackmd 版本](https://hackmd.io/@CynthiaChuang/How-to-Determine-Whether-It-Is-a-Multiple-of-1-to-13) > **版權聲明**: 部落格中所有文章,均採用 [姓名標示-非商業性-相同方式分享 4.0 國際](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en) (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up