#! perl -slw use strict; use List::Util qw[max]; use Quantum::Superpositions UNARY => ['CORE::int']; use Benchmark qw[cmpthese]; sub pl_all{ $_ || return 0 for @_; 1 } sub PL_is_prime{ $_[0]==2 || pl_all map{ $_[0] % $_ } 2..sqrt($_[0])+1 } sub QS_is_prime{ $_[0]==2 || $_[0] % all(2..sqrt($_[0])+1) != 0 } our ($QS_count, $PL_count); cmpthese( -1, { 'Q::S' => q[ $QS_count = 0; $QS_count += QS_is_prime($_) ? 1 : 0 for 2..10000; ], 'perl' => q[ $PL_count = 0; $PL_count += PL_is_prime($_) for 2..100000; ], }); print "Primes found: Q::S:$QS_count; Perl:$PL_count\n\n"; sub QS_factors { eigenstates (int($_[0] / any(2..$_[0]-1)) == ($_[0] / any(2..$_[0]-1))); } sub PL_factors{ grep{ $a = $_[0]/$_; int($a) == $a } 2 .. $_[0] - 1; } our (@QS_factors, @PL_factors); cmpthese( -1, { QS_factors => q[ $a=1; @QS_factors = QS_factors($_) for map{$a *= $_} 2 .. 6; ], PL_factors => q[ $a=1; @PL_factors = PL_factors($_) for map{$a *= $_} 2 .. 9; ], }); print "qs:\n@QS_factors\npl:\n@PL_factors\n"; sub QS_GCD{ my ($x, $y) = @_; my $common = all(any(QS_factors($x)), any(QS_factors($y))); any(eigenstates $common) >= all(eigenstates $common); } sub PL_GCD{ my ($x, $y) = @_; my %any; max grep{ ++$any{$_} > 1 } PL_factors($x), PL_factors($y); } our ($QS_GCD, $PL_GCD); cmpthese( -20, { QS_GCD => q[ $a=$b=1; $QS_GCD = QS_GCD($a=$b, $b *= $_) for 2 .. 6; ], PL_GCD => q[ $a=$b=1; $PL_GCD = PL_GCD($a=$b, $b *= $_) for 2 .. 9; ], }); print "QS:$QS_GCD - PL:$PL_GCD"