-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisprime.pl
More file actions
35 lines (28 loc) · 777 Bytes
/
isprime.pl
File metadata and controls
35 lines (28 loc) · 777 Bytes
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
#!/usr/bin/perl
use strict;
use warnings;
# Get the number from the user
print "Enter a positive integer: ";
my $n = <STDIN>;
chomp($n);
# Make sure $n is a positive integer
die "Invalid input: $n" unless $n =~ /^[1-9][0-9]*$/;
# Initialize an array to hold the primes we find
my @primes = ();
# Check each number up to $n for primality
for (my $i = 2; $i <= $n; $i++) {
# Assume $i is prime until proven otherwise
my $is_prime = 1;
# Check if $i is divisible by any prime less than it
foreach my $prime (@primes) {
if ($i % $prime == 0) {
$is_prime = 0;
last;
}
}
# If $i is prime, add it to the list of primes
if ($is_prime) {
push @primes, $i;
print "$i is prime\n";
}
}