2019年1月1日火曜日

開発環境

2019年の素数日 – はむかず!Prime Number DaysのSymPy(🐍Python)版のコードを描いてみた。(ついでにさらに10年先の分の素数の日も求めてみた。)

コード(Emacs)

Python 3

#!/usr/bin/env python3
import datetime
from sympy import isprime


def get_prime_number_days(year: int = 2019) -> list:
    '''
    >>> get_prime_number_days()
    [20190221, 20190227, 20190301, 20190319, 20190323, 20190421, 20190523, 20190529, 20190601, 20190613, 20190719, 20190811, 20190823, 20190913, 20191009, 20191027, 20191109, 20191117, 20191231]
    '''
    one_day = datetime.timedelta(days=1)
    a = datetime.date(year, 1, 1)
    b = datetime.date(year, 12, 31)
    days = []
    while a <= b:
        day = int(a.isoformat().replace('-', ''))
        if isprime(day):
            days.append(day)
        a += one_day
    return days


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    # try:
    #     year = input('Enter a year: ')
    #     year = int(year)
    #     for day in get_prime_number_days(year):
    #         print(day)
    # except ValueError as err:
    #     print(err)
    # except Exception as err:
    #     print(err)

    print(len(get_prime_number_days(2019)))
    for year in range(2020, 2030):
        print(f'Prime days of the year {year}:')
        for i, day in enumerate(get_prime_number_days(year), 1):
            print(f'{i:0>2}: {day}')

入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))

$ ./prime_number_days.py
Trying:
    get_prime_number_days()
Expecting:
    [20190221, 20190227, 20190301, 20190319, 20190323, 20190421, 20190523, 20190529, 20190601, 20190613, 20190719, 20190811, 20190823, 20190913, 20191009, 20191027, 20191109, 20191117, 20191231]
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.get_prime_number_days
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
19
Prime days of the year 2020:
01: 20200109
02: 20200111
03: 20200121
04: 20200123
05: 20200223
06: 20200309
07: 20200429
08: 20200511
09: 20200529
10: 20200613
11: 20200619
12: 20200703
13: 20200711
14: 20200721
15: 20200723
16: 20200729
17: 20200801
18: 20200813
19: 20200903
20: 20201021
21: 20201029
22: 20201101
23: 20201113
24: 20201227
25: 20201231
Prime days of the year 2021:
01: 20210101
02: 20210107
03: 20210131
04: 20210317
05: 20210401
06: 20210503
07: 20210513
08: 20210609
09: 20210717
10: 20210803
11: 20210831
12: 20210923
13: 20210929
14: 20211001
15: 20211007
16: 20211031
17: 20211109
18: 20211127
19: 20211203
20: 20211209
21: 20211221
Prime days of the year 2022:
01: 20220103
02: 20220119
03: 20220121
04: 20220127
05: 20220217
06: 20220307
07: 20220311
08: 20220323
09: 20220331
10: 20220407
11: 20220517
12: 20220601
13: 20220619
14: 20220713
15: 20220817
16: 20220821
17: 20220901
18: 20220923
19: 20221009
20: 20221027
21: 20221127
22: 20221213
23: 20221231
Prime days of the year 2023:
01: 20230103
02: 20230109
03: 20230121
04: 20230201
05: 20230219
06: 20230303
07: 20230411
08: 20230517
09: 20230519
10: 20230619
11: 20230621
12: 20230831
13: 20230919
14: 20231011
15: 20231017
16: 20231023
17: 20231129
18: 20231203
Prime days of the year 2024:
01: 20240107
02: 20240219
03: 20240323
04: 20240327
05: 20240411
06: 20240419
07: 20240531
08: 20240603
09: 20240611
10: 20240723
11: 20240729
12: 20240807
13: 20240819
14: 20240821
15: 20240903
16: 20241017
17: 20241029
18: 20241119
19: 20241121
20: 20241211
21: 20241229
Prime days of the year 2025:
01: 20250101
02: 20250121
03: 20250203
04: 20250227
05: 20250317
06: 20250401
07: 20250413
08: 20250421
09: 20250509
10: 20250701
11: 20250707
12: 20250809
13: 20250821
14: 20250907
15: 20251013
16: 20251027
17: 20251103
18: 20251123
19: 20251129
20: 20251211
21: 20251223
22: 20251229
Prime days of the year 2026:
01: 20260117
02: 20260211
03: 20260301
04: 20260319
05: 20260511
06: 20260523
07: 20260529
08: 20260609
09: 20260621
10: 20260627
11: 20260703
12: 20260729
13: 20260813
14: 20260829
15: 20260909
16: 20260913
17: 20260921
18: 20261009
19: 20261023
20: 20261107
21: 20261113
22: 20261119
23: 20261123
24: 20261231
Prime days of the year 2027:
01: 20270119
02: 20270123
03: 20270203
04: 20270303
05: 20270323
06: 20270417
07: 20270513
08: 20270527
09: 20270617
10: 20270629
11: 20270707
12: 20270717
13: 20270821
14: 20270827
15: 20270909
16: 20270911
17: 20271101
18: 20271107
19: 20271109
Prime days of the year 2028:
01: 20280101
02: 20280103
03: 20280109
04: 20280119
05: 20280217
06: 20280229
07: 20280301
08: 20280307
09: 20280311
10: 20280401
11: 20280413
12: 20280419
13: 20280509
14: 20280523
15: 20280529
16: 20280607
17: 20280707
18: 20280713
19: 20280721
20: 20280811
21: 20280817
22: 20280919
23: 20280929
24: 20281003
25: 20281007
26: 20281013
27: 20281123
28: 20281211
29: 20281229
Prime days of the year 2029:
01: 20290211
02: 20290217
03: 20290223
04: 20290307
05: 20290327
06: 20290331
07: 20290421
08: 20290427
09: 20290519
10: 20290607
11: 20290609
12: 20290801
13: 20290819
14: 20290901
15: 20290909
16: 20291119
17: 20291123
18: 20291231
$

最初の4桁が違うだけで、各年の素数になる日数には結構ばらつきがあるみたい。

0 コメント:

コメントを投稿

関連コンテンツ