Shlomi Fish avatar Shlomi Fish committed 5c0fa61

Fixed Euler #111.

The problem was a bug in bsd-games' primes.

Comments (0)

Files changed (1)

project-euler/111/euler-111.pl

 
 use List::Util qw(sum);
 
-sub is_prime
+use List::MoreUtils qw(all);
+
+sub is_prime1
 {
     my $n = shift;
     return (length(scalar(`primes $n @{[$n+1]}`)) > 0);
 }
 
+sub is_prime2
+{
+    my $n = shift;
+
+    return all { $n % $_ } (2 .. int(sqrt($n))+1);
+}
+
+=begin verification
+
+This function throws an exception due to a bug in bsg-games' primes. 
+
+sub is_prime
+{
+    my $n = shift;
+
+    my $v1 = is_prime1($n);
+    my $v2 = is_prime2($n);
+
+    if ($v1 xor $v2)
+    {
+        die "Results don't match for $n!";    
+    }
+
+    return $v1;
+}
+
+=end verification
+
+=cut
+
+*is_prime = \&is_prime2;
+
 # print is_prime(5), "\n";
 # print is_prime(10), "\n";
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.