Respected Monks,
This will perhaps go in the history of PerlMonks (and otherwise) as the stupidest prime number detector ever!!, but I am trying to learn how to create modules and needed something to work on. If after checking this script, the Monks here decide that I am too stupid to even attempt to create modules, I can certainly live with it.
So, as shown below, the script detects if the supplied number is a prime or not. If the number is a prime and is 5 or 6 digits, the script becomes somewhat slower. At 7 digit primes, it just starts getting exponentially slower and anything above that, it just hangs.However, if the number is NOT a prime, and even if it is 20 digits, it shows the output in a flash!!
use strict; use warnings; use 5.10.1; use bignum; use Time::HiRes qw(gettimeofday tv_interval); print "Enter the integer: "; chomp (my $prime_candidate = <STDIN>) ; exit if $prime_candidate <= 0 && say "$prime_candidate is not a prime. +"; my $is_prime = 1; my $start_time = [gettimeofday]; foreach my $each_num (2..$prime_candidate) { if ($prime_candidate % $each_num == 0 && $prime_candidate - $each_ +num != 0 ) { say "$prime_candidate is not a prime"; $is_prime = 0; exit; } } my $elapsed_time = tv_interval($start_time); say "$prime_candidate is a prime " if $is_prime != 0; printf "The [$0] script took %2.2f seconds to finish\n", $elapsed_time +;
Given below are the outputs:
$ perl time_is_prime_v1.pl Enter the integer: 99929 99929 is a prime The [time_is_prime_v1.pl] script took 0.79 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 999983 999983 is a prime The [time_is_prime_v1.pl] script took 7.69 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 9999991 9999991 is a prime The [time_is_prime_v1.pl] script took 73.97 seconds to finish $ perl time_is_prime_v1.pl Enter the integer: 1320486952377516576 1320486952377516576 is not a prime
So why have I put up this monstrosity here? I just want to find a way to make it work faster so that I can create a module out of it by doing the required changes, test the output by checking it against the output of Math::Prime::Util and be happy that I've created a module!!. I will not ever dare to put it up on CPAN as I know it's too stupid :D, I just want to play with Perl.
In reply to Stupidest Prime Number detector ever!! by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |