1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#! /sw/tools/bin/perl -w
# FIXME: one line description
# Generated automatically from @FROM@.
# remember to add to perl_exe in Makefile.am
use strict;
use Getopt::Long;
(my $VERSION = '$Revision: 1.2 $ ') =~ tr/[0-9].//cd;
(my $program_name = $0) =~ s|.*/||;
my $debug = 0;
my $verbose = 0;
sub usage ($)
{
my ($exit_code) = @_;
my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
if ($exit_code != 0)
{
print $STREAM "Try `$program_name --help' for more information.\n";
}
else
{
# FIXME: add new option descriptions here
print $STREAM <<EOF;
Usage: $program_name [OPTIONS] FIXME: FILE ?
OPTIONS:
--help display this help and exit
--version output version information and exit
--debug output debugging information
--verbose generate verbose output
EOF
}
exit $exit_code;
}
sub END
{
use POSIX qw (_exit);
# This is required if the code might send any output to stdout
# E.g., even --version or --help. So it's best to do it unconditionally.
close STDOUT
or (warn "$program_name: closing standard output: $!\n"), _exit (1);
}
{
GetOptions
(
# FIXME: add new options here
debug => \$debug,
verbose => \$verbose,
help => sub { usage 0 },
version => sub { print "$program_name version $VERSION\n"; exit },
) or usage 1;
# FIXME: provide default here if necessary
# unshift (@ARGV, ".") if !@ARGV;
# print "$#ARGV ; $ARGV[0], $ARGV[1]\n" if $debug;
# foreach my $arg (@ARGV)
# {
# die "$program_name: FIXME - process $arg here"
# }
my $n = $ARGV[0];
my $is_prime =
{
2 => 1,
3 => 1,
5 => 1,
7 => 1,
11 => 1,
};
sub is_prime ($)
{
my ($n0) = @_;
use integer;
$n0 == 2
and return 1;
my $n = $n0;
my $d = 2;
my $w = 1;
my $q;
while (1)
{
$q = $n / $d;
#print "q=$q n=$n d=$d\n";
($n == $q * $d)
and return 0;
$d += $w;
$q < $d
and last;
$w = 2;
}
return 1;
}
my $prod = 2 * 3 * 5;
my $last_prime = 1;
my $i;
for ($i = 2; $i < 2 * $prod; $i++)
{
if (is_prime $i)
{
print $i, ' ', $i - $last_prime, "\n";
$last_prime = $i;
$prod < $i
and last;
}
}
exit 0;
}
|