primesコマンドで素数を求める

整数・自然数・素数

bsdgamesのprimesコマンドで素数を表示することができます。

準備するもの
  • Linux PC(確認環境はDebian 8.3 Jessie)
  • 端末ウィンドウ(ターミナルソフト)
  • bsdgamesパッケージ

インストールはapt-get一発です。

# apt-get install bsdgames

では使ってみます。primesコマンドの引数に範囲を指定するか、

takk@deb83:~$ primes 2 20
2
3
5
7
11
13
17
19
takk@deb83:~$ 

開始する数字を指定すれば、素数が表示されます。
(開始の数字のみの場合はCTRL+Cするまで表示が止まりません)

takk@deb83:~$ primes 1000
1009
1013
1019
1021
1031
1033
1039
1049
1051
1061

(省略)

どんなアルゴリズムを使っているのでしょうか。ソースのコメントを読んでみます。

takk@deb83:~$ apt-get source bsdgames
(省略)
takk@deb83:~$ cd bsdgames-2.17/
takk@deb83:~/bsdgames-2.17$ cd primes
takk@deb83:~/bsdgames-2.17/primes$ 

コメントは各行の2文字目にアスタリスクがあるようです。2番目にアスタリスクがある行をgrepで抽出します。

takk@deb83:~/bsdgames-2.17/primes$ grep '^.\*' primes.c | leafpad
/*	$NetBSD: primes.c,v 1.19 2011/08/30 02:58:04 jakllsch Exp $	*/
/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Landon Curt Noll.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * primes - generate a table of primes between two values
 *
 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
 *
 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
 *
 * usage:
 *	primes [start [stop]]
 *
 *	Print primes >= start and < stop.  If stop is omitted,
 *	the value 4294967295 (2^32-1) is assumed.  If start is
 *	omitted, start is read from standard input.
 *
 * validation check: there are 664579 primes between 0 and 10^7
 */
/*
 * Eratosthenes sieve table
 *
 * We only sieve the odd numbers.  The base of our sieve windows are always
 * odd.  If the base of table is 1, table[i] represents 2*i-1.  After the
 * sieve, table[i] == 1 if and only iff 2*i-1 is prime.
 *
 * We make TABSIZE large to reduce the overhead of inner loop setup.
 */
/*
 * prime[i] is the (i-1)th prime.
 *
 * We are able to sieve 2^32-1 because this byte table yields all primes 
 * up to 65537 and 65537^2 > 2^32-1.
 */
/*
 * To avoid excessive sieves for small factors, we use the table below to 
 * setup our sieve blocks.  Each element represents a odd number starting 
 * with 1.  All non-zero elements are factors of 3, 5, 7, 11 and 13.
 */
/*
 * read_num_buf --
 *	This routine returns a number n, where 0 <= n && n <= BIG.
 */
/*
 * primes - sieve and print primes from start up to and but not including stop
 *
 *	start	where to start generating
 *	stop	don't generate at or above this value
 */

やはりエラトステネスの篩(Eratosthenes sieve)を使っているようですね。

コメント

タイトルとURLをコピーしました