[アルゴリズム] 1から100までの素数を取得(Python編)

2017年3月2日

Python テクノロジー プログラミング 特集

t f B! P L
pythonで素数を取得する事は、さほど難しくないと思うんだが、今回は、phpとの処理時間を比較してみたいと思う。 言語ごとの処理速度は色々なサイトで行っているが、結果はどれも処理内容によって違っているので、あまり参考にならない。 ということは、自分で直接コーディングして比較するしかないでしょう。 他人の一喜一憂に付き合う必要はない。

ソースコード

#coding: UTF-8 def getPrimeNumber(num): flg = 0 for i in range(2 , num-1): if num % i == 0: flg = i break if flg == 0 : return True else : return False prime_numbers = [] for i in range(2,100): if getPrimeNumber(i) == True : prime_numbers.append(i) print prime_numbers

実行

$ # python prime_number.py [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

解説

PythonのBooleanの扱い

pythonでもboolean変数は使えますが、「True」「False」という風にちゃんとキャメルケースで記述すること。 xcodeのnilも違和感あったんだが、それよりは分かりやすいと思う。

PHPとの速度比較

今回は1から1000までの素数という条件で、実行した結果をtimeコマンドで得た値を載せておきます。 $ time python prime_number.py [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] real 0m0.037s user 0m0.020s sys 0m0.000s time php prime_number.php Array ( [0] => 2 [1] => 3 [2] => 5 [3] => 7 [4] => 11 [5] => 13 [6] => 17 [7] => 19 [8] => 23 [9] => 29 [10] => 31 [11] => 37 [12] => 41 [13] => 43 [14] => 47 [15] => 53 [16] => 59 [17] => 61 [18] => 67 [19] => 71 [20] => 73 [21] => 79 [22] => 83 [23] => 89 [24] => 97 [25] => 101 [26] => 103 [27] => 107 [28] => 109 [29] => 113 [30] => 127 [31] => 131 [32] => 137 [33] => 139 [34] => 149 [35] => 151 [36] => 157 [37] => 163 [38] => 167 [39] => 173 [40] => 179 [41] => 181 [42] => 191 [43] => 193 [44] => 197 [45] => 199 [46] => 211 [47] => 223 [48] => 227 [49] => 229 [50] => 233 [51] => 239 [52] => 241 [53] => 251 [54] => 257 [55] => 263 [56] => 269 [57] => 271 [58] => 277 [59] => 281 [60] => 283 [61] => 293 [62] => 307 [63] => 311 [64] => 313 [65] => 317 [66] => 331 [67] => 337 [68] => 347 [69] => 349 [70] => 353 [71] => 359 [72] => 367 [73] => 373 [74] => 379 [75] => 383 [76] => 389 [77] => 397 [78] => 401 [79] => 409 [80] => 419 [81] => 421 [82] => 431 [83] => 433 [84] => 439 [85] => 443 [86] => 449 [87] => 457 [88] => 461 [89] => 463 [90] => 467 [91] => 479 [92] => 487 [93] => 491 [94] => 499 [95] => 503 [96] => 509 [97] => 521 [98] => 523 [99] => 541 [100] => 547 [101] => 557 [102] => 563 [103] => 569 [104] => 571 [105] => 577 [106] => 587 [107] => 593 [108] => 599 [109] => 601 [110] => 607 [111] => 613 [112] => 617 [113] => 619 [114] => 631 [115] => 641 [116] => 643 [117] => 647 [118] => 653 [119] => 659 [120] => 661 [121] => 673 [122] => 677 [123] => 683 [124] => 691 [125] => 701 [126] => 709 [127] => 719 [128] => 727 [129] => 733 [130] => 739 [131] => 743 [132] => 751 [133] => 757 [134] => 761 [135] => 769 [136] => 773 [137] => 787 [138] => 797 [139] => 809 [140] => 811 [141] => 821 [142] => 823 [143] => 827 [144] => 829 [145] => 839 [146] => 853 [147] => 857 [148] => 859 [149] => 863 [150] => 877 [151] => 881 [152] => 883 [153] => 887 [154] => 907 [155] => 911 [156] => 919 [157] => 929 [158] => 937 [159] => 941 [160] => 947 [161] => 953 [162] => 967 [163] => 971 [164] => 977 [165] => 983 [166] => 991 [167] => 997 ) real 0m0.042s user 0m0.000s sys 0m0.020s この程度の数字であれば、一瞬で終わるので、比較にならないかもしれないが、若干Pythonの方が早いようだ。 ほぼ同じ内容のプログラムという事を考えれば、処理速度の比較としては、問題ないと思うので、PHPが遅いという一般的な解釈は間違っていないようだ。

リンク

色々なプログラム言語でアルゴリズム学習

このブログを検索

ブログ アーカイブ

QooQ