in reply to is it prime?
ofcourse there are several ways of checking or testing primality of integers, I used Trial division method. There are other better method I believe. Check below:
And 2 was used here because 1 is not a prime number by defination, which state that a prime number must have two factors: 1 and itself, but 1 has only itself! too bad for 1 though.use warnings; use strict; is_prime( $ARGV[0] ); sub is_prime { my ($val) = @_; my $num_sqrt = int( sqrt($val) ); my @factor; for ( 2 .. $num_sqrt ) { if ( ( $val % $_ ) == 0 ) { push @factor, $_; } } my $count = () = @factor; print $val, " is a Prime Number" if $count == 0; }
|
---|